Yii Framework Forum: Access module models ? - Yii Framework Forum

Jump to content

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

Access module models ? Rate Topic: -----

#1 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 19 April 2010 - 04:18 AM

How can I load the models from a module... I am using the user module but something like: User::model()->findByPK(1);

Inside the application does not work.
0

#2 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 19 April 2010 - 05:03 AM

Make sure you put the User model inside a directory that you import. i usually put the models under protected/models and add the path alias 'application.models.*' to the import array in the application configuration.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#3 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 19 April 2010 - 10:17 PM

Is there a way to autoload module classes? other then adding include paths on all modules?
0

#4 User is offline   Onman 

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

Posted 20 April 2010 - 04:19 AM

To use a model from a module, create a path alias to that module and then import the desired model using that path alias, e.g.

Yii::setPathOfAlias('MyAlias','path/to/the/module');

Yii::import('MyAlias.models.DesiredModel');  // this will include the class DesiredModel immediately

$model = Yii::createComponent('DesiredModel');


Make sure your import specifies the path to a particular class (i.e. don't use a wildcard) so the class will be included immediately.
0

#5 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 21 April 2010 - 01:41 AM

Quote

Make sure your import specifies the path to a particular class (i.e. don't use a wildcard) so the class will be included immediately.


The class will not included immediately unless you pass in the value 'true' to the second argument in the import function to force include the class. other wise it will be included when needed.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#6 User is offline   Onman 

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

Posted 21 April 2010 - 04:37 AM

View PostVince., on 21 April 2010 - 01:41 AM, said:

The class will not included immediately unless you pass in the value 'true' to the second argument in the import function to force include the class. other wise it will be included when needed.


Oops, yes you're right, forgot about that:

Yii::import('MyAlias.models.DesiredClass',true);

0

#7 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 08-May 09

Posted 21 April 2010 - 05:15 AM

Yii::app()->getModule('moduleId');

1

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