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

Tuesday, February 15, 2022

[FIXED] Difference between saving data in cakephp

 February 15, 2022     cakephp-3.0, php     No comments   

Issue

I can see two different ways of saving data to my database in CakePHP 3.x, one that works and one that doesn't. Other than the obvious, that I can't get one to work, why would you use one rather than the other, and can you tell why one of the ways isn't working for me?

Option 1: (not working)

$usersTable = TableRegistry::get('users');   
$user = $usersTable->newEntity();
$user = $usersTable->patchEntity($user, $this->request->data);
$usersTable->save()

This option gives the error,

Warning (4096): Argument 1 passed to Cake\ORM\Table::save() must implement interface Cake\Datasource\EntityInterface, none given, called in 
Notice (8): Undefined variable: entity [CORE\src\ORM\Table.php, line 1453]

Option 2: Does work

$user = $this->Users->newEntity();
$user = $this->Users->patchEntity($user, $this->request->data);
$this->Users->save($user)

Solution

First one obviously can't work as you are missing argument within save method:

$usersTable->save(); // invalid
$usersTable->save($user); // valid

And if i am saving data from users controller why to use TableRegistry ? I always prefer second one.Using TableRegistry is a bit slower process (may be by some microseconds or less).



Answered By - Manohar Khadka
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

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