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.