How to Add to Table from Another Controller Cakephp 3









up vote
0
down vote

favorite












I'm new to cakephp3. I would like to know if it is possible to add a new entry form controller1 to table2.



It is a login form. So I would like to authenticate first whether the user is registered, then after authenticating, I would like to save the details to another table.



class UsersController extends AppController
{

public function login()

$user = $this->Users->newEntity();
if($this->request->is(['post']))

$user = $this->Auth->identify();
if($user)

$logs = TableRegistry::get('AttendsTable');
$log->username = 'lorem ipsum';
$log->datenow = '2018-11-10';
$log->tin = '12:42:00';
$log->tout ='12:42:00';
$logs->save($log);
$this->Auth->setUser($user);
$this->redirect(['action'=>'index']);





I am getting an error:




"Argument 1 passed to CakeORMTable::save() must implement interface CakeDatasourceEntityInterface, instance of stdClass given, called in C:xampphtdocsTimeStampssrcControllerUsersController.php on line 32"











share|improve this question























  • stackoverflow.com/questions/37030044/…
    – Alex
    Nov 10 at 3:33










  • Possible duplicate of Cakephp3 : using another model in a controller
    – Saeed Zhiany
    Nov 10 at 5:11










  • I have solved this with the help of the documentation from book.cakephp.org/3.0/en/orm/saving-data.html
    – TakeAcake
    Nov 10 at 9:29














up vote
0
down vote

favorite












I'm new to cakephp3. I would like to know if it is possible to add a new entry form controller1 to table2.



It is a login form. So I would like to authenticate first whether the user is registered, then after authenticating, I would like to save the details to another table.



class UsersController extends AppController
{

public function login()

$user = $this->Users->newEntity();
if($this->request->is(['post']))

$user = $this->Auth->identify();
if($user)

$logs = TableRegistry::get('AttendsTable');
$log->username = 'lorem ipsum';
$log->datenow = '2018-11-10';
$log->tin = '12:42:00';
$log->tout ='12:42:00';
$logs->save($log);
$this->Auth->setUser($user);
$this->redirect(['action'=>'index']);





I am getting an error:




"Argument 1 passed to CakeORMTable::save() must implement interface CakeDatasourceEntityInterface, instance of stdClass given, called in C:xampphtdocsTimeStampssrcControllerUsersController.php on line 32"











share|improve this question























  • stackoverflow.com/questions/37030044/…
    – Alex
    Nov 10 at 3:33










  • Possible duplicate of Cakephp3 : using another model in a controller
    – Saeed Zhiany
    Nov 10 at 5:11










  • I have solved this with the help of the documentation from book.cakephp.org/3.0/en/orm/saving-data.html
    – TakeAcake
    Nov 10 at 9:29












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm new to cakephp3. I would like to know if it is possible to add a new entry form controller1 to table2.



It is a login form. So I would like to authenticate first whether the user is registered, then after authenticating, I would like to save the details to another table.



class UsersController extends AppController
{

public function login()

$user = $this->Users->newEntity();
if($this->request->is(['post']))

$user = $this->Auth->identify();
if($user)

$logs = TableRegistry::get('AttendsTable');
$log->username = 'lorem ipsum';
$log->datenow = '2018-11-10';
$log->tin = '12:42:00';
$log->tout ='12:42:00';
$logs->save($log);
$this->Auth->setUser($user);
$this->redirect(['action'=>'index']);





I am getting an error:




"Argument 1 passed to CakeORMTable::save() must implement interface CakeDatasourceEntityInterface, instance of stdClass given, called in C:xampphtdocsTimeStampssrcControllerUsersController.php on line 32"











share|improve this question















I'm new to cakephp3. I would like to know if it is possible to add a new entry form controller1 to table2.



It is a login form. So I would like to authenticate first whether the user is registered, then after authenticating, I would like to save the details to another table.



class UsersController extends AppController
{

public function login()

$user = $this->Users->newEntity();
if($this->request->is(['post']))

$user = $this->Auth->identify();
if($user)

$logs = TableRegistry::get('AttendsTable');
$log->username = 'lorem ipsum';
$log->datenow = '2018-11-10';
$log->tin = '12:42:00';
$log->tout ='12:42:00';
$logs->save($log);
$this->Auth->setUser($user);
$this->redirect(['action'=>'index']);





I am getting an error:




"Argument 1 passed to CakeORMTable::save() must implement interface CakeDatasourceEntityInterface, instance of stdClass given, called in C:xampphtdocsTimeStampssrcControllerUsersController.php on line 32"








cakephp-3.0






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 20:41









Roberto

17610




17610










asked Nov 10 at 3:31









TakeAcake

32




32











  • stackoverflow.com/questions/37030044/…
    – Alex
    Nov 10 at 3:33










  • Possible duplicate of Cakephp3 : using another model in a controller
    – Saeed Zhiany
    Nov 10 at 5:11










  • I have solved this with the help of the documentation from book.cakephp.org/3.0/en/orm/saving-data.html
    – TakeAcake
    Nov 10 at 9:29
















  • stackoverflow.com/questions/37030044/…
    – Alex
    Nov 10 at 3:33










  • Possible duplicate of Cakephp3 : using another model in a controller
    – Saeed Zhiany
    Nov 10 at 5:11










  • I have solved this with the help of the documentation from book.cakephp.org/3.0/en/orm/saving-data.html
    – TakeAcake
    Nov 10 at 9:29















stackoverflow.com/questions/37030044/…
– Alex
Nov 10 at 3:33




stackoverflow.com/questions/37030044/…
– Alex
Nov 10 at 3:33












Possible duplicate of Cakephp3 : using another model in a controller
– Saeed Zhiany
Nov 10 at 5:11




Possible duplicate of Cakephp3 : using another model in a controller
– Saeed Zhiany
Nov 10 at 5:11












I have solved this with the help of the documentation from book.cakephp.org/3.0/en/orm/saving-data.html
– TakeAcake
Nov 10 at 9:29




I have solved this with the help of the documentation from book.cakephp.org/3.0/en/orm/saving-data.html
– TakeAcake
Nov 10 at 9:29












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Your code isn't working because the Model->save() function expects an entity. In your snippet $log is not defined first, so PHP just makes it a stdClass (gotta love high-level programming :p).



Something like this should work:



$logs = TableRegistry::get('AttendsTable');
$log = $logs->newEntity([
'username' => 'lorem ipsum',
'datenow' => '2018-11-10',
// ...
]);

$logs->save($log);


Moreover, I've heard say it's better to use $this->loadModel() instead of TableRegistry::get():



$this->loadModel('ExternalModel');

// Now available:
$this->ExternalModel->//...


  • Cookbook > Controllers > Loading Additional Models





share|improve this answer






















  • Up for this. I used the loadModel(). Big thanks! Also to the documentation. I guess I just really need to read all the contents of the documentation before asking a question. :D
    – TakeAcake
    Nov 13 at 12:49










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235784%2fhow-to-add-to-table-from-another-controller-cakephp-3%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Your code isn't working because the Model->save() function expects an entity. In your snippet $log is not defined first, so PHP just makes it a stdClass (gotta love high-level programming :p).



Something like this should work:



$logs = TableRegistry::get('AttendsTable');
$log = $logs->newEntity([
'username' => 'lorem ipsum',
'datenow' => '2018-11-10',
// ...
]);

$logs->save($log);


Moreover, I've heard say it's better to use $this->loadModel() instead of TableRegistry::get():



$this->loadModel('ExternalModel');

// Now available:
$this->ExternalModel->//...


  • Cookbook > Controllers > Loading Additional Models





share|improve this answer






















  • Up for this. I used the loadModel(). Big thanks! Also to the documentation. I guess I just really need to read all the contents of the documentation before asking a question. :D
    – TakeAcake
    Nov 13 at 12:49














up vote
0
down vote



accepted










Your code isn't working because the Model->save() function expects an entity. In your snippet $log is not defined first, so PHP just makes it a stdClass (gotta love high-level programming :p).



Something like this should work:



$logs = TableRegistry::get('AttendsTable');
$log = $logs->newEntity([
'username' => 'lorem ipsum',
'datenow' => '2018-11-10',
// ...
]);

$logs->save($log);


Moreover, I've heard say it's better to use $this->loadModel() instead of TableRegistry::get():



$this->loadModel('ExternalModel');

// Now available:
$this->ExternalModel->//...


  • Cookbook > Controllers > Loading Additional Models





share|improve this answer






















  • Up for this. I used the loadModel(). Big thanks! Also to the documentation. I guess I just really need to read all the contents of the documentation before asking a question. :D
    – TakeAcake
    Nov 13 at 12:49












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Your code isn't working because the Model->save() function expects an entity. In your snippet $log is not defined first, so PHP just makes it a stdClass (gotta love high-level programming :p).



Something like this should work:



$logs = TableRegistry::get('AttendsTable');
$log = $logs->newEntity([
'username' => 'lorem ipsum',
'datenow' => '2018-11-10',
// ...
]);

$logs->save($log);


Moreover, I've heard say it's better to use $this->loadModel() instead of TableRegistry::get():



$this->loadModel('ExternalModel');

// Now available:
$this->ExternalModel->//...


  • Cookbook > Controllers > Loading Additional Models





share|improve this answer














Your code isn't working because the Model->save() function expects an entity. In your snippet $log is not defined first, so PHP just makes it a stdClass (gotta love high-level programming :p).



Something like this should work:



$logs = TableRegistry::get('AttendsTable');
$log = $logs->newEntity([
'username' => 'lorem ipsum',
'datenow' => '2018-11-10',
// ...
]);

$logs->save($log);


Moreover, I've heard say it's better to use $this->loadModel() instead of TableRegistry::get():



$this->loadModel('ExternalModel');

// Now available:
$this->ExternalModel->//...


  • Cookbook > Controllers > Loading Additional Models






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 13:17

























answered Nov 12 at 14:04









Roberto

17610




17610











  • Up for this. I used the loadModel(). Big thanks! Also to the documentation. I guess I just really need to read all the contents of the documentation before asking a question. :D
    – TakeAcake
    Nov 13 at 12:49
















  • Up for this. I used the loadModel(). Big thanks! Also to the documentation. I guess I just really need to read all the contents of the documentation before asking a question. :D
    – TakeAcake
    Nov 13 at 12:49















Up for this. I used the loadModel(). Big thanks! Also to the documentation. I guess I just really need to read all the contents of the documentation before asking a question. :D
– TakeAcake
Nov 13 at 12:49




Up for this. I used the loadModel(). Big thanks! Also to the documentation. I guess I just really need to read all the contents of the documentation before asking a question. :D
– TakeAcake
Nov 13 at 12:49

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235784%2fhow-to-add-to-table-from-another-controller-cakephp-3%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo