All pastes #634317 Raw Edit

Count method in CakePHP

public php v1 · immutable
#634317 ·published 2007-07-26 10:56 UTC
rendered paste body
/** * Returns number of rows matching given SQL condition. * * @param array $conditions SQL conditions array for findAll * @param int $recursize The number of levels deep to fetch associated records * @return int Number of matching rows * @see Model::findAll */	function findCount($conditions = null, $recursive = 0) {		list($data) = $this->findAll($conditions, 'COUNT(*) AS count', null, null, 1, $recursive);		if (isset($data[0]['count'])) {			return $data[0]['count'];		} elseif (isset($data[$this->name]['count'])) {			return $data[$this->name]['count'];		}		return false;	}/** * Returns a resultset array with specified fields from database matching given conditions. * By using the $recursive parameter, the call can access further "levels of association" than * the ones this model is directly associated to. * * @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...) * @param mixed $fields Either a single string of a field name, or an array of field names * @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") * @param int $limit SQL LIMIT clause, for calculating items per page. * @param int $page Page number, for accessing paged data * @param int $recursive The number of levels deep to fetch associated records * @return array Array of records */	function findAll($conditions = null, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {		$db =& ConnectionManager::getDataSource($this->useDbConfig);		$this->id = $this->getID();		$offset = null;		if (empty($page) || !is_numeric($page) || intval($page) < 1) {			$page = 1;		}		if ($page > 1 && $limit != null) {			$offset = ($page - 1) * $limit;		}		if ($order == null) {			if ($this->order == null) {				$order = array();			} else {				$order = array($this->order);			}		} else {			$order = array($order);		}		$queryData = array(			'conditions' => $conditions,			'fields'    => $fields,			'joins'     => array(),			'limit'     => $limit,			'offset'	=> $offset,			'order'     => $order		);		if (!empty($this->behaviors)) {			$behaviors = array_keys($this->behaviors);			$ct = count($behaviors);			for ($i = 0; $i < $ct; $i++) {				$ret = $this->behaviors[$behaviors[$i]]->beforeFind($this, $queryData);				if (is_array($ret)) {					$queryData = $ret;				} elseif ($ret === false) {					return null;				}			}		}		$ret = $this->beforeFind($queryData);		if (is_array($ret)) {			$queryData = $ret;		} elseif ($ret === false) {			return null;		}		$results = $db->read($this, $queryData, $recursive);		if (!empty($this->behaviors)) {			$b = array_keys($this->behaviors);			$c = count($b);			for ($i = 0; $i < $c; $i++) {				$ret = $this->behaviors[$b[$i]]->afterFind($this, $results, true);				if (is_array($ret)) {					$results = $ret;				}			}		}		$return = $this->afterFind($results, true);		if (isset($this->__backAssociation)) {			$this->__resetAssociations();		}		return $return;	}