CakePHP 3.0 Where to Post Requests

I'm trying to use the new CakePHP 3.0, and I have a little problem when I figure out where to put the requests.

Lets say that we have something like this, right from their documentation.

$articles = $this->Articles->find('all', [
    'fields' => ['id', 'title'],
    'conditions' => [
        'OR' => ['title' => 'Cake', 'author_id' => 1],
        'published' => true
    ],
    'contain' => ['Authors'],
    'order' => ['title' => 'DESC'],
    'limit' => 10,
]);

Where can I put this code? In my controller class or in the model folder.

If I need to put this code in my controller class, and in the future I would like to reuse this request. Do I need to rewrite the request in another controller?

If in the model folder, in which folder do I insert it? Behavior, entity or table? and how will i use it?

thank

+4
source share
3 answers

, . , () ( DRY)

$this->loadModel('Model'); //if needed
$this->Model->nameOfYourMethod();
+2

, , , $this->Articles (, ArticlesController). Cake , .

, . : -

class ArticlesTable extends Table
{
    public function getAllArticles() {
        return $this->find('all', [
            'fields' => ['id', 'title'],
            'conditions' => [
                'OR' => ['title' => 'Cake', 'author_id' => 1],
                'published' => true
            ],
            'contain' => ['Authors'],
            'order' => ['title' => 'DESC'],
            'limit' => 10,
        ]);
    }
}

, , , - .

ArticlesController : -

$articles = $this->Articles->getAllArticles();

, . : -

$this->loadModel('Articles');
$articles = $this->Articles->getAllArticles();
+2

/ * You can call this request in your controller, but make sure that your database should have a table of articles, and the table of articles and articles of authors belongs to the table of authors. table article belongs to authoring means in your article, table must have author_id. In fact, this table connects the table of articles with the table of authors and gets the result of both tables in accordance with the conditions of or, and sets and sets the limit and order * /

0
source

All Articles