2010-07-13 27 views
0

我正在使用cakePHP 1.26。 我有一個簡單的表,在我的本地數據庫的一些數據:首先通過USER ID搜索接收者ID並通過接收者ID查找數據

user_id | message | receiver_id 
----------------------------- 
1  | helloworld | 12 
2  | hi there | 12 

讓我們說現在這一體系從用戶接收到的用戶ID $。
用戶只有用戶標識。他想通過搜索特定接收
這裏收到的所有消息上面提到我的情況下代碼:

$receiverID=$this->user->read('receiver_id', $userid); // find the receiver ID 
$data=$this->user->findByreceiver_id($receiverID); //then retrieve data by the receiver ID 
debug($data); 

的代碼是不是整齊,看起來笨拙。
我不確定在這種情況下是否有最佳方法來完成。

請指教。

+0

這是否返回您期望的結果集? – 2010-07-13 17:13:19

+0

@Jason。對不起,我在代碼中犯了一個錯誤 – user327712 2010-07-14 07:36:44

回答

1

我的意思是,你可以這樣做:

$data = $this->User->findByreceiver_id($this->User->read('receiver_id', $userid)); 

這是假設你的代碼上面按預期工作。

或者你可以隱藏模型中的一些語法。編寫Model :: find_by_receiver($ user_id),它將包含實際的查找調用,並返回它。雖然,看起來在包裝上有點矯枉過正。但它會讓你的控制器看起來更整齊一些