Question

I'm busy with an invoice project in CakePHP. I've set my related tables in the models, but somehow it's not forwarding to the view.

These are my models:

Invoice Model

class Invoice extends AppModel {

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

    ...

Project Model

class Project extends AppModel {

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

    public $hasMany = array(    
        'Invoice' => array(
            'className'    => 'Invoice',
            'foreignKey'   => 'project_id'
        ),
    );

Customer Model

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

And the view:

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

        <td><?php echo $invoice['Customer']['display_name']; ?></td>    
        <td><?php echo $invoice['Project']['project_name']; ?></td>

Here is my controller:

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

If I print an Invoice, I can see the related fields, but the values are empty. Like so:

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

But if I change the foreign keys in the models, I get an error that the column doesn't exists.

What is going wrong here?

Thank you in advance.

Was it helpful?

Solution

Instead of HasOne association try to use BelongsTo,

class Invoice extends AppModel {

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

OTHER TIPS

Do not bind your tables as you are doing in the above code! These relationships are not correct and could be difficult to understand.

Here is one solution

Step 1: create one function in Invoice model, lets say getData()

Step 2: inside that function write below code with small edits like in fields array...

$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);

Step 3: execute your code, it will result data.

Hope it will solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top