PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] Model Table CakePHP 3 using existing table

 February 22, 2022     cakephp, cakephp-3.0, model     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing