CakePHP相关值未显示

CakePHP相关值未显示,php,cakephp,cakephp-model,Php,Cakephp,Cakephp Model,我正忙于CakePHP中的一个发票项目。我已经在模型中设置了相关的表,但不知何故它没有转发到视图 这些是我的模型: 发票模型 class Invoice extends AppModel { public $hasOne = array( 'Customer' => array( 'className' => 'Customer', 'foreignKey' => 'id' )

我正忙于CakePHP中的一个发票项目。我已经在模型中设置了相关的表,但不知何故它没有转发到视图

这些是我的模型:

发票模型

class Invoice extends AppModel {

    public $hasOne  = array(
        'Customer' => array(
            'className'    => 'Customer',
            'foreignKey'   => 'id'
        ),
        'Project' => array(
            'className'    => 'Project',
            'foreignKey'   => 'id'
        ),      
    );

    ...
项目模型

class Project extends AppModel {

    public $hasOne  = array(
        'Customer' => array(
            'className'    => 'Customer',
            'foreignKey'   => 'id'
        )
    );

    public $hasMany = array(    
        'Invoice' => array(
            'className'    => 'Invoice',
            'foreignKey'   => 'project_id'
        ),
    );
客户模型

class Customer extends AppModel {

    public $virtualFields = array(
        'full_name' => 'CONCAT(Customer.frontname, " ", Customer.lastname)',
        'display_name' => 'IF(Customer.company > " ", Customer.company, CONCAT(Customer.frontname, " ", Customer.lastname))',
    );

    public $hasMany = array(        
        'Project' => array(
            'className' => 'Project',
            'foreignKey' => 'customer_id',
        ),
        'Invoice' => array(
            'className' => 'Invoice',
            'foreignKey' => 'customer_id',
        ),
    );
以及以下观点:

<?php
foreach($invoices as $invoice){
?>

        <td><?php echo $invoice['Customer']['display_name']; ?></td>    
        <td><?php echo $invoice['Project']['project_name']; ?></td>
但是如果我在模型中更改外键,我会得到一个错误,即该列不存在

这里出了什么问题

提前谢谢。

请尝试使用BelongsTo,而不是HasOne协会


不要像在上述代码中那样绑定表!这些关系不正确,可能难以理解

这里有一个解决方案

步骤1:在发票模型中创建一个函数,比如getData

第2步:在该函数中,编写下面的代码,并进行一些小的编辑,如字段数组中的编辑

$options = array(
            'conditions' => array('Invoice.cloud_id' => $this->Auth->user('cloud_id')),
            'joins' => array(
                array(
                    'alias' => 'Customer',  
                    'table' => 'customers',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Customer.id = Invoice.id',
                    ),
                ),
                array(
                    'alias' => 'Project',  
                    'table' => 'projects',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Project.id = Invoice.id',
                    ),
                )
            ),
            'fields' => array('PUT DESIRED FIELDS COMMA SEPERATED')
        );
   $returnData = $this->find('all',$options);
步骤3:执行代码,它将生成数据


希望它能解决您的问题。

控制器在哪里?这些关系是错误的。尝试将模型与条件链接,并将foreignKey设置为false。你用的是什么版本的蛋糕?旧版本对于链接模型来说是可怕的,除非您专门为外键名称使用推荐的DB模式。我已经添加了控制器。我使用的是2.4.3。类似问题的答案是:为什么不检查查询?现在可以了!谢谢你的帮助!
Array
(
    [Invoice] => Array
        (
            [id] => 143
            [cloud_id] => 1
            [invoice_number] => 143
            [customer_id] => 2
            [date] => 2013-08-17
            [period] => 30
            [project_id] => 1
            [status] => 1
            [notes] => 
            [secret] => df56cd45ea7cb086458cc5c3480f77c386647a86
        )

    [Customer] => Array
        (
            [id] => 
            [cloud_id] => 
            [company] => 
            [frontname] => 
            [lastname] => 
            [address] => 
class Invoice extends AppModel {

    public $belongsTo  = array(
        'Customer' => array(
            'className'    => 'Customer',
            'foreignKey'   => 'customer_id'
        ),
    );
$options = array(
            'conditions' => array('Invoice.cloud_id' => $this->Auth->user('cloud_id')),
            'joins' => array(
                array(
                    'alias' => 'Customer',  
                    'table' => 'customers',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Customer.id = Invoice.id',
                    ),
                ),
                array(
                    'alias' => 'Project',  
                    'table' => 'projects',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Project.id = Invoice.id',
                    ),
                )
            ),
            'fields' => array('PUT DESIRED FIELDS COMMA SEPERATED')
        );
   $returnData = $this->find('all',$options);