trying to grasp CakePHP 4 after transitioning from previous versions. Once there was this usefull way of propagating errors from Model to controller, that really helped a lot while trying to have thick models and slim controllers.
In your Model (Table) you just did:
$this->error = "something got wrong';
...in your controller you retrieved it via:
$this->Modelname->error
...and voila, you could use it wherever.
However CakePHP 4 is flipping me off with
You have not defined the error association on App\Model\Table\Modelname
What am I missing? Is there some new equivalent of these calls? Tried to go through documentation on www.cakephp.org, indeed there is a part about error handling, but that is quite overkill for something that was once simple.
And all SO / other questions related to this are pretty old, not for Cake PHP 4.
Thank you!
EDIT
After trying the approach within the same Model+Controller using much simpler method, it obviously still works the good old aforementioned way (which is great!). The script must be crashing somewhere inside my much more complex method and this is just a very weird manifestation of the crash.
BTW I know this is hard to find in CakePHP documentation, but I was leveraging it for a long time. So just for clarity, I'm talking about this:
Model:
class ModelTable extends Table {
public function myMegaBusinessLogic(){
// doing stuff
// ... but OOPS something is wrong ...
$this->error = "It broke down! :( ";
return false;
}
}
Controller:
class ModelController extends AppController{
public function myAction() {
if (!$this->Model->myMegaBusinessLogic()){
$this->Flash->error(__($this->Model->error));
} else {
$this->Flash-success(__("Nailed it!"));
}
return $this->redirect(['action' => 'index']);
}