【问题标题】:cakephp table relation emptycakephp 表关系为空
【发布时间】:2012-01-10 22:53:01
【问题描述】:

我想知道我在这里做错了什么。

我有这种关系

专有模式:

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' => ''
    )
);

在业主中,我尝试获得这样的住宅名称

<?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'));
   }

}
?>

但在我的老板看来

    <?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']; ?>&nbsp;</td>
            <td style="text-align: center;"><?php echo $proprietaire['Proprietaire']['prenom']; ?>&nbsp;</td>
            <td style="text-align: center;"><?php echo $proprietaire['Residence']['nom']; ?>&nbsp;</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(__('&Ecirc;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 

什么是坏的?我的表关系?

感谢您的帮助。

【问题讨论】:

  • 你从pr($residences)得到什么?另外,你看过蛋糕生成的 SQL 查询了吗?
  • 您确定您的数据库表设置正确,并且您的表中确实存在相关的“住宅”数据吗?

标签: cakephp pagination


【解决方案1】:

确保您已在住宅模型中设置 displayField 属性

var $displayField = 'nom';

或者像这样进行查找:

$residences = $this->Proprietaire->Residence->find('list', array('Residence.id', 'Residence.nom'));

请检查您的查询,将调试级别设置为 2 并在您的布局或视图中添加以下内容

<?php echo $this->element('sql_dump'); ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多