tags:

views:

48

answers:

1

I have a model 'listing' with a field 'created' which is in a datetime format. I need to list in a view all listings that were created over 2 weeks ago. An extra thing if possible is to somehow mark them as expired.

This is in cakePhp 1.27

+1  A: 

Hi I think you can use a simple script to do that in cake.

      function meScript(){
            // first load your model if necessary
            $listingModel = ClassRegistry::init('Listing');

            // Then set your date margin to , two weeks back
            $date_margin =  date("Y-m-d H:i:s", strtotime('-2 week')) ;

            // now retrieve all records that were created over 2 weeks ago  
            $listings = $listingModel ->find('all', array(
                                                        'conditions' => array('created <' => $date_margin),
                                                        )


                            );

}

That's pretty much it. Since the margin date is in "Y-m-d H:i:s" format, the " 'created <' => $date_margin" condition will retrieve all records that were created before that date.

As for the next step of marking them as expired: Simply loop through the results and use their ids to set your 'expired' field (or whatever it is called in your database table) to 'true'.

Vicer
Is this script to be put in the listings_controller like all other actions which I call from the view pages?? or in a separate JavaScript file? My model 'listing' already has data in it and a controller with CRUD actions I was just trying to show in a view page all listings that were over 2 weeks old.I have just started learning cakephp.
joy
Vicer
You could also put this code in the model layer (eg. `Listing::findExpired()`). This would allow you to call `$this->Listing->findExpired();` at controller level.(You would need to put `return $listings;` at the bottom though.)
deizel