use cakephp component inside a model

后端 未结 3 2098
长发绾君心
长发绾君心 2021-02-13 05:03

How do I use a component that I created in cakePHP inside one of my model classes? Is this possible?

If so, please let me know how I can do so

3条回答
  •  心在旅途
    2021-02-13 05:47

    It is possible to use a component inside a model (but I cannot comment if this is a recommended or a best-practice).

    Inspired from original source, an example to use a component called ‘Geocoder’ in a model:

    App::import('Component','GeoCoder');
    $gc = new GeoCoderComponent(new ComponentCollection);
    

    Then you can use $gc to call the functions of the component.

    --

    P.S.: I do not want to encourage bad programming practices, but sometimes the pressure of deadlines (in real world projects) may force a developer to make such decisions.

提交回复
热议问题