似乎没有人对此有任何问题,所以要么我做错了,要么没有人尝试过:
我有一个模型“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。
谢谢你的帮助。