Yii Framework Forum: load a Model information from a Controller - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

load a Model information from a Controller Rate Topic: -----

#1 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 12 May 2009 - 06:39 AM

Hi, i'm in the controller of a model and in this controller I want to load the information of another model passing it the specific id in the db.

How can I do this?

Make me an example please with a model name :)
0

#2 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 12 May 2009 - 07:12 AM

in the ModelAController you can call

ModelB::model()->findByPk($id) // OR any method of ModelB
0

#3 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 12 May 2009 - 07:22 AM

Ah ok so I can't call a method of ModelBController.php ?

Couse the loadModelB($id=null) is in ModelBController.php :)

So I have to copy it in the ModelBController or just use the part of code tha I need, isn't it?
0

#4 User is offline   StErMi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 259
  • Joined: 30-March 09

Posted 12 May 2009 - 07:31 AM

Ok maybe I'm saying a stupid thing...

But I think that I can use $modelBobj = new ModelB($_GET['id']);

Like in every OOP language as costructor... Am I right?
0

#5 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,856
  • Joined: 04-October 08
  • Location:DC, USA

Posted 12 May 2009 - 07:36 AM

You can still call a method of a different controller (as long as you include class file first). However, in this case, it is better to abstract the shared code out and save them in a separate class (or in the model class).
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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