Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cakephp只返回一条使用findby方法的记录_Php_Cakephp - Fatal编程技术网

Cakephp只返回一条使用findby方法的记录

Cakephp只返回一条使用findby方法的记录,php,cakephp,Php,Cakephp,在Cakephp中,有没有一种方法可以使用findby方法只返回一个字段? 到目前为止我已经知道了 $user = $this->User->findbyUsername($username, array('User.id')); 但它似乎会返回所有相关的结果……试试看 $this->User->recursive = -1; // If you are using the Containable behavior: $this->User->contai

在Cakephp中,有没有一种方法可以使用findby方法只返回一个字段? 到目前为止我已经知道了

$user = $this->User->findbyUsername($username, array('User.id'));
但它似乎会返回所有相关的结果……

试试看

$this->User->recursive = -1;

// If you are using the Containable behavior:
$this->User->contain();

$user = $this->User->findByUsername($username, array('fields' => 'User.id'));

你应该把$recursive放在你的应用程序模型中。我想@mark想说的是,在你的应用程序模型中把recursive设置为-1是一种常见的做法,所以除非另有规定,否则它是所有查询的默认值。不过,道格的表现完全不错谢谢你把我的话说得有点太短了。当然,道格的回答本身是正确的。