tags:

views:

34

answers:

2

Hi all,

In a Controller, I got this:

 $this->Site1->post_id=$id;
 $this->set('posts', $this->Site1->read()); 

And when I replaced it by this line of code: $this->set('posts', $this->Site1->read('post_id', $id));

But the returned result is totally different.

Is there any difference between them?

Is it possible to make this two lines of code neat by re-writing it into one line of code?

 $this->Site1->post_id=$id;
 $this->set('posts', $this->Site1->read()); 
+2  A: 

The result is totally different because you told the read() method to fetch only the post_id column. That's what happens when you pass a string as the first argument to read(). You can also pass an array of columns as the first argument, or null to fetch all columns. The second argument is the ID of the record you want to retrieve. See the documentation for examples.

Is it possible to make this two lines of code neat by re-writing it into one line of code?

If you want to fit it all on one line, try: $this->set('posts', $this->Site1->read(null, $id);

Mike
Thanks again for the help, Mike.
+1  A: 

As another option, you can also simply use findBy

$this->set('posts', $this->Site1->findByPostId($id)); 
SpawnCxy