Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending ORM classes in CakePHP 3

I would like to extend Query class in order to create function customContain() available in every Table model. How should I do it?

like image 665
Zielony_Buszmen Avatar asked Mar 20 '26 02:03

Zielony_Buszmen


1 Answers

I want to use that BleMethod() in all table models in cakephp. Where I have to add code of that function? Where I have to implements BleMethod?

Unlike Cake2 Cake3 does not feature an application level class like AppModel from where all other classes inherit from. So you have two options:

  • Trait
  • Behavior

The behavior can be loaded globally to all models by using the Model.initialize event. And then loading the behavior inside the events callback. Read these pages:

  • Creating a behavior
  • Event system
  • Model / Table callbacks

But that's not what you want

customContain() indicates for me that you want to setup some contains very often. Well, use finders.

Finders can be combined:

$this->Table->find('foo')->find('myContains')->all();

Each custom find will add something to the query object. You can add your custom contains this way.

Read Custom Finder Methods.

like image 126
floriank Avatar answered Mar 21 '26 18:03

floriank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!