Monday, April 4, 2011

Re: Delegates

I have no idea what it is you are looking for beyond that which Cake (and MVC) can already provide if you do some planning and then structure your application correctly.

You could place common functions that are called from models that need to make calculations before returning data to the controller in behaviours. If (as you emphasise in scenario 4) you do not need to access data from one model in another) then use a component, which is an object that can be accessed via multiple controllers.

The authentication is something that can extend the Auth component, or write your own. This should only be needed from part of your application (say, the Users controller) so is quite simple.

If you want to handle data from many models, then make sure your database is designed correctly (foreign keys, referential integrity etc) and then create your model associations properly (hasMany, belongsTo etc). Then you can access functions and data in distantly related models by daisy chaining them together.

You do not need a new layer (this is not groundbreaking stuff that proves that MVC is outdated and insufficient) - you just need to do some planning and a bit of hard work.


On 4 Apr 2011, at 19:43, Anas Mughal wrote:

I appreciate your responses. Here is some detail on what I am trying to achieve. Thank you for your advice and suggestions.

Ideally, I wish to have functionality that could be called from different controllers. (Generally, in enterprise applications, I would place this functionality in a delegate package.)

Here is the access that applications would follow:

Views --> Controllers --> Delegates --> Models

(Views can access all controllers. Controllers can access all delegates. Delegates can access all models.)


Yes, in most cases, there is no need for a delegate:

Views --> Controllers --> Models


Here is typical logic that could be placed in delegates:
  1. Distance calculations
  2. Time conversions (based on time zones, etc.)
  3. Handling authentication from different sources (openID, etc.)
  4. Providing logic and data that combines information from multiple models.

In item 1, I need to access multiple tables to fetch latitude and longitude information for different entities. Then, perform distance calculations. I want a generic method that would calculate distance for different types of objects.

In item 2, event times need to be converted based on entity time zone information. Multiple models need to be accessed.

Scenario 4 is typically where I need to access multiple models to prepare my data. (I do NOT want to access one model data from another model.) I could access all the needed models from my controller. However, I wish to make the functionality re-usable and available to other controllers.

I appreciate folks recommending the components approach. If I could access all models (i.e. database) from components, then I could perform my calculations in my components and make them available to all the controllers. Would components be the right approach?

Honestly, I just need a layer that sits between Controllers and Models.

Thank you very much in advance.










On Sat, Apr 2, 2011 at 5:14 AM, majna <majnaggz@gmail.com> wrote:
>Can I have sub-folders in the models?
Yes you can.
That goes for controllers and "libs" too, whithout any configuraton.


On Apr 1, 11:36 am, Ryan Schmidt <google-2...@ryandesign.com> wrote:
> On Apr 1, 2011, at 10:29, Anas Mughal wrote:
>
> > Do components have access to models?
>
> My understanding is that components can have their own models. I don't know if components can access the app's main models. The main app's models could however make use of and even extend the component's models. The idea is that components are supposed to be black boxes, and that you could copy a component from one project to another and reuse it. The book explains more about components:
>
> http://book.cakephp.org/view/996/Creating-Components
>
> > Otherwise, I will consider your recommendation and put my delegates in the model package.
>
> You mean: in the model. There's no such thing as a "model package".
>
> > Can I have sub-folders in the models?
>
> I don't believe so.

--
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php



--
Anas Mughal






--
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: