开发者

How to use Read()

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));

B开发者_Python百科ut 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 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);


As another option, you can also simply use findBy

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜