2014-02-10 29 views
0

默認情況下,cakephp會在find()沒有找到任何東西時返回空數組。 但如何將其設置爲顯示爲空白數組。Cakephp當沒有匹配的結果時,如何設置find()返回空數組而不是空數組

例如:

$customer = $this->Transaction->Customer->find(--conditions to return 0 result.--) 

我想讓它顯示爲空白陣列這樣。

array('Customer' => array('customer_id'=>null, 'name'=>null, 'lastname'=>null)) 

不僅僅是一個像

array()null

因爲我總是得到了在視圖中顯示錯誤$customer['Customer']['name']是不確定的指數。而且我不想每次都使用isset()is_null()進行檢查。

回答

0

如果你真的想/需要做到這一點,你可以使用類似:

$default = array('Customer' => array('customer_id' => null, 'name'=>null, 'lastname' => null)); 
$customer = $this->Transaction->Customer->find(...) 
$customer = array_merge($default, $customer); 

這樣一來,如果結果是空的,它會使用默認值。

但是,這不是一個好的做法,因爲您最終可能會在頁面中顯示"Welcome, NULL"。您應該在您的視圖中使用if (!empty($customer)) ...

此外,在本例中,您是否使用find->('first')

+0

是的,我使用查找 - >( '第一')。而找到 - >( '全部') – user447172

+0

就是我的回答對你合適需要?如果沒有,你可以粘貼你在視圖中使用的代碼嗎? – cornelb

+0

對於我的問題,您的回答是可以的,每次更新客戶表時,默認數組必須更改。我會盡力找到解決這個問題的方法,併合並數組,謝謝。 – user447172

相關問題