Accessing Application Models from a Module

Hello -

I recently made a post in another thread inquiring about how to access core models (outside the modules directory) from within the modules directory

basically, the project has a Users model which is not confined to only one of my modules, but is used project-wide

for example - one of my modules saves a 'created-by' value into its model, and needs to consult the core Users model to find this value -

Can anybody give direction?

Is this a path issue? or is it a module configuration issue?

Also for the record:

The general types of errors that I am getting are of the ‘CWebApplication.Users’ is not defined variety when i try to access them from within the module

Outside the module, the same means to access these models works fine

i'm thinking i have to somehow load these models into the module??

well - i got something working, but i dont like how i did it

i copied all my other mvc's into the module and voila - my module started working

these seems to be a bit redundant - would love to know how i could call those objects without having to do something like this (for obvious reasons)

please let me know if there is a way to avoid having to do this copy hack job

thanks

Peace

hi

I don't seem to have your problem

how are you calling your models ?

MyModel::model()->findAll();

??

Quote

hi

I don't seem to have your problem

how are you calling your models ?

MyModel::model()->findAll();

??

yup - thats exactly what i am doing -

i noticed another post in a different thread today that mentioned something along the lines of

'if you are using models across different models you should put them in the applications directory'

would this be what i would need to do as well then?

and if so - how would i go about accomplishing this?

would it change how i am calling these models in general if i moved them elsewhere?

thanks for the help

** edit - i found the quote:

Quote

You will need to use Yii::app()->getModule($name) to generate the module instance first so that its init() can be executed.

Since you are sharing models among modules, it would better if you move those models to the application folder.

please advise

if i am missing something obvious - please let me know that as well

it wouldnt be the first time

you can leave a short response that says something like:

"youre a idiot"

my feelings wouldnt be hurt

thanks

It seems my response was meant for another post?

Would you please elaborate how you get this error? "CWebApplication.Users' is not defined". Are you trying to access Yii::app()->Users?

Nope, we never laugh at people who raise questions.

The problem is that sometimes a question is raised without giving enough details, and thus it makes people trying to guess what is the real problem.

Quote

It seems my response was meant for another post?

Would you please elaborate how you get this error? "CWebApplication.Users' is not defined". Are you trying to access Yii::app()->Users?

Nope, we never laugh at people who raise questions.

The problem is that sometimes a question is raised without giving enough details, and thus it makes people trying to guess what is the real problem.

  • yes - that is exactly what i am trying to do

from my Module, i am trying to access Yii::app()->Users

2 contexts:

  1. i am trying to display contents from the Users model in one of my module views

  2. in my module, i have a create method, and i need to set the author_id using the id of the user who is currently logged in

let me know any and all other details you may need - the help is appreciated

(and yes - i pulled that quote from another thread in the forum, but it seemed somehow applicable for this)

Why you are accessing Yii::app()->Users if the app doesn't have 'Users' property or app component defined? Are you trying to use 'Users' class that is under app's models directory? If so, you may simply reference Users::model().

Quote

Why you are accessing Yii::app()->Users if the app doesn't have 'Users' property or app component defined? Are you trying to use 'Users' class that is under app's models directory? If so, you may simply reference Users::model().

aha - nice - that solved my problem #2

i successfully sent a value to the database with that

but this still does not work (problem #1):

             

 <?php

       

        $static = Users::model()->findByPk(2);

		$this-&gt;renderPartial(&#039;/users/_show&#039;,array(


			&#039;static&#039;=&gt;$static,


		));

                ?>

attempting to display a particular entry in a _show view i created in the users view directory (not inside the module)

thanks for the help

Quote

Quote

Why you are accessing Yii::app()->Users if the app doesn't have 'Users' property or app component defined? Are you trying to use 'Users' class that is under app's models directory? If so, you may simply reference Users::model().

aha - nice - that solved my problem #2

i successfully sent a value to the database with that

but this still does not work (problem #1):

             

 <?php

       

        $static = Users::model()->findByPk(2);

		$this-&gt;renderPartial(&#039;/users/_show&#039;,array(


			&#039;static&#039;=&gt;$static,


		));

                ?>

attempting to display a particular entry in a _show view i created in the users view directory (not inside the module)

thanks for the help

You need to put you view files under module's views folder, unless you specified a different view path explicitly

ahh interesting - thanks man - i'll test this out

Quote

Quote

Quote

Why you are accessing Yii::app()->Users if the app doesn't have 'Users' property or app component defined? Are you trying to use 'Users' class that is under app's models directory? If so, you may simply reference Users::model().

aha - nice - that solved my problem #2

i successfully sent a value to the database with that

but this still does not work (problem #1):

             

 <?php

       

        $static = Users::model()->findByPk(2);

		$this-&gt;renderPartial(&#039;/users/_show&#039;,array(


			&#039;static&#039;=&gt;$static,


		));

                ?>

attempting to display a particular entry in a _show view i created in the users view directory (not inside the module)

thanks for the help

You need to put you view files under module's views folder, unless you specified a different view path explicitly

A+

thanks guys - i recognize the errors in my ways now

Peace