2015-12-10 99 views
-1

我試圖用read()函數讀取我的表中的一行,這給了我一個錯誤。我試着使用find('all'),但同樣的事情發生:CAKEPHP:錯誤:SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法中有錯誤;

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

型號Article.php

class Article extends AppModel { 

     function index() { 

     } 
    } 

控制器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; 
    } 

    } 

任何人可以幫助我嗎?

+1

該代碼幾乎沒用。顯示你實際正在嘗試使用分貝的位... –

+0

該控制器應該擴展'Controller' ** NOT **'AppModel'。此外,您所顯示的代碼都不是與錯誤相關的。代碼在哪裏? – Ohgodwhy

+0

對不起,我把錯誤的代碼。現在請看! @Ohgodwhy –

回答

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行動將檢索來自articlesid=1

相關問題