Sent emails to users who haven't check the website for a long time (using last vist...but how to write findbyAttributes command...?)

Dear all,

I am doing a crontab that to email the users who haven’t check our website for quite a long time.

I want to emails the user, for example, haven’t on board for 1-2 weeks.

I have a column called "lastvist" in my tbl_user table.

But how can I write the filter command?

something like:


$users=Users::model()->findAllByAttributes->(...2weeks off the board since last visit....)

I store the lastvist via timestamp like this: 1343043800

Now i am doing something like this but i believe it’s very low efficient since findAll is very expensive.


$now=time();

$users=Users::model()->findAll();

foreach($users as $user){

$intervel=$now-$user->lastvisit;

$days=intval($intervel/86400);

if($days > 14){

//send emails

}

Any ideas would be appreciated…

Thanks so much! :D

using that findall, compare lastvisit date with now().

If you want to use findAll(), you can use it with a CDbCriteria

[list=1]

[*]using $criteria->compare(‘lastvisit’, ‘>=’ . $value) with $value being the value in seconds of now minus 2 weeks

[*]or using $criteria->addCondition('lastvisit >= ’ . $value)

[/list]

I would, however, use a simple SQL query to select the needed rows, if my goal is simply to send emails to the records matching a condition.

Use CDbExpression(‘NOW()’) to compare the current date with the ‘lastvist’ in tbl_user.

Happy coding.

Thanks so much for your help!! Because I also need to get user’s id and other information so I use findAll instead of SQL query.

I am using something like this now:




$now=time();

$value=$now-86400*12;   //12days

$criteria = new CDbCriteria();

$criteria->condition = "lastvisit >= $value";


$users=Users::model()->findAll($criteria);

foreach($users as $user){

.....

.....



What do you think about this …(above part)…Because corncobs are not that easy to test now …so I gotta test it tomorrow…

It looks good to me, except that you need visitors who haven’t logged in the past 12 days, so you should change your condition to


$criteria->condition = "lastvisit < $value";

Haha my bad…That is stupid… :D :D

Thanks for your help dude!

Have a good day.

Cheers