【问题标题】:CakePHP Containable doesn't fire other behaviors' callbacks on contained models?CakePHP Containable 不会在包含的模型上触发其他行为的回调?
【发布时间】: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


【解决方案1】:

目前我不相信 CakePHP 核心支持跨包含模型的行为。

这可能是由于可能的递归,如果你有奇怪的包含数组,那么行为可能会被错误地调用。

CakePHP Lighthouse 项目上有一篇关于调用关联模型的行为的长文,并提供了一些解决方法的建议。

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

【讨论】:

    【解决方案2】:

    我刚刚写了一篇关于如何处理这种情况的详细文章。

    适用于 CakePHP 2.x 和 PHP 5.4+。

    Containable behavior alternative function

    【讨论】:

    • 请注意 link-only answers 是不鼓励的,所以答案应该是寻找解决方案的终点(与另一个中途停留的参考相比,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,并保留链接作为参考。
    【解决方案3】:

    显然这是一个经过深思熟虑的设计点(??!?)。因此,如果您希望关联模型的行为与模型完全一样,则必须一直升级到 3.0。叹息。

    这是一个广泛的讨论:https://github.com/cakephp/cakephp/issues/1730 和最直接的食谱修复:https://schneimi.wordpress.com/2009/09/06/behavior-afterfind-on-model-associations/

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多