Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use more than one model in a CakePHP controller

I have created a registration form and I send data to the controller.

I want to insert this data to 3 different tables (models).

How can this be achieved?

like image 882
krissanawat Avatar asked Nov 30 '22 08:11

krissanawat


1 Answers

What you mean (in CakePHP terms) is that you want to use more models than the default one. the default model is the one named like your controller.

To achieve what you want you just declare a variable $uses in your controller. It's done like this:

<?php
class ExampleController extends AppController {
    var $name = 'Example';

    // $uses is where you specify which models this controller uses
    var $uses = array('Model1', 'Model2', 'ModelN');

    // ... here go your controller actions (methods)

}
?>   

This will allow your controller to make use of Model1, Model2 and ModelN. Rename those and add more according to your needs.

If you do not wish to use models in your controller, you can assign $uses to an empty array, i.e.:

var $uses = array();

Take a look at the corresponding CakePHP book chapters according to the version you're using:

  • Cake v1.3 "Components helpers and users", and
  • Cake v2.0 controller::$uses
like image 147
Oerd Avatar answered Dec 02 '22 22:12

Oerd