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());
The result is totally different because you told the
read()method to fetch only thepost_idcolumn. That’s what happens when you pass a string as the first argument toread(). You can also pass an array of columns as the first argument, ornullto fetch all columns. The second argument is the ID of the record you want to retrieve. See the documentation for examples.If you want to fit it all on one line, try:
$this->set('posts', $this->Site1->read(null, $id);