-1

创建视图时,我得到一个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);

}
4

2 回答 2

3

你的电脑是对的。:)

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

不存在。

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

做。

数据采用这种格式,因为它们可能是多个帐户喜欢发件人名称。

编辑:

你能在你的模型中给出关系吗?

于 2012-08-15T10:29:38.820 回答
1

我认为您应该查看您的代码:

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

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

于 2012-08-15T16:39:02.207 回答