Yii Framework Forum: How can I get the a model instance dinamicaly? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How can I get the a model instance dinamicaly? Rate Topic: -----

#1 User is offline   GodFather 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 19-July 09

Posted 17 February 2010 - 02:45 PM

Hi folks,
I'm creating a generic controller to be used in a model, this controller will work with any modules, but in some actions I'll need to pass a model instance, like this:

public actionCreate()
{
  $model = new MYACTUALMODEL;
  //do some thing ...
}


Some controllers on my app will extend this controller.

A inportant note:
Normally the controller name and de model name will be the same, so:
//if the controller name is:
PersonController
//the model name will be:
Person


So I need know:

How can I get a model instance dinamicaly to use in place of MYACTUALMODEL ?

Thanks.
0

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 17 February 2010 - 03:49 PM

Seems you want something like this (in the parent controller):

public actionCreate()
{
    $model = new $this->id;
    //do some thing ...
}

0

#3 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 17 February 2010 - 06:41 PM

you can also use the get_called_class() function within the controller class. If you then strip of the last part of the class name (i.e. 'Controller'), you then have your model name.
0

#4 User is offline   GodFather 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 19-July 09

Posted 17 February 2010 - 10:01 PM

Hi guys, first of all, thanks for answer my post.

Onman, I saw your tip, but the get_called_class() return the class name as a string and I can't use a string to instantiate a object.

For exemple $c = new 'MODELNAME' don't work, becouse 'MODELNAME' is a string, the idea is good, but have this problem.

Andy_s I don't understand what you said, if I use "$this" on action controller I think that I will references the parent controller class and not the model class.

I need to create a model instance based on controller name.

Thanks.
0

#5 User is offline   GodFather 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 19-July 09

Posted 17 February 2010 - 10:53 PM

Onman,
after do alot of test I found that I can't instantiate a class using a string, but if I put this string in a variable, I can use it to instantiate a class, like this:

$v = "classname";
$t = new $v;



Now I'll try this on my project and after I'll put the result here.
Thanks
0

#6 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 18 February 2010 - 12:47 AM

Quote

Andy_s I don't understand what you said, if I use "$this" on action controller I think that I will references the parent controller class and not the model class.


$this->id references to the current controller's id. If you have controller PersonController extending GenericController, it will return "Person". So if you want to create some common methods in your generic controller where you instantiate some models (and you assume that a model of PersonController has name Person), you can do it this way (in GenericController's method): $model = new $this->id. So when you extend GenericController with PersonController, $this->id will return "Person".

Sorry if I understood you wrong.
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 February 2010 - 05:09 AM

To make it more clean you could put the logic for model creation into it's own method in the parent controller:

private $_model;
public function getModel()
{
    // Find out model name here, e.g. like andy_s suggested
    $modelname=$this->id;
    if($this->_model===null)
      $this->_model = new $modelname;
    return $this->_model
}


In any extended controller (and even view) you can access that model easily with $this->model;
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users