CakePHP Containable 不会触发其他行为。包含模型的回调?
似乎没有人对此有问题,所以要么我做错了,要么没有人尝试过:
我有一个模型“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)我确保 afterFind() 不会通过放置一个简单的 die() 来调用;在第一行。脚本不会死()。
3)签名应该没问题,我仔细检查过。
4)我使用的是CakePHP 1.3。
感谢您的帮助。
Nobody seems to have a problem with that so either I'm doing it wrong or no one ever tried:
I have a model "Infocenter" which has many "InfocenterArticle"s. To fetch data including the related stuff I attached the Containable behavior to both.
This worked well until now that I attached a "HasImageAttachment" behavior implemented by myself. The problem is that on contained models the callbacks of my behavior don't get called.
My Models:
class Infocenter extends AppModel {
...
$actsAs = array('HasImageAttachment', 'Containable');
$hasMany = array('InfocenterArticle');
...
}
class InfocenterArticle extends AppModel {
...
$actsAs = array('Containable');
$belongsTo = array('Infocenter');
...
}
In my Controller I call:
$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,
));
And I can see that my HasImageAttachmentBehavior::setup() method is called but the HasImageAttachmentBehavior::afterFind() (as well as beforeFind()) are not. Infocenter::afterFind() is called though, which enabled me to do some dirty hacking, good enough for now, but I hate it.
What am I doing wrong?
Edit: Additional info in reply to RichardAtHome's comment.
1) My behavior works on models that don't have Containable attached.
2) I made sure that afterFind() doesn't get called by putting a simple die(); in the first line. The script doesn't die().
3) Signature should be okay, I double checked.
4) I'm using CakePHP 1.3.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
目前我不相信 CakePHP 核心支持跨包含模型的行为。
这可能是由于可能的递归,如果您有奇怪的包含数组,则可能会错误地调用行为。
CakePHP Lighthouse 项目上有一篇很长的文章,内容涉及关联模型上的调用行为,并提供了一些解决方法的建议。
Currently I don't believe CakePHP core supports Behavior across Contained models.
It may be due to possible recursion, if you have weird contain array the behaviors may be called incorrectly.
There is a long post on the CakePHP Lighthouse project regarding calling behaviors over associated models, with a few recommendations for workarounds.
http://cakephp.lighthouseapp.com/projects/42648/tickets/95-afterfind-and-beforefind-callbacks-not-working-on-associated-models-was-translate-behavior-should-translate-associated-model-data
我刚刚写了一篇关于如何处理此类情况的详细文章。
它适用于 CakePHP 2.x 和 PHP 5.4+。
可包含的行为替代函数
I just wrote an extensive entry on how to deal with this type of scenario.
It is for CakePHP 2.x and PHP 5.4+.
Containable behavior alternative function
显然这是一个故意的设计点(?!?)。因此,如果您希望关联模型的行为完全像模型一样,则必须一直升级到 3.0。叹。
这里有一个广泛的讨论: https://github.com/cakephp/cakephp/issues/1730< /a>
最简单的食谱修复: https:// schneimi.wordpress.com/2009/09/06/behavior-afterfind-on-model-associations/
Apparently this was a deliberate point of design(??!?). So you have to upgrade all the way to 3.0 if you want associated models to behave fully like models. Sigh.
Here is an extensive discussion: https://github.com/cakephp/cakephp/issues/1730
and the most straightforward cookbook fix: https://schneimi.wordpress.com/2009/09/06/behavior-afterfind-on-model-associations/