【问题标题】:Deep Containable associations not working in cakephp 1.3.0深度可包含关联在 cakephp 1.3.0 中不起作用
【发布时间】:2012-04-20 09:38:13
【问题描述】:

我的模型中有以下关系

基本索引属于申请人

申请人有很多请求

因此,我想检索 BasicIndexing 模型并包含申请人模型和申请人对应的请求,如下面的代码所示

$fullCondition = array(
    'contain' => array(
        'Applicant' => array(
            'Request',
            'fields'=>array('Applicant.surname','Applicant.first_name','Applicant.id')
        )
    ),
    'conditions' =>  $conditions,
    'fields'=>array('BasicIndexing.application_date','BasicIndexing.application_number')
);
$this->loadModel('BasicIndexing');
$searchResult = $this->BasicIndexing->find('all',$fullCondition);

问题是返回到$searchResult 的结果根本不包含Request 模型。它只包含申请者模型并忽略请求模型。我尝试使用与申请者无关的模型,但收到警告说该模型与申请者模型无关。

Array
(
    [0] => Array
        (
            [BasicIndexing] => Array
                (
                    [application_date] => 2012-04-17
                    [application_number] => BIA170420124356
                )

            [Applicant] => Array
                (
                    [surname] => Kermit
                    [first_name] => Frog
                    [id] => 4f8d3b63-c2bc-48a1-9fb5-0290c982293d
                )
        )
)

是我做错了什么还是蛋糕 1.3.0 版本有问题?

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: cakephp cakephp-1.3 containable


    【解决方案1】:

    认为这是因为您的fields 数组。您要么需要将Request.* 添加到现有的fields 数组,要么将fields 数组添加到Request

    所以它应该看起来像以下两个示例之一:

    $fullCondition = array(
        'contain' => array(
            'Applicant' => array(
                'fields'=>array('Applicant.surname','Applicant.first_name','Applicant.id'),
                'Request' => array(
                    'fields' => array('*')
                )
            )
        ),
        'conditions' =>  $conditions,
        'fields'=>array('BasicIndexing.application_date','BasicIndexing.application_number')
    );
    

    $fullCondition = array(
        'contain' => array(
            'Applicant' => array(
                'fields'=>array('Applicant.surname','Applicant.first_name','Applicant.id', 'Request.*'),
                'Request'
            )
        ),
        'conditions' =>  $conditions,
        'fields'=>array('BasicIndexing.application_date','BasicIndexing.application_number')
    );
    

    【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。基本上,contain 是不稳定的,会返回关联的“belongsTo”关系的结果,而不是“hasMany”的结果。我只需要一个级别的递归,事实证明'1'对于递归声明来说是一个特殊的东西(以及-1和0)。因此,我获得所需数据的唯一方法是使用笨重的“递归”声明,但将其设置为不必要的高“2”。

    我知道这是个老问题,但我只是花了一整天的时间来解决这个问题,我希望能把这个问题留给其他一些被那个过时版本的蛋糕卡住的可怜的笨蛋……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多