same name component and model in cakePhp? - php

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()

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.

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.

Related

How to associate model for a controller which is different than one computed from Inflector class?

In Cake 2.x, models are lazy loaded in the controller if the name of controller and model follow a convention - pluralized controller, singularized model.
However, I have a scenario where my controller and model don't follow this convention. How do I load the model for each method of the controller?
One way is to use loadModel method of the controller. But I will have to repeat it in each method.
One way is to use loadModel method of controller. But I will have to repeat it in each method.
https://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
Use beforeFilter().
However, this sounds like you're doing stuff in a controller that should be done in another controller that reflects what actually is inside the domain of the model you're using.

Extending ORM classes in CakePHP 3

I would like to extend Query class in order to create function customContain() available in every Table model. How should I do it?
I want to use that BleMethod() in all table models in cakephp. Where I have to add code of that function? Where I have to implements BleMethod?
Unlike Cake2 Cake3 does not feature an application level class like AppModel from where all other classes inherit from. So you have two options:
Trait
Behavior
The behavior can be loaded globally to all models by using the Model.initialize event. And then loading the behavior inside the events callback. Read these pages:
Creating a behavior
Event system
Model / Table callbacks
But that's not what you want
customContain() indicates for me that you want to setup some contains very often. Well, use finders.
Finders can be combined:
$this->Table->find('foo')->find('myContains')->all();
Each custom find will add something to the query object. You can add your custom contains this way.
Read Custom Finder Methods.

How can I ensure that my model named View will be called instead of laravel's View

I have a model named 'View' but laravel also has View object (the "V" in the MVC). I will be calling my model 'View' from different areas of my application. How can I make sure that it's not referring to laravel's View?
Extending #RonaldPK 's answer a little bit.
You have your App\View and Laravel has Illuminate\Support\Facades\View
use your App\View as ViewModel instead of View. That should do it.
Use App\View as ViewModel;
A better option: using ViewInterface and referencing it to your model. This will help you get rid of lots of problems at once.
Perhaps by using namespaces: "a way of encapsulating items" in order to prevent name collisions.

Best practice in CakePHP for call function in another controller

I have an controller attachment responsible for handling all uploads files. This controller uses a component to perform this control.
Now I have the following problem, I need call a function implemented in attachments controller from another controller, what is the best practice in this case? whereas:
It is not advised to call a function from a controller to another.
If I implement the function in my model this would have to use a component that is also not advised.
You don't. It's completely wrong and violating the MVC pattern.
Files represent a single entity in the system and should be handled as those (IMO) and in the model layer, not in the controller. You implement the validation and saving logic in a single model and access that model through associations from other places if you have too.
For example User hasOne Avatar, Gallery hasMany Image. Where the Avatar and Gallery association is your attachments or files table model.
I've written a plugin that does exactly the above and a little more
https://github.com/burzum/cakephp-file-storage
Use that or move your controller logic into a model. Controllers should be really really skinny and only have, well, controlling logic, they should never care about data handling or manipulation.

CakePHP how to retrieve data from another controller?

I need to show latest news titles on the main page of my site.
Main page is static and based on the Cake's default PagesController.
I have table, model, controller and view for news part of my site.
In the Cookbook I found the
Controller::requestAction
method, but it also says that it can cause a poor performance if I don't use cache.
As I know I also can create a method needed for all (or some) of my controllers in the AppController itself, but how I can link this method with specific model?
What is a proper OOP way to achieve my goals?
If you need to share logic/data between controllers your best bet is to use a component. Put your logic in your component and simply call the component's method from each of your controllers.
Edit
To access your model from your component, you can simply pass whichever model it is to your component's method. Example:
Component
public function fetchMyData($model) {
return $model->find('all');
}
Controller
$mydata = $this->ComponentName->fetchMyData($this->ModelName);
I don't know if that's what you meant, but you can access models that arn't related to a specific controller by adding
public $uses = array('modelINeed');
in your controller's definitions. For more info, look at this Controllers in cakePHP, CTRL + F and look for "$uses", .
I do not suggest loading one controller into another. Use the models to retrieve data from the database!

Categories