same name component and model in cakePhp?

875 views Asked by At

I have same name component and model e.g. product. So how can we use the component function and model functions?

E.g.: $this->componentName->function() / $this->modelName->function()

2

There are 2 answers

1
GauravGD On

call the components from the collection object.

$this->Color->find(); // Model 
$this->Component->Color->method(); // Component

I ran into this problem when I had a model and component both named "Account". Model gets loaded after so I think it overwrites.

However, I am not sure the Component thing will work as I never tried it. It does work for Behaviors, so its worth a shot.

0
drmonkeyninja On

Assuming this is your own code I'd change the name used by your component. The way I tend to deal with this in CakePHP 2 is to use a plural naming convention with components. So in your example you would have the model Product and the component ProductsComponent. Then in your controller you can easily distinguish between the two:-

$this->Product->function(); // Model method
$this->Products->function(); // Component method

Seeing as in Cake's naming convention controllers are plural it makes sense to extend this to components.