Mike
Mike

Reputation: 530

CakePHP: calling other Model functions

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

Upvotes: 12

Views: 24154

Answers (7)

DJ Far
DJ Far

Reputation: 506

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

Upvotes: 0

Rápli András
Rápli András

Reputation: 3923

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.

Upvotes: 0

Hardik Sondagar
Hardik Sondagar

Reputation: 4495

User App::import()

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

Upvotes: 2

AlexBrand
AlexBrand

Reputation: 12429

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

Upvotes: 7

Vinayak Phal
Vinayak Phal

Reputation: 8919

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.

Upvotes: 18

Luke Barker
Luke Barker

Reputation: 915

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.

Upvotes: 0

Anh Pham
Anh Pham

Reputation: 5481

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

  • use bindModel

Upvotes: 0

Related Questions