Re: What is the advised way to call a function from another controller?
Spain, lucky beggar! We have dreams of France/Spain/Oz/New Zealand, but currently stuck in the mediocrity of the UK rat race where two full-time working parent's struggle to make enough to pay the monthly bills. The only saving grace is that I run my own business and work from home so at least I can be flexible enough to look after my daughter after school and in holidays plus play a few sets of tennis during the day a cpl of times a week! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
Glad you got it sorted. There seem to be various 'best practices' regarding OOP and MVC depending on who you are, what the language is and what the framework is. My personal best practice is 'get it done'. > Are you still in the North East or have plying your trade elsewhere? North East of Spain now. Plying my trade everywhere and nowhere. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
leo, Sounds like you obviously know more about this than me and are probably more proficient in OOP and MVC than I am (not too hard as I'm self-taught so have a lot of undiscovered bad habits). >From the little understanding I have I agree that they should be individual functions in the Model and it would make sense to include the wrapper function in the controller as not all forms will need all of the list arrays ... thanks very much for your input! Are you still in the North East or have plying your trade elsewhere? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
> you can wrap them, again within the model class, with one function > that calls all. > Thinking about this, it might be more correct to code the wrapper method in the controller where the return result can be better tailored to the function of that controller. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
Just checked your profile, Paul. We're from the same neck of the woods. I was born in Quarrington Hill. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
Looks good, but I'd be inclined to further break it up into functions that return individual lists. There might be a time later on when you want to just get ethnicities or sexualities or whatever. If need be, you can wrap them, again within the model class, with one function that calls all. On Jan 28, 12:15 pm, WebbedIT wrote: > I have gone with the Model approach as it seems right. See the > following and please give your thoughts: > > Model: > ... > function __formData($authUser = array()) { > if ($authUser['User']['user_group_id'] < 3) { > $organisations = $this->Organisation->find('list', array( > 'conditions'=>array('Organisation.is_deleted'=>'0'), > 'order'=>'Organisation.name ASC' > )); > $output['organisations'] = compact('organisations'); > } elseif ($authUser['User']['user_group_id'] == 3) { > $organisations = $this->Organisation->find('list', array( > 'conditions'=>array('Organisation.is_deleted'=>'0', > 'Organisation.scheme_id' => $authUser['Scheme']['id']), > 'order'=>'Organisation.name ASC' > )); > $output['organisations'] = compact('organisations'); > } > > $users = $this->User->find('list', array > ('fields'=>'User.username')); > $this->set(compact('users')); > > $ethnicities = $this->Ethnicity->find('list', array( > 'conditions'=>array('Ethnicity.option_list_id'=>'13'), > 'fields'=>array('Ethnicity.value'), > 'order'=>'Ethnicity.value ASC' > )); > $output['ethnicities'] = compact('ethnicities'); > > $sexualities = $this->Sexuality->find('list', array( > 'conditions'=>array('Sexuality.option_list_id'=>'18'), > 'fields'=>array('Sexuality.value'), > 'order'=>'Sexuality.value ASC' > )); > $output['sexualities'] = compact('sexualities'); > > $religions = $this->Religion->find('list', array( > 'conditions'=>array('Religion.option_list_id'=>'30'), > 'fields'=>array('Religion.value'), > 'order'=>'Religion.value ASC' > )); > $output['religions'] = compact('religions'); > > $genders = $this->Gender->find('list', array( > 'conditions'=>array('Gender.option_list_id'=>'12'), > 'fields'=>array('Gender.value'), > 'order'=>'Gender.value ASC' > )); > $output['genders'] = compact('genders'); > return $output;} > > ... > > Controller: > ... > $formData = $this->Referral->Client->Person->__formData($this->authUser); > > foreach ($formData as $key => $value) { > $this->set($formData[$key]);} > > ... > > I believe this fits correctly within Cake's MVC design pattern, but > I'm fully open to suggestions as to how this can be improved. > > Paul. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
I have gone with the Model approach as it seems right. See the following and please give your thoughts: Model: ... function __formData($authUser = array()) { if ($authUser['User']['user_group_id'] < 3) { $organisations = $this->Organisation->find('list', array( 'conditions'=>array('Organisation.is_deleted'=>'0'), 'order'=>'Organisation.name ASC' )); $output['organisations'] = compact('organisations'); } elseif ($authUser['User']['user_group_id'] == 3) { $organisations = $this->Organisation->find('list', array( 'conditions'=>array('Organisation.is_deleted'=>'0', 'Organisation.scheme_id' => $authUser['Scheme']['id']), 'order'=>'Organisation.name ASC' )); $output['organisations'] = compact('organisations'); } $users = $this->User->find('list', array ('fields'=>'User.username')); $this->set(compact('users')); $ethnicities = $this->Ethnicity->find('list', array( 'conditions'=>array('Ethnicity.option_list_id'=>'13'), 'fields'=>array('Ethnicity.value'), 'order'=>'Ethnicity.value ASC' )); $output['ethnicities'] = compact('ethnicities'); $sexualities = $this->Sexuality->find('list', array( 'conditions'=>array('Sexuality.option_list_id'=>'18'), 'fields'=>array('Sexuality.value'), 'order'=>'Sexuality.value ASC' )); $output['sexualities'] = compact('sexualities'); $religions = $this->Religion->find('list', array( 'conditions'=>array('Religion.option_list_id'=>'30'), 'fields'=>array('Religion.value'), 'order'=>'Religion.value ASC' )); $output['religions'] = compact('religions'); $genders = $this->Gender->find('list', array( 'conditions'=>array('Gender.option_list_id'=>'12'), 'fields'=>array('Gender.value'), 'order'=>'Gender.value ASC' )); $output['genders'] = compact('genders'); return $output; } ... Controller: ... $formData = $this->Referral->Client->Person->__formData($this- >authUser); foreach ($formData as $key => $value) { $this->set($formData[$key]); } ... I believe this fits correctly within Cake's MVC design pattern, but I'm fully open to suggestions as to how this can be improved. Paul. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
What about making the method static? Otherwise you can just instantiate the object, and use its methods. On Jan 27, 8:59 am, leo wrote: > On Jan 27, 3:26 pm, RichardAtHome wrote: > > > or you could put the function in your AppController > > Surely then you would either have non-generic methods relating to > models that don't exist at that point or models included unnecessarily > in all controllers? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
On Jan 27, 3:26 pm, RichardAtHome wrote: > or you could put the function in your AppController Surely then you would either have non-generic methods relating to models that don't exist at that point or models included unnecessarily in all controllers? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
or you could put the function in your AppController On Jan 27, 2:17 pm, leo wrote: > I'm not suggesting this is the correct way to do it, but I would put > those methods in the model definitions to which they relate. Then they > are accessible from any controller that 'uses' that model in addition > to its own. Then you can say something like: > > $organisations= $this->Person->Organisation->formData(); > $genders = $this->Person->Gender->formData(); > ...and so on... > > On Jan 27, 2:07 pm, WebbedIT wrote: > > > Are you sure a component would be correct for this as again the > > CookBook states "To access/use a model in a component is not generally > > recommended" and my __formData() functions are made to access models: > > > function formData() { > > $organisations= $this->Person->Organisation->find('list', array( > > 'order'=>'Organisation.name ASC' > > )); > > $this->set(compact('organisations')); > > > $users = $this->Person->User->find('list', array > > ('fields'=>'User.username')); > > $this->set(compact('users')); > > $ethnicities = $this->Person->Ethnicity->find('list', array( > > 'conditions'=>array('Ethnicity.option_list_id'=>'13'), > > 'fields'=>array('Ethnicity.value'), > > 'order'=>'Ethnicity.value ASC' > > )); > > $this->set(compact('ethnicities')); > > > $sexualities = $this->Person->Sexuality->find('list', array( > > 'conditions'=>array('Sexuality.option_list_id'=>'18'), > > 'fields'=>array('Sexuality.value'), > > 'order'=>'Sexuality.value ASC' > > )); > > $this->set(compact('sexualities')); > > > $religions = $this->Person->Religion->find('list', array( > > 'conditions'=>array('Religion.option_list_id'=>'30'), > > 'fields'=>array('Religion.value'), > > 'order'=>'Religion.value ASC' > > )); > > $this->set(compact('religions')); > > > $genders = $this->Person->Gender->find('list', array( > > 'conditions'=>array('Gender.option_list_id'=>'12'), > > 'fields'=>array('Gender.value'), > > 'order'=>'Gender.value ASC' > > )); > > $this->set(compact('genders')); > > > } > > > Also I would still have to create a lot of logic or duplicate > > functions within the component so that the find calls were made via > > the right associations, i.e. the above function would work fine when > > the referencing controller was PeopleController but when called from > > ClientController the find calls would have to start $this->Client- > > > >Person... and from Staff $this->Staff->Person... etc. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
I'm not suggesting this is the correct way to do it, but I would put those methods in the model definitions to which they relate. Then they are accessible from any controller that 'uses' that model in addition to its own. Then you can say something like: $organisations= $this->Person->Organisation->formData(); $genders = $this->Person->Gender->formData(); ...and so on... On Jan 27, 2:07 pm, WebbedIT wrote: > Are you sure a component would be correct for this as again the > CookBook states "To access/use a model in a component is not generally > recommended" and my __formData() functions are made to access models: > > function formData() { > $organisations= $this->Person->Organisation->find('list', array( > 'order'=>'Organisation.name ASC' > )); > $this->set(compact('organisations')); > > $users = $this->Person->User->find('list', array > ('fields'=>'User.username')); > $this->set(compact('users')); > $ethnicities = $this->Person->Ethnicity->find('list', array( > 'conditions'=>array('Ethnicity.option_list_id'=>'13'), > 'fields'=>array('Ethnicity.value'), > 'order'=>'Ethnicity.value ASC' > )); > $this->set(compact('ethnicities')); > > $sexualities = $this->Person->Sexuality->find('list', array( > 'conditions'=>array('Sexuality.option_list_id'=>'18'), > 'fields'=>array('Sexuality.value'), > 'order'=>'Sexuality.value ASC' > )); > $this->set(compact('sexualities')); > > $religions = $this->Person->Religion->find('list', array( > 'conditions'=>array('Religion.option_list_id'=>'30'), > 'fields'=>array('Religion.value'), > 'order'=>'Religion.value ASC' > )); > $this->set(compact('religions')); > > $genders = $this->Person->Gender->find('list', array( > 'conditions'=>array('Gender.option_list_id'=>'12'), > 'fields'=>array('Gender.value'), > 'order'=>'Gender.value ASC' > )); > $this->set(compact('genders')); > > } > > Also I would still have to create a lot of logic or duplicate > functions within the component so that the find calls were made via > the right associations, i.e. the above function would work fine when > the referencing controller was PeopleController but when called from > ClientController the find calls would have to start $this->Client- > > >Person... and from Staff $this->Staff->Person... etc. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
Looks like you need to re factor a bit more Thumb rules - #1 sharing controller code - components #2 sharing model code - behaviour #3 sharing view code - elements HTH Tarique On Tue, Jan 27, 2009 at 6:37 PM, WebbedIT wrote: > > Are you sure a component would be correct for this as again the > CookBook states "To access/use a model in a component is not generally > recommended" and my __formData() functions are made to access models: > -- = Cheesecake-Photoblog: http://cheesecake-photoblog.org PHP for E-Biz: http://sanisoft.com = --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
Are you sure a component would be correct for this as again the CookBook states "To access/use a model in a component is not generally recommended" and my __formData() functions are made to access models: function formData() { $organisations= $this->Person->Organisation->find('list', array( 'order'=>'Organisation.name ASC' )); $this->set(compact('organisations')); $users = $this->Person->User->find('list', array ('fields'=>'User.username')); $this->set(compact('users')); $ethnicities = $this->Person->Ethnicity->find('list', array( 'conditions'=>array('Ethnicity.option_list_id'=>'13'), 'fields'=>array('Ethnicity.value'), 'order'=>'Ethnicity.value ASC' )); $this->set(compact('ethnicities')); $sexualities = $this->Person->Sexuality->find('list', array( 'conditions'=>array('Sexuality.option_list_id'=>'18'), 'fields'=>array('Sexuality.value'), 'order'=>'Sexuality.value ASC' )); $this->set(compact('sexualities')); $religions = $this->Person->Religion->find('list', array( 'conditions'=>array('Religion.option_list_id'=>'30'), 'fields'=>array('Religion.value'), 'order'=>'Religion.value ASC' )); $this->set(compact('religions')); $genders = $this->Person->Gender->find('list', array( 'conditions'=>array('Gender.option_list_id'=>'12'), 'fields'=>array('Gender.value'), 'order'=>'Gender.value ASC' )); $this->set(compact('genders')); } Also I would still have to create a lot of logic or duplicate functions within the component so that the find calls were made via the right associations, i.e. the above function would work fine when the referencing controller was PeopleController but when called from ClientController the find calls would have to start $this->Client- >Person... and from Staff $this->Staff->Person... etc. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: What is the advised way to call a function from another controller?
Consider re-factoring your fromData() into a component Tarique On Tue, Jan 27, 2009 at 5:55 PM, WebbedIT wrote: > > In most of my controllers I have a __formData() function which creates > any arrays for select lists, radio buttons, check-boxes etc. I can > then call this from the add() and edit() actions and save some lines > of code etc. -- = Cheesecake-Photoblog: http://cheesecake-photoblog.org PHP for E-Biz: http://sanisoft.com = --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
What is the advised way to call a function from another controller?
In most of my controllers I have a __formData() function which creates any arrays for select lists, radio buttons, check-boxes etc. I can then call this from the add() and edit() actions and save some lines of code etc. However I would also like to call this function from another controller when I am displaying a form which manages data across multiple models. I believe I could use requestAction(), but the CookBook states "It is rarely appropriate to use in a controller or model" so how do I go about this in the most efficient manner? Thanks, Paul. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---