繁体   English   中英

CakePHP 2.x HABTM返回的结果不匹配

[英]CakePHP 2.x HABTM returning is no matching results

我在cakephp的habtm上有问题,我还不知道如何解决。 我有三个表:setores,veiculos和setores_veiculos。

设置模型:

public $hasAndBelongsToMany = array(
    'Veiculo' => array(
        'className' => 'Veiculo',
        'joinTable' => 'setores_veiculos',
        'foreignKey' => 'setor_id',
        'associationForeignKey' => 'veiculo_id',
        'unique' => 'keepExisting'
    )
);

Veiculo模型:

public $hasAndBelongsToMany = array(
    'Setor' => array(
        'className' => 'Setor',
        'joinTable' => 'setores_veiculos',
        'foreignKey' => 'veiculo_id',
        'associationForeignKey' => 'setor_id',
        'unique' => 'keepExisting'
    )
);

在我的setores控制器中,我有:

$options = array('conditions' => array('Setor.' . $this->Setor->primaryKey => $id));    
$setores = $this->Setor->find('first', $options);

然后在我的调试中:

array(
'Setor' => array(
    'id' => (int) 5,
    'nome' => '2º Batalhão',
    'secretaria_id' => (int) 6,
    'sigla' => '2º BPM',
    'status_id' => (int) 1
),
'Veiculo' => array()
)

我不明白为什么未列出Veiculo(车辆)。 插入工作正常。

我期望:

 'Veiculo' => array(
     0 => array(
        'id' => 1,
        'name' => 'Corolla'
     ),
     1 => array(
          'id' => 2,
        'name' => 'Hillux'
    )
  );

在我的veiculos控制器中,列出了setore。 有什么建议吗?

在此先感谢您,对不起我的英语。

在我看来很好。 尝试改变

'unique' => 'keepExisting'

'unique' => true

目前,我遵循文档中说明的简单规则,如果我使用的是简单链接表,即不在其中存储任何数据,则使用HABTM并将unique设置为true ,否则,我使用hasMany through(The Join Model ),设置为keepExisting的唯一值,以免丢失额外的存储数据。

发布setores_veiculos的数据转储。

暂无
暂无

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

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