2013-11-20 66 views
1

我想在CakePHP中構建一個開票系統。到目前爲止這麼好,但我想在CakePHP中構建一個函數來正確顯示客戶端名稱。在早先的版本(只是PHP)我builed這個(User類):正確顯示名稱的函數

function get_display_name(){ 
    if($this->company){ 
     return $this->company; 
    } else { 
     return $this->frontname . ' ' . $this->lastname; 
    } 
} 

但我不能讓它在CakePHP的工作。或者我應該在視圖中執行此操作嗎?

編輯

我已經在我加入這個CakePHP中的助手文件夾做了一個CustomerHelper:

class CustomerHelper extends AppHelper { 
    function get_display_name(){ 
     if($this->Customer->find('company')){ 
      return $this->Customer->find('company'); 
     } else { 
      return $this->Customer->find('frontname') . ' ' . $this->Customer->find('lastname'); 
     } 
    } 
} 

但我現在得到一個致命的錯誤。 「錯誤:調用一個成員函數查找()一個非對象」

+0

它爲什麼不起作用?某種錯誤或不適合? –

+0

你應該發佈你的整個'用戶'類... – noslone

+0

這聽起來像2.x的壞方法,其中模型不是直接保存數據的對象。更好地使用find()和helper/element來正確地格式化你的輸出。但是你需要傳入你的客戶數據數組(而不是試圖從你的幫手進行模型查詢!)。 – mark

回答

0

傳遞到自己的數據:

class CustomerHelper extends AppHelper { 
    public function getDisplayName($customer) { 
     ///... 
     return $result; 
    } 
} 

另外請注意,我糾正它滿足CakePHP的編碼標準。

+0

工作正常!謝謝! 我希望有一個更好的方法來做到這一點,但我很好! – Vincent