Reputation: 15
In cakephp, I executed this query to find the id field value of the corresponding username
$count = $this->User->find('all',array('conditions'=>array('User.username' => $n)))
How I will get take the value of a field from this array result to a variable?
I am new to cakephp and any help will be very useful.
Upvotes: 0
Views: 1145
Reputation: 3879
Well, after calling find, you will notice that $count will be populated if something was brought from the database. I would change something, though, I would use "first" instead of "all" because you are finding only one record.
You can either use this
//in your controller
$count = $this->User->find('first',
array('conditions'=>array('User.username' => $n)));
//and set the variable to be used in the view in this way
$this->set('yourId', $count['User']['id']);
Then in your view
echo $yourId;
Or, you can also do this
$yourId = $this->User->field('id', array('User.username' => $n));
$this->set(compact('yourId'));
and then in your view
echo $yourId
Upvotes: 4