【问题标题】:CakePHP append to Containable/ContainCakePHP 附加到 Containable/Contain
【发布时间】:2012-06-26 18:38:14
【问题描述】:

已经声明后如何追加到包含?

  1. 我称之为常规的包含/查找:

    // A controller
    $this->Site->contain(array('User'));
    $site = $this->Site->find();
    
  2. 我想自动添加要包含的内容。我正在考虑通过添加到 find 函数中来在模型中执行此操作......像这样:

    // Site.php Model
    function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
        if(!isset($this->containVariable) || !in_array('Box', $this->containVariable)) {
            $this->containVariable[] = 'Box';
        }
        parent::find($conditions, $fields, $order, $recursive);
    }
    

为了完成这项工作(自动添加模型 Box 以包含),我只需将 $this->containVariable 更改为包含已包含内容的数组的函数或变量。在这种情况下,这将返回 array('User')。已经声明时如何附加到包含?有没有包含contain的变量?

【问题讨论】:

    标签: cakephp cakephp-1.2


    【解决方案1】:

    除非有人能找到其他解决方案,否则我得出的结论是:

    不幸的是,我试图做的似乎不可能如何设计可包含性。我必须手动将我的模型添加到每个 ->contain() 调用中。

    【讨论】:

      【解决方案2】:

      我修复了一个类似的问题,即多个 find 调用的持久包含(使用 cakephp 1.3)。在为某个模型声明包含后,将为该模型设置此值:

      $this->Behaviors->Containable->runtime
      

      -其中 $this 是一些模型对象。 'runtime' 是一个数组,它本质上包含包含信息。所以我相信你很接近,但不是:

      $this->containVariable[] = 'Box'
      

      你会:

      $this->Behaviors->Containable->runtime['someModel']['contain'][] = 'Box'
      

      要能够指定的不仅仅是模型,您必须设置为如下处理(可能相应地构建一个方法):

      $this->Behaviors->Containable->runtime['someModel']['contain'][] = $model_contain_array
      

      $model_contain_array 只是我刚刚为包含您想为特定模型添加的包含信息的数组选择的任意名称。 $model_contain_array 的一个例子可能是:

      array('modelName' => array(
          'fields' => array('id', 'other'),
          'otherModelName' => array(
              'fields' => array('id', 'otherfield', 'etc')
          )
      )
      

      【讨论】:

        猜你喜欢
        • 2011-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-19
        • 2012-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多