简体   繁体   English

CakePHP关系模型:完全忽略模型

[英]CakePHP Relational models: Completely ignoring model

I have a pretty simple relational model setup. 我有一个非常简单的关系模型设置。 When using $this->model->find('all',$params) , the results do not return the complete relational data set. 使用$this->model->find('all',$params) ,结果不会返回完整的关系数据集。 I'm pulling my hair out here. 我在这里拔头发。

Here's my table setup: 这是我的桌子设置:

Table qs_skus: 表qs_skus:

id (AI, PK)
sku_name
profile
...

Table net_lengths_in_skus: 表net_lengths_in_skus:

id (AI,PK)
quick_ship_skus_id
net_lengths_id

Table net_lengths 表net_lengths

id (AI,PK)
name

The way the models are configured are: 模型的配置方式为:

  • Model QuickShipSku $hasMany=array('NetLengthsInSku') 型号QuickShipSku $hasMany=array('NetLengthsInSku')
  • Model NetLengthsInSku $hasOne='NetLength' 型号NetLengthsInSku $hasOne='NetLength'
  • All models have $actAs = array('Containable') 所有模型都有$actAs = array('Containable')

When doing the following, I get only the first relationship queried, the last relationship is completely ignored: 当执行以下操作时,我仅查询到第一个关系,而最后一个关系被完全忽略:

$model = $this->QuickShipSku->find('all',
        array(
            'contain' => array(
                'NetLengthsInSku' => array('NetLength')
        )
    );

Output: 输出:

Array
(
    [0] => Array
        (
            [QuickShipSku] => Array
                (
                    [id] => 3
                    [sku_name] => 1112-8
                    [product_name] => Product A
                    [sku_specie_id] => 1
                    [members_ft] => 8
                    [profile] => Profile Description
                )

            [NetLengthsInSku] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                            [quick_ship_skus_id] => 3
                            [quick_ship_net_length_id] => 1
                        )
                )
        )
    )

For each NetLengthsInSku there should be a NetLength , but it's not even being queried. 对于每个NetLengthsInSku ,都应该有一个NetLength ,但甚至没有被查询。

Any ideas? 有任何想法吗?

What you have here is a many to many relationship. 您在这里拥有多对多关系。 That means an association called hasAndBelongsToMany. 这意味着一个名为hasAndBelongsToMany的关联。

If you are using Cake 2.x you should have something like this: 如果您使用的是Cake 2.x,则应具有以下内容:

class QsSku extends AppModel {
    public $hasAndBelongsToMany = array(
      'NetLength' =>
        array(
            'className' => 'NetLength',
            'joinTable' => 'net_lengths_in_skus',
            'foreignKey' => 'quick_ship_skus_id',
            'associationForeignKey' => 'net_lengths_id',
        )
    );
}

You should do something similar to your NetLength model as well. 您还应该执行类似于NetLength模型的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM