0
我正在處理我的第一個CakePHP項目。在我對「Item」的烘焙視圖中,有一個「相關」div顯示「相關項目」。找不到任何相關的(null)相關項目
它檢查「工程」數組爲空:
<div class="related">
<h3><?php echo __('Related Projects'); ?></h3>
<?php if (!empty($item['Project'])): ?>
<dl>
...
看到的「項目」 db表是空的,現在,我認爲這將是空的。不過,我在$item['Project']
得到這個:
["Project"]=> array(7) {
["id"]=> NULL
["item_id"]=> NULL
["title"]=> NULL
...
}
我可以解決此通過檢查ID爲空,但我想避免的變通和做事的方式蛋糕。
控制器:
public function view($id = null) {
if (!$this->Item->exists($id)) {
throw new NotFoundException(__('Invalid item'));
}
$options = array('conditions' => array('Item.' . $this->Item->primaryKey => $id));
$this->set('item', $this->Item->find('first', $options));
}
型號:
public $hasOne = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'item_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
謝謝你提供的信息。除非有匹配,否則有沒有辦法讓它不能填充?將它改爲$ hasMany是否有意義,即使它只有一個或沒有? – smerny
不,這沒有意義。我已經給你答案。整個記錄是一個空數組(如果沒有主記錄)或者您需要使用上述檢查。 – mark