Search receiver ID by the USER ID first and locate the data by the receiver ID
I am using cakePHP 1.26. I got a simple Table with some data in my localhost Datab开发者_StackOverflow社区ase:
user_id | message | receiver_id
-----------------------------
1 | helloworld | 12
2 | hi there | 12
Let say the system now received the $userid from a user.
The user only got the user ID. He wants to search all the message received by a specific Receiver Here is my code for the case mentioned above:$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);
The code isn't neat and looks clumsy.
I am not sure if there is any best way to do it in this case.Please advise.
I mean, you could do:
$data = $this->User->findByreceiver_id( $this->User->read( 'receiver_id', $userid ) );
This assumes that your code above works as expected.
Or you could hide some of that syntax in the model. Write a Model::find_by_receiver( $user_id ) and it would contain the actual find call, and return it. Seems a little overkill on the wrappers, though. But it will make your controller appear more neat.
This example from the cookbook may help.
精彩评论