【发布时间】:2014-03-12 19:48:37
【问题描述】:
如何将 beforeFind() 条件传播到关联模型中?
以 School、Course 和 Term 为例(应该是 Class,但这个词在 php 中是保留的),这是 Course 模型上使用的代码:
/**
* beforeFind method
*
* Filters by the school (based on the url)
* @param array $query The find query
* @return array $query The query with the added condition
*/
public function beforeFind($query = array()) {
parent::beforeFind($query);
$query['conditions'] = (is_array($query['conditions'])) ? $query['conditions'] : array();
$this->School->id = CakeSession::read('School');
$conditions = array(get_class($this).'.school_id' => $this->School->id);
$query['conditions'] = array_merge($query['conditions'], $conditions);
return $query;
}
当我获取课程时,条件适用:
$this->Course->contain = array('School');
$this->Course->find('all');
这会根据会话返回当前学校的所有课程。 当我取课时,学校条件不适用:
$this->Term->contain = array('Course');
$this->Term->find('all');
它返回所有条款,包括所有课程,没有课程模型上的 beforeFind 条件。 是否可以在传播到其子模型的父模型上添加 beforeFind 条件,而不必在每个子模型上编写 beforeFind 代码?
【问题讨论】:
标签: php cakephp before-filter