tags:

views:

63

answers:

3

Hi all,

In my sample database table, I have a record with post id 999. I got a few lines of code to retrieve the record of post id 999:

function viewing($sid){    
$this->Testing->post_id=$sid;
$this->set('posts', $this->Testing->read());
}

But no record can be record retrieved from the post with id 999 when I altered the code:

function viewing($sid){ 
$sid=999;   
$this->Testing->post_id=$sid;
$this->set('posts', $this->Testing->read());
}

Could you tell help me please?

+3  A: 

You want to set the model::id attribute, not post_id.

$this->Site1->id=$sid;

Travis Leleu
+2  A: 

I think you messing the terms and functions. First you set id of the Site1 model. Then you trying to read record from the Testing model and setting post_id in the record. The proper code should look like:

function viewing($sid){ 
  $sid=999;   
  $this->Post->id=$sid;
  $this->set('post', $this->Post->read());
}

Alternatively and more short it will be:

function viewing($sid){ 
  $sid=999;   
  $this->set('post', $this->Post->read(null, $sid));
}

But assuming that you want to get posts probably the actual code should look like:

function viewing($sid){ 
  $sid=999;   
  $this->set('post', $this->Posts->fund('all', array('conditions'=>array('post_id'=>$sid))));
}

Or something similar. I think first you should take a look on the CakePHP Cookbook and on the CakePHP API

Nik
Your answer is exactly what I want.Thank you, Nik.
A: 

Better to use the form $this->Post->read(null, $sid)

If you want to control retrieval of associated tables, use $this->MyModel->contain()

E.g.

$this->Post->contain('Testing') //(Post & Testing but not Site1)

or

$this->Post->contain('Testing','Site1.someValue') //(Post & Testing and Site1.someValue)

I think you should also carefully reread the manual sections on http://book.cakephp.org/view/78/Associations-Linking-Models-Together & http://book.cakephp.org/view/73/Retrieving-Your-Data

Leo