tiepvut
tiepvut

Reputation: 90

How to write these line of code in cakephp 3.x

I am very new in cakephp and i have to upgrade a cake project from version 1.1 to 3.6. I do not know how to convert these lines of code to cakephp 3.6:

    App::import('Model', 'SystemMenu');
    $system_menu =& new SystemMenu();

SystemMenu is an model which was define in Model folder.

Thank you very much for your help.

Upvotes: 0

Views: 56

Answers (2)

Dariusz Majchrzak
Dariusz Majchrzak

Reputation: 1237

You can use TableRegistry class.

$system_menu = \Cake\ORM\TableRegistry::get('SystemMenu');

//new entity
$entity = $system_menu->newEntity();

//get entity by id 
$entity = $system_menu->get(2);

//Save entity
$system_menu->save($e);

// finder 
$menu = $system_menu->find()->toArray();

Upvotes: 0

Vindur
Vindur

Reputation: 373

If youre within a controller, you can do

$this->loadModel('SystemMenus');

and access the model like so

$this->SystemMenus->find()->...

If not, you can use TableRegistry

$systemMenus = TableRegistry::get('SystemMenus')

And access is simple:

$systemMenus->find()->...

See https://book.cakephp.org/3.0/en/orm/table-objects.html for more information

Notice that i have changed the table name to be plural, as the CakePHP 3.x conventions specifies https://book.cakephp.org/3.0/en/intro/conventions.html

Upvotes: 1

Related Questions