views:

25

answers:

2

Hi all,

I am using cakePHP 1.26. I got a simple Table with some data in my localhost Database:

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.

+1  A: 

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.

Travis Leleu
+1  A: 

This example from the cookbook may help.

SpawnCxy