我想知道我在这里做错了什么。
我有这种关系
Proprietaire模型:
var $belongsTo = array(
'Residence' => array(
'className' => 'Residence',
'foreignKey' => 'residence_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
住宅模型
var $hasMany = array(
'Proprietaire' => array(
'className' => 'Proprietaire',
'foreignKey' => 'residence_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
在proprietaire我尝试获得这样的居住名称
<?php
class ProprietairesController extends AppController {
var $name = 'Proprietaires';
function index() {
$this->Proprietaire->recursive = 1;
$this->set('proprietaires', $this->paginate());
$residences = $this->Proprietaire->Residence->find('list');
$this->set(compact('residences'));
}
}
?>
但在我的proprietaire观点
<?php foreach ($proprietaires as $proprietaire): ?>
<?= print_r($proprietaire['Residence'])?>
<tr <?= $this->Cycle->css(array('list-line-odd', 'list-line-even'),true); ?>>
<td><?php echo $proprietaire['Proprietaire']['nom']; ?> </td>
<td style="text-align: center;"><?php echo $proprietaire['Proprietaire']['prenom']; ?> </td>
<td style="text-align: center;"><?php echo $proprietaire['Residence']['nom']; ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('Modifier', true), array('action' => 'edit', $proprietaire['Proprietaire']['id'])); ?>
<?php echo $this->Html->link(__('Supprimer', true), array('action' => 'delete', $proprietaire['Proprietaire']['id']), null, sprintf(__('Êtes-vous certain de vouloir supprimer # %s?', true), $proprietaire['Proprietaire']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
print_r(proprietaire ['Residence']给我一个空数组。
Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1 Array ( ) 1
什么是坏事?我的表关系?
感谢您的帮助。
答案 0 :(得分:0)
确保在住宅模型中设置了displayField属性
var $displayField = 'nom';
或者像这样找到:
$residences = $this->Proprietaire->Residence->find('list', array('Residence.id', 'Residence.nom'));
否则检查您的查询,将调试级别设置为2并在布局或视图中添加以下内容
<?php echo $this->element('sql_dump'); ?>