CakePHP - 未显示相关值

时间:2013-12-23 09:15:34

标签: php cakephp cakephp-model

我正在忙着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>

这是我的控制器:

<?php
class InvoicesController extends AppController {

    public $helpers = array('Status');

    public function index() {
        $uid = $this->Auth->user('cloud_id');
        $this->set('uid', $uid);

        $allInvoices = $this->Invoice->find('all', array(
            'conditions' => array(
                    'Invoice.cloud_id' => $this->Auth->user('cloud_id'),
                )
            )
        );

        $this->set('invoices', $allInvoices);
    }

如果我打印发票,我可以看到相关字段,但值为空。像这样:

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] => 

但是如果我更改了模型中的外键,我会收到一个错误,即该列不存在。

这里出了什么问题?

提前谢谢。

2 个答案:

答案 0 :(得分:0)

而不是HasOne关联尝试使用BelongsTo

class Invoice extends AppModel {

    public $belongsTo  = array(
        'Customer' => array(
            'className'    => 'Customer',
            'foreignKey'   => 'customer_id'
        ),
    );

答案 1 :(得分:0)

不要像上面的代码那样绑定表格!这些关系不正确,可能难以理解。

这是一个解决方案

步骤1:在Invoice模型中创建一个函数,让我们说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步:执行你的代码,它将产生数据。

希望它能解决你的问题。