我试图使用read()
函数读取我的表格中的一行,这给了我一个错误。我曾尝试使用find('all')
,但同样的事情发生了:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
Model Article.php
class Article extends AppModel {
function index() {
}
}
Controller ArticlesController.php
class ArticlesController extends AppController {
public $uses = array('Article', 'User');
public function view() {
$id = $this->request->params['id'];
$this->Article->recursive = 3;
$this->Article->id = $id;
$articl = $this->Article->read();
echo count($articl);
exit;
}
}
有人可以帮帮我吗?
答案 0 :(得分:0)
该行
$id = $this->request->params['id'];
不正确。然而
$id = $this->request->params['named']['id'];
如果您的通话看起来像/articles/view/id:1
,将会有效。
在CakePHP中,使用Passed Arguments更容易。参数从URL中提取并传递给操作。在你的情况下:
class ArticlesController extends AppController {
public $uses = array('Article', 'User');
public function view($id) {
//$this->Article->recursive = 3; // does not apply, only needed with Model->find
$this->Article->id = $id;
$article = $this->Article->read(); //will return a single row
}
}
通过致电/articles/view/1
,view
操作将从id=1
表中检索articles
。