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"
cakephp-3.0
add a comment |
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"
cakephp-3.0
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
add a comment |
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"
cakephp-3.0
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
cakephp-3.0
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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