加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

cakephp 2.x beforeFind相关模型

发布时间:2020-12-13 16:52:52 所属栏目:PHP教程 来源:网络整理
导读:如何将beforeFind()条件传播到关联模型中? 以学校,课程和术语(应该是一个类,但这个词在php中保留)为例,这是课程模型中使用的代码: /** * beforeFind method * * Filters by the school (based on the url) * @param array $query The find query * @return
如何将beforeFind()条件传播到关联模型中?

以学校,课程和术语(应该是一个类,但这个词在php中保留)为例,这是课程模型中使用的代码:

/**
 * 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代码?

解决方法

不幸的是,我认为这不可能在Cake 2.x.如果你看一下 this ticket from Github,在Cake 3.0(在页面底部)修复了使用关联模型的beforeFind的问题,但除了手动更改核心文件之外,没有旧版本的解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读