-1

I'm trying to read a row of my table using read() function and this gives me an error. I've tried to use find('all') but the same thing happens:

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;
    }

  }

Can anybody help me please?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402

1 Answers1

0

The line

    $id = $this->request->params['id'];

is incorrect. However

    $id = $this->request->params['named']['id'];

will work if your calls look like /articles/view/id:1.

In CakePHP it's easier to use Passed Arguments. The parameters are extracted from the URL and passed on to the action. In your case:

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            
    }

}

By calling /articles/view/1, the view action will retrieve id=1 from the articles table.

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36