CakePHP简单检索数据

时间:2013-12-08 13:54:47

标签: sql cakephp find

我无法处理从模型中检索数据。我的表是:

CREATE TABLE grades(
id INT NOT NULL,
value nvarchar(2) NOT NULL
);

INSERT grades(id, value) VALUES
(1,N'A'),
(2,N'F'),
(8,N'B+'),
(10,N'C')

我试过控制器

var $uses = array( 'Grade');

function index(){
    $this->Grade->read();
    $this->set ( 'grade', $this->Grade->data );
    $this->set ( 'res' );
    $res = $this->Grade->find(array(
    'conditions' => array('id' => 2)
            ));

}

并在视野中

<?php debug($res);?> 

返回array()。我需要它像'SELECT * FROM grades WHERE id = 2;'

一样工作

我需要它来返回像〜

这样的东西
2, F

3 个答案:

答案 0 :(得分:3)

function index(){
    $res = $this->Grade->find('first', array(
        'conditions' => array('id' => 2)
    ));
    $this->set('res', $res);
}

我强烈建议你浏览CakePHP Book's Tutorials & Examples。您的代码有很多错误或奇怪的内容,我甚至不会尝试解释这一切,但上面是基于Grade找到id的方法。

答案 1 :(得分:0)

您的find语句看起来不错,但您没有为视图设置它

$this->set ( 'res' );

尝试:

$res = $this->Grade->find(array(
'conditions' => array('id' => 2)
        ));

$this->set('res', $res);

使用该功能进行查找,将结果保存在$res中,然后使用set()将其提供给视图。

答案 2 :(得分:0)

当我擦除行

时,它只是起作用
$this->Grade->read();

它解决了我的问题,没有人知道。