Cakephp未定义索引

时间:2012-08-15 08:28:09

标签: cakephp indexing

创建视图时,我在undefined index error: Account的行上获得$senderName['Account']['company_name'],但在调试变量时,数组打印出来

array(
    (int) 0 => array(
        'Account' => array(
            'id' => '0',
            'street' => 'SYSTEM',
            'city' => 'SYSTEM',
            'postcode' => '0',
            'state' => 'SYS',
            'country' => 'SYS',
            'active' => true,
            'company_name' => 'SYSTEM',
            'abn' => '0'
        ),
        'Template' => array(),
        'User' => array(),
        'Invoice' => array()
    ),

这是我的观点的代码

<?php foreach($invoice as $invoices):?>
                <?php foreach($senderName as $senderName):?>
                <?php foreach($receiverName as $receiverName):?>
                    <tr> 
                <tr>
                        <td align='center'><?php echo $senderName['Account']['company_name']; ?></td>
                        <td align='center'><?php echo $receiverName['Account']['company_name']; ?></td>
                        <td align='center'><?php echo $this->Form->Html->link($invoices['Invoice']['id'],
                                    array('controller' => 'Invoices','action'=>'viewinvoice',$invoices['Invoice']['id'])); ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php endforeach; ?>
                <?php endforeach; ?>

以防万一这是我的相关功能

        $accounts2=$this->User->find('list', array(
        'fields'=>array('account_id'),
        'conditions' => array(
        'id' => $this->Auth->user('id'))));


        $invoices=$this->Invoice->find('all', array(
        'conditions' => array(
        'Invoice.receiver_id' => $accounts2)));

        $sender=$this->Invoice->Find('list', array('fields'=>('sender_id')));
        $receiver=$this->Invoice->Find('list', array('fields'=>('receiver_id')));

        $senderName=$this->Account->Find('all', array(
        'conditions' => array(
        'id'=>array_values($sender))));

        $receiverName=$this->Account->find('all', array(
        'conditions' => array(
        'id'=>array_values($receiver))));
debug($senderName);

        $this->set('senderName', $senderName);
        $this->set('accounts2', $accounts2); 
        $this->set('receiverName', $receiverName); 
        $this->set('sender',$sender);
        $this->set('receiver',$receiver);
        $this->set('invoice', $invoices);

}

2 个答案:

答案 0 :(得分:3)

你的电脑是对的。 :)

$senderName['Account']['company_name']

不存在。

$senderName['0']['Account']['company_name']

确实

数据采用这种格式,因为它们可能是几个喜欢senderName的帐户。

编辑:

你能否在你的模特中给出这种关系?

答案 1 :(得分:1)

我认为您应该在视图中查看您的代码:

<?php foreach($senderName as $senderName):?>
<?php foreach($receiverName as $receiverName):?>
foreach中的

array_expression和value变量应该不同,但是你使用了相同的变量名,请为此使用不同的名称。