Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which approach is best to get instance of a Table in cakephp 3.0

Tags:

cakephp-3.0

I am new to Cakephp and started with cake3.

A question arises on me is that which approach is the best from the performance sense to get an instance of a table in CakePHP-3.0 which is not a controller's default one between loadModel or TableRegistry.

Such as:

1. $this->loadModel('Articles');
OR
2. TableRegistry::get('Articles');

I read Cake-Doc for loadModel and TableRegistry. Confusion comes from here

Thanks in advance

like image 376
Bayezid Alam Avatar asked Apr 11 '15 14:04

Bayezid Alam


1 Answers

When in a controller, it is better to use loadModel(). The reason is that it is easier to mock when doing unit testing.

It also sets the $this->[MyTable] variable according to the table that has be loaded.

like image 105
José Lorenzo Rodríguez Avatar answered Oct 21 '22 17:10

José Lorenzo Rodríguez