Issue
Hi need your help to understand some feature in CakePHP.
I have a SQL Table : user. I generate with bake the Model : UserTable.
In the action home() of my UsersController, i have this :
$t_Results = TableRegistry::get('User')->findByLogin('jdupont')->execute()->fetchAll('assoc');
debug($t_Results);
The query is generated by Cake and this code works well.
My question are :
- Must i create the function findByLogin inside the Model or not ?
- Is my code correct ?
Thanks for the help ;)
Solution
Yes you can create a findByLogin in your model but you don't have to.
Your code works but doesn't respect conventions. In CakePHP 3 SQL tables are singular lowercase, Table files has upper first letter and plural suffixed by Table, Controllers are plural first letter upper and suffixed by Controller.
If you follow these conventions in your controller you can do this:
$t_Results = $this->Users->findByLogin('jdupont')->execute()->fetchAll('assoc');
debug($t_Results);
You don't have to use ->execute(). Query objects are lazily evaluated, execute will be called when you will use the request.
Answered By - Tartempion34
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.