First of all, and IMHO when you need to access a model from a controller,
better set it in the $uses array rather than taking advantage of model
linking.
Why? Source code readability. When I'm skimming through a controller I'm
expecting to see how it binds to business logic on the $uses array (i.e:
what data is this controller handling?), while when using the model linking
way you need to get right down to the action to figure it out. Again, it's
MHO.
* One way is just set a $GLOBAL variable on AppController, and then retrieve
on AppModel, but that's not nice (not very OOP).
* Another way is to use Cake's Configure variable to set it:
Configure::write('data_aco_list', $dataAcoList);
And then on your model get it as:
$dataAcoList = Configure::read('data_aco_list');
But then again, not nice since even though Configure::read() can handle any
data, its documentation states that it returns strings, so better keep that
for strings.
* Another way, following on second method I provided you before (still not
good enough since we'll be accessing a private member of ClassRegistry):
1. Create a file called app_data_aco_list_model.php on your app/ folder
with:
class AppDataAcoListModel extends AppModel
{
var $dataAcoList;
}
2. For those models that *will* use this variable, change their declaration
to (I'm assuming here a model named Model):
require_once(APP . 'app_data_aco_list_model.php');
class Model extends AppDataAcoListModel {
// ...
}
3. Do this on your AppController::beforeFilter()
$classRegistry = ClassRegistry::getInstance();
foreach($classRegistry->_objects as $key => $object)
{
if (is_subclass_of($object, 'AppDataAcoListModel'))
{
$classRegistry->_objects[$key]->dataAcoList = $dataAcoList;
}
}
If you wish to do that for all models then omit step 1 and 2, and on step 3
change 'AppDataAcoListModel' with 'AppModel'
I'm wondering what data you need to send to *all* models, perhaps there's a
better way to do the whole thing...
-MI
---------------------------------------------------------------------------
Remember, smart coders answer ten questions for every question they ask.
So be smart, be cool, and share your knowledge.
BAKE ON!
blog: http://www.MarianoIglesias.com.ar
-----Mensaje original-----
De: [email protected] [mailto:[EMAIL PROTECTED] En nombre
de Langdon Stevenson
Enviado el: Domingo, 25 de Febrero de 2007 11:22 p.m.
Para: [email protected]
Asunto: Re: Passing value from appController to all models used by an action
I have hit a snag with the approach below. In most cases I do not need
to set the $uses array. If I need to call a related model then I will
do the following (in OperationsController for instance):
$this->Operation->OperationType->generateList()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---