在cakephp中将特定字段的值从数组提取到变量

在cakephp中将特定字段的值从数组提取到变量,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,在cakephp中,我执行此查询以查找相应用户名的id字段值 $count = $this->User->find('all',array('conditions'=>array('User.username' => $n))) 如何将字段的值从此数组结果获取到变量 我是cakephp新手,任何帮助都会非常有用。好吧,在调用find之后,您会注意到,如果从数据库中带来了一些东西,将填充$count。不过,我会更改一些内容,我会使用“first”而不是“all”,因为您只

在cakephp中,我执行此查询以查找相应用户名的id字段值

$count = $this->User->find('all',array('conditions'=>array('User.username' => $n)))
如何将字段的值从此数组结果获取到变量


我是cakephp新手,任何帮助都会非常有用。

好吧,在调用find之后,您会注意到,如果从数据库中带来了一些东西,将填充$count。不过,我会更改一些内容,我会使用“first”而不是“all”,因为您只找到一条记录

你可以用这个

//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']);
那么在你看来,

echo $yourId
echo$yourId

或者,你也可以这样做

$yourId = $this->User->field('id', array('User.username' => $n));
$this->set(compact('yourId'));
然后在你看来

echo $yourId

在调用find之后,您会注意到,如果从数据库中带来了一些内容,那么$count将被填充。不过,我会更改一些内容,我会使用“first”而不是“all”,因为您只找到一条记录

你可以用这个

//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']);
那么在你看来,

echo $yourId
echo$yourId

或者,你也可以这样做

$yourId = $this->User->field('id', array('User.username' => $n));
$this->set(compact('yourId'));
然后在你看来

echo $yourId