CakePHP: calling other Model functions

前端 未结 7 629
感动是毒
感动是毒 2020-12-31 10:04

How can i call, from a Model, a function present in another model? I would like not to repeat code.

相关标签:
7条回答
  • 2020-12-31 10:08
    • if there's a (direct or indirect) relationship between the model, you can call the function: $this->Model1->Model2->...->Modeln->function();

    • use bindModel

    0 讨论(0)
  • 2020-12-31 10:08

    no, you should use ClassRegistry like so:

        //MessagesController - in my send() method...
    
    $this->set('content', ClassRegistry::init('Content')->find('first', array(
            'conditions' => array('Content.id' => 3)
    
            )));
    

    NOTE:this is from my controller but I am pretty sure it works in model too.

    0 讨论(0)
  • 2020-12-31 10:13

    You should try to have relationships between your models. There are many types of relationships which you can read here...

    If you have above said associations, you can access your associated models using:

    $this->Model->OtherModel->function();
    

    If your models are not related in any way, you should use:

    ClassRegistry::init('OtherModel')->function();
    

    You can check out my question on this where I obtained great answers

    0 讨论(0)
  • 2020-12-31 10:13

    User App::import()

    App::import('Model','OtherModel');
    $attr = new OtherModel();
    $attr->Othermodelfunction();
    
    0 讨论(0)
  • 2020-12-31 10:16

    In 2.x versions the $this->Model1->Model2 syntax answered above will not work. Calling functions from another models in many cases is the job of a controller, not the model. Consider that, the model methods should be limited to querying and updating data whilst maintaining database integrity.

    1st method: using the controller

    I'll illustrate this with an example of Firm and User models, while Firm hasMany users. This method is recommended, if you plan to add extra controller functionality inbetween, such as setting flash messages or cookies.

    User model:

    public function saveRegisteredUsers($user_data,$firm_id){ ... }
    

    --

    FirmsController:

    public function add(){
        if($this->Firm->save($this->request->data)){
    
        // set $this->Firm->id here 
    
        $this->loadModel('User');
        $this->User->saveRegisteredUsers($this->request->data['User'], 
          $this->Firm->id);
    
        // ... 
    
        }
    }
    

    2nd method: using the model

    For this you will need to have correct model associations. The table names have to be users and firms conventionally. Following the terminology of the example above your relation should be defined as this in the Firm model:

    public $hasMany = array( 'User' => array(
        'className' => 'User',
    ));
    

    In the User model, you have to set up the belongsTo association properly:

    public $belongsTo = array(
        'Firm' => array(
            'className' => 'Firm',
            'foreignKey' => 'firm_id',
            'dependent' => false
            )
        );
    

    After this, you can call $this->User->saveRegisteredUsers() directly from any of the Firm model methods.

    0 讨论(0)
  • 2020-12-31 10:28

    If you have a model function that you want to call from many models, the best approach is to abstract any references to the model name ($this->alias) and place the function in AppModel. Then it is accessible in any of your models.

    class AppModel extends Model{
        public function myFunction($options = array(){
           do some stuff with $this->alias;
        }
    }
    
    0 讨论(0)
提交回复
热议问题