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.
Page 1 of 1
Access module models ?
#2
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.
#3
Posted 19 April 2010 - 10:17 PM
Is there a way to autoload module classes? other then adding include paths on all modules?
#4
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.
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.
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.
#5
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.
#6
Posted 21 April 2010 - 04:37 AM
Vince., 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);
Share this topic:
Page 1 of 1