12

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

1

7 Answers 7

18

We can use Model relation to call the function in another model. Eg.

$this->Model->ModelOne->find();
$this->Model->ModelOne->customFunc();

If there is no relation in the models, The we can use

$this->loadModel('ModelName');

To use in the model. In this case you can use

$this->ModelName->function();

directly as you've loaded that model.

Sign up to request clarification or add additional context in comments.

2 Comments

This does not work in cakephp 2.3 ( if loaded in a controller, ofc works, in model not )
7

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

1 Comment

ClassRegistry::init('OtherModel')->function(); Worked perfectly! Thanks
2

User App::import()

App::import('Model','OtherModel');
$attr = new OtherModel();
$attr->Othermodelfunction();

Comments

0
  • if there's a (direct or indirect) relationship between the model, you can call the function: $this->Model1->Model2->...->Modeln->function();

  • use bindModel

Comments

0

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.

Comments

0

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.

Comments

0

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.