【发布时间】:2011-12-12 14:44:31
【问题描述】:
似乎没有人对此有任何问题,所以要么我做错了,要么没有人尝试过:
我有一个模型“Infocenter”,其中包含许多“InfocenterArticle”。为了获取包括相关内容的数据,我将 Containable 行为附加到两者。
到目前为止,这一直很好,因为我附加了一个由我自己实现的“HasImageAttachment”行为。问题是在包含的模型上,我的行为的回调不会被调用。
我的模特:
class Infocenter extends AppModel {
...
$actsAs = array('HasImageAttachment', 'Containable');
$hasMany = array('InfocenterArticle');
...
}
class InfocenterArticle extends AppModel {
...
$actsAs = array('Containable');
$belongsTo = array('Infocenter');
...
}
在我的控制器中我调用:
$conditions = array('InfocenterArticle.id' => $id);
if ($this->notLoggedIn()) $conditions['InfocenterArticle.freigabe'] = 1;
$article = $this->InfocenterArticle->find('first', array(
'contain' => array(
'Infocenter',
'Infocenter.InfocenterArticle' => array(
'fields' => array('id', 'title', 'freigabe'),
'order' => array(
'InfocenterArticle.order_index' => 'desc',
'InfocenterArticle.created' => 'desc',
'InfocenterArticle.title' => 'asc'
),
'conditions' => array(
'InfocenterArticle.infocenter_id' => 'Infocenter.id'
),
),
),
'conditions' => $conditions,
));
我可以看到我的 HasImageAttachmentBehavior::setup() 方法被调用,但 HasImageAttachmentBehavior::afterFind()(以及 beforeFind())没有被调用。虽然调用了 Infocenter::afterFind(),这使我能够进行一些肮脏的黑客攻击,现在已经足够了,但我讨厌它。
我做错了什么?
编辑:回复 RichardAtHome 评论的附加信息。
1) 我的行为适用于未附加 Containable 的模型。
2) 我确保不会通过放置一个简单的 die(); 来调用 afterFind();在第一行。脚本不会死()。
3) 签名应该没问题,我仔细检查了。
4) 我使用的是 CakePHP 1.3。
感谢您的帮助。
【问题讨论】:
-
你确定 afterFind() 没有触发吗?您是否尝试过将一些调试代码作为 afterFind() 的第一行以确保它没有被调用并且 afterFind() 中的某些内容不起作用?另外,请确保您的 afterFind() 参数与 Cake 食谱中的参数匹配(您没有说明您使用的是哪个版本的 CakePHP,1.3 和 2 具有不同的 afterFind 签名)
-
我已将您要求的信息添加到我的问题中。
-
afterFind 在模型中对我有效,但在同一模型使用的行为的 afterFind 中无效。
标签: php cakephp callback behavior containable