logo

Only showing active records – model wide conditions cakePHP

logo

I know its been a bit of a whole since my last update but have a bit of useful info here:

 function beforeFind(&$queryData)
	{
	    $conditions = $queryData['conditions'];

	    if (!is_array($conditions)) {
	        if (!$conditions) {
	            $conditions = array();
	        } else {
	            $conditions = array($conditions);
	        }
	    }

	    if (!isset($conditions['active']) && !isset($conditions[$this->alias . '.active'])) {
	        $conditions[$this->alias . '.active'] = 1;
	    }

	    $queryData['conditions'] = $conditions;
	    return true;
	}

Put this in the model that you have an ‘active’ field in and it will save you having to add a condition into each find statement to check whether that record is flagged as active / inactive!

Share with your friends and help out this site:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogosphere News
  • E-mail this story to a friend!
  • Identi.ca
  • LinkedIn
  • Live
  • MisterWong
  • MySpace
  • Ping.fm
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz

logo
Powered by Nu Order Webs