cakephp controller using functions from another controller

Asked

Viewed 582 times

0

good evening! I have 2 client tables and email ,when saving the client I need to save the email next to the email table, but I need to call the "save" function of the email controller. How do I do? Thank you all

2 answers

0

To use a model from another controller, you can do the following:

public function add() {
    ...
    Controller::loadModel('EmailModel');
    $this->EmailModel->save(...);
}
  • explains me, the Controller::loadModel('Emailmodel')?

  • Within the method you use the model (example) "Emailmodel". For example, within your method add()

  • paste and look like this: Controller::loadModel('Email'); $email="[email protected]"; $this->Email->save($email); but still error in "Controller:::loadModel('Email');" the error that shows is "Class 'App Controller Controller' not found" even trying to call "use App Controller Controller;" still gives error...

  • I forgot to mention that this code should be placed inside the Controller, not in the Model. You’re doing this in the Controller, right?

  • yes , in the controller

  • Just in case, add this to the beginning of your controller App::uses('Controller', 'Controller');

  • put and appeared error "Class 'App Controller App' not found" my version of cake is that newer...makes a lot of difference?

  • Possibly. Try using the function $this->loadModel('Email'), as in the documentation: https://book.cakephp.org/3.0/pt/controllers.html#loading-models-additional

Show 3 more comments

0

At the beginning of the controller you have to put:

use Cake Controller Controller;

And in the method place: Controller::loadModel('model name_model');

After I put it the error disappeared.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.