rrbot
(Rob Rhyne)
1
I have a ConsoleCommand to run some mail queue stuff via cron.
I’m trying to send emails based on the current time and the time a mail should be sent.
I ended up writing this in MySQL, and it works fine, but I know there’s a simpler way to achieve this, possibly with a ->compare().
Can someone rewrite this for me in the most Yii manner?
$mq = MailQueue::model();
$con = array(
'condition' => "try_sent < 1 AND sent_time is NULL AND time_to_send < NOW()",
);
$results = $mq->findAll($con);
Thanks!
samdark
(Alexander Makarov)
2
In your case it’s more efficient to use
Yii::app()->db->createCommand($sql)->queryAll();
You can move it into the method of MailQueue model and use it like this:
$emails = MailQueue::model()->getEmailsToSend();
foreach($emails as $email){
//
}
rrbot
(Rob Rhyne)
3
Thanks samdark!
The biggest part of that query I struggled with was the time comparison. Does Yii have any built in methods for comparing dates?
samdark
(Alexander Makarov)
4
Depends on how are you storing dates.
samdark
(Alexander Makarov)
6
rrbot
(Rob Rhyne)
7
Thanks for the references Sam. I was trying to get too tricky with it I suppose.