我在两个表(联系人和报价)之间建立了关系,我正在尝试访问联系人报价控制器中的信息。所以在QuoteController.php中我有以下代码:
$contacts = $this->Quote->Contact->find('list', array('fields' => array(
'Contact.id',
'Contact.name',
'Contact.company',
'Contact.mainAddressLine2',
'Contact.mainAddressTown',
'Contact.mainAddressPostCode',
'Contact.mainAddressCountry'
)));
$this->set(compact('contacts'));
echo '<pre>';
print_r($contacts);
echo '</pre>';
print_r语句的输出是:
Array
(
[1] => Joe Bloggs
[21] => Jane Doe
)
正如您所看到的,我只获取了id和名称,由于某种原因,公司,mainAddressLine2等在数组中出现。
最终,我希望用户能够从下拉列表中选择联系人姓名,然后将在“报价”视图中填充“联系人”表中的详细信息。
任何帮助表示感谢。
答案 0 :(得分:2)
$this->Quote->Contact->find('list',
只会渲染1d数组,将其更改为all
而不是list
。您可能还想查看containable behaviors。
btw pr($contacts);
是php的内置便利功能
答案 1 :(得分:1)
如果您希望列出所有字段,为什么不使用find('all')。具有2个字段参数的查找列表将返回具有key-&gt;值的数组,而添加第3个字段将对结果进行分组。请参阅find options
上的文档$contacts = $this->Quote->Contact->find('all', array('fields' => array(
'Contact.id',
'Contact.name',
'Contact.company',
'Contact.mainAddressLine2',
'Contact.mainAddressTown',
'Contact.mainAddressPostCode',
'Contact.mainAddressCountry'
)));
$this->set(compact('contacts'));
echo '<pre>';
print_r($contacts);
echo '</pre>';