I have created a class that components can descend which provides this
functionality.

http://svn3.xp-dev.com/svn/cakephp/libs/magic_tools/libs/magic_component.php

MagicComponent offers you this functionality by letting you specify
which attributes you want to merge in your AppController and
descending controllers using the MagicComponent::$attributesToMerge
attribute:

MyComponent extends MagicComponent {
  var $attributesToMerge = array('someVar');
}

Now your AppController::$someVar and descending controller's $someVar
attribute automatically get merged (they both need to be an array, of
course):

AppController extends Controller {
  var $someVar = array('one', 'two');
}

SomeController extends AppController {
  var $someVar = array('three');
}

For SomeController, MagicComponent will now automatically set $someVar
to the value `array('one', 'two', 'three')` - as you know it already
from some other variables like AppController::$uses or
AppController::$helpers.

Notice: I think I read somewhere that components should inherit from
Object because otherwise it could lead to infinite loops or something
like that. I didn't experience this problem so far, but be warned!

On Thu, Dec 2, 2010 at 12:47 PM, Joshua Muheim <psybea...@gmail.com> wrote:
> Thanks for pointing me into the right direction! :-)
>
> On Thu, Dec 2, 2010 at 11:56 AM, Amit Badkas <amit.sanis...@gmail.com> wrote:
>> Hi,
>> Controller does merging of 'uses', 'helpers' and 'components' only by
>> default, please have a look at __mergeVars() method in
>> cake/libs/controller/controller.php
>> Amit Badkas
>> PHP Applications for E-Biz: http://www.sanisoft.com
>>
>>
>> On Thu, Dec 2, 2010 at 3:53 PM, psybear83 <psybea...@gmail.com> wrote:
>>>
>>> Hey everybody
>>>
>>> I know that CakePHP applies some magic to inheritance. So when having
>>> a class...
>>>
>>> class AppController extends Controller {
>>>  var $uses = array('Model', 'AnotherModel');
>>> }
>>>
>>> ...and then extending another one from it...
>>>
>>> class AnotherController extends AppController {
>>>  var $uses = array('YetAnotherModel');
>>> }
>>>
>>> ...we don't end up with AnotherController::uses being...
>>>
>>> array('YetAnotherModel')
>>>
>>> ...(as PHP would normally do it), but with
>>>
>>> array('Model', 'AnotherModel', 'YetAnotherModel')
>>>
>>> This is very nice! But I wonder how I can use this magic for my own
>>> class variables? E.g. I want AppController::filters to be
>>> array('Filter1', 'Filter2') and then for UsersController I want to add
>>> 'Filter3', then normally I would have to use the beforeFilter:
>>>
>>> UsersController extends AppController {
>>>  function beforeFilter() {
>>>    $this->filters[] = 'Filter3';
>>>  }
>>> }
>>>
>>> But this isn't very beautiful, so I'd rather use the CakePHP magic
>>> mentioned above. But how could I do this? Is there an easy way to tell
>>> CakePHP to treat "filters" the same way as "uses" etc.?
>>>
>>> Thanks a lot for help
>>> Josh
>>>
>>> Check out the new CakePHP Questions site http://cakeqs.org and help others
>>> with their CakePHP related questions.
>>>
>>> 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
>>
>> Check out the new CakePHP Questions site http://cakeqs.org and help others
>> with their CakePHP related questions.
>>
>> 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
>>
>

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


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

Reply via email to