我忙於CakePHP中的發票項目。我已經在模型中設置了相關的表格,但不知何故它不會轉發到視圖。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] =>
但是,如果我改變模型中的外鍵,我得到一個錯誤,列不存在。
這裏怎麼回事?
預先感謝您。
控制器在哪裏? –
這些關係是錯誤的。嘗試將模型與**條件**相鏈接,並將「foreignKey」設置爲false。你使用的是什麼版本的Cake?除非你特別使用他們推薦的數據庫模式(對於外鍵名稱),否則舊版本對於鏈接模型是很可怕的 –
我已經添加了控制器。 我正在使用2.4.3。 – Vincent