Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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
给定一个带有顺序和限制的简单select查询,用CakePHP编写find()语句的正确方法是什么?_Php_Cakephp - Fatal编程技术网

给定一个带有顺序和限制的简单select查询,用CakePHP编写find()语句的正确方法是什么?

给定一个带有顺序和限制的简单select查询,用CakePHP编写find()语句的正确方法是什么?,php,cakephp,Php,Cakephp,如何在cakephp中使用find语句编写此查询 $this->Form->query("Select id from forms order by id DESC LIMIT 1") 查看文档:查看文档:这应该可以: $this->Form->find('all', array( 'fields' => array('Form.id'), 'order' => 'Form.id DESC', 'limit' => 1 ));

如何在cakephp中使用find语句编写此查询

$this->Form->query("Select id from forms order by id DESC LIMIT 1")

查看文档:

查看文档:

这应该可以:

$this->Form->find('all', array(
    'fields' => array('Form.id'),
    'order' => 'Form.id DESC',
    'limit' => 1
));
这应该做到:

$this->Form->find('all', array(
    'fields' => array('Form.id'),
    'order' => 'Form.id DESC',
    'limit' => 1
));

我同意这里的第一个回答, 我想提的两件事 我假设您有一个名为Form的模型,现在Cakephp有自己的FormHelper,有时可能会出现名称冲突,我在项目中创建FileController时遇到了这个错误, 您还将得到一个数组,它的形式如下


$result['Form] => array(
                   [id] => 1
                   [name] => whatever)


诸如此类,你可以很容易地使用它来获取你想要的任何数据。

我同意这里的第一个回答, 我想提的两件事 我假设您有一个名为Form的模型,现在Cakephp有自己的FormHelper,有时可能会出现名称冲突,我在项目中创建FileController时遇到了这个错误, 您还将得到一个数组,它的形式如下


$result['Form] => array(
                   [id] => 1
                   [name] => whatever)

依此类推,您可以轻松地使用它来获取所需的任何数据。

虽然这应该是find('first'),但真正的问题可能更简单。我只能假设执行此类查询的唯一原因是获取最后一个插入的id。在与保存相同的“运行”中,您可以使用相同的名称获取模型属性中插入的id:

$this->ModelName->create($this->data);
if ($this->ModelName->save()) {
  $this->Session->setFlash('Saved it!');

  $id_of_new_row = $this->ModelName->id;
  $this->redirect(array('action' => 'view',$id_of_new_row));
}
虽然这应该是find(“first”),但真正的问题可能更简单。我只能假设执行此类查询的唯一原因是获取最后一个插入的id。在与保存相同的“运行”中,您可以使用相同的名称获取模型属性中插入的id:

$this->ModelName->create($this->data);
if ($this->ModelName->save()) {
  $this->Session->setFlash('Saved it!');

  $id_of_new_row = $this->ModelName->id;
  $this->redirect(array('action' => 'view',$id_of_new_row));
}
如上所述,但在查询中添加了“递归”:

$this->Form->find('all', array(
    'fields' => array('Form.id'), // just return the id, thank you
    'order' => 'Form.id DESC',    // sort the query result by id DESC
    'limit' => 1,                 // gimme the top id
    'recursive' => -1,            // don't scan associated models in the query
));
但我也会使用

$this->Form->find('first', array(
     'fields' => array('Form.id'),
     'order' => array('Form.id DESC'),
     'recursive' => -1,
     )
);
这不是很短,但更能表达你想要的

我建议你小心点,因为已经有了表单助手,可能会出现混乱。另一方面,通常在视图中使用$form变量,而这是控制器代码。

如前所述,但在查询中添加“递归”:

$this->Form->find('all', array(
    'fields' => array('Form.id'), // just return the id, thank you
    'order' => 'Form.id DESC',    // sort the query result by id DESC
    'limit' => 1,                 // gimme the top id
    'recursive' => -1,            // don't scan associated models in the query
));
但我也会使用

$this->Form->find('first', array(
     'fields' => array('Form.id'),
     'order' => array('Form.id DESC'),
     'recursive' => -1,
     )
);
这不是很短,但更能表达你想要的


我建议你小心点,因为已经有了表单助手,可能会出现混乱。另一方面,通常在视图中使用$form变量,而这是控制器代码。

我得到的值是“Array”Angeline,您应该a)多阅读一些文档,b)尝试打印($variable);看看数组中有什么。为什么你说的“全部”是限制1?当“first”这样做时,find('all')和find('first')返回不同的数据结构。从技术上讲,“first”也会起作用。@Alexander:老实说,我已经有一段时间没有和CakePHP打交道了(1.1),所以我不确定“first”是否暗示了限制1。我最初的回答使用了它,但我认为安全比抱歉好,所以我将其更改为带有明确限制的“all”。好吧,我得到的值为“Array”Angeline,您应该a)多阅读一些文档,b)尝试打印($variable);看看数组中有什么。为什么你说的“全部”是限制1?当“first”这样做时,find('all')和find('first')返回不同的数据结构。从技术上讲,“first”也会起作用。@Alexander:老实说,我已经有一段时间没有和CakePHP打交道了(1.1),所以我不确定“first”是否暗示了限制1。我最初的回答使用了它,但我认为安全总比抱歉好,所以我把它改成了带有明确限制的“all”。你在['Form]之后缺少一个'you'missing a'在['Form]之后