Re: Does calling model methods from elements break MVC pattern?

2008-10-05 Thread skylar

I'm really curious about this topic as well as it speaks to an aspect
of the CakePHP Model class that has never really clicked with me.
Andrea's example is perfect for explaining the point.

In most MVC frameworks, when you retrieve data from storage (ram, db,
file, etc), you usually get an instance of the model class itself, not
a map/dictionary structure.  This makes it easy to fetch calculated
values, such as full_name, age, etc.  In one respect, you could
consider these helper functions, but I usually think of them as
smart accessors - you store the minimal data necessary and calculate
more common derived values on the fly (or you cache them in the rare
chance it's worthwhile to do so).

Andrea's suggestion (pre-calculating values in the after_find method)
makes sense, but it assumes that the value is always worth calculating
and storing after a lookup.  (Perhaps you don't always need the value,
or calculating it takes a long time).  So what's the recommended
convention for CakePHP for handling this stuff?  Better yet, what do
some of you who are veterans with the framework do?

I could imagine it could be a helper class available to views where
the input is the map of values.  I could imagine something like QRCC
suggests where its a method (static?) on the Model.  Still, none of
this feels right.  I'm looking for something elegant and hope someone
out there has already worked through this as a design issue.

Thanks,
skylar

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Does calling model methods from elements break MVC pattern?

2008-10-05 Thread Kappa

First of all, i must admit that this discussion is becoming very
interesting, because i often find myself thinking if the way i'm
doing something with CakePHP is actually the right way.

My suggestion in using the after_find was just an example of
a restricted case, but I think that it's not the true correct way
to do things (because there could be a lot of situations in which
we cannot apply that, and there are also many cases in which
that solution is not efficient at all).

I think that the fact that the Model Classes are often used as
nothing more than an associative array, so sometimes i don't
consider those real classes; that's why i prefer to let access them
only from the controller.
And i even think that making some shortcut from the view to the
model, could create some problems with the routing/security/etc.
because in that way there are actually ACTIONS not controlled (nor
performed) by a CONTROLLER. For example if you then want to
make any statistic on those performed actions where can you put
 your code then? in the model? in the view? ..there is not the
controller.

Bye,
   Andrea

On Oct 5, 9:49 am, skylar [EMAIL PROTECTED] wrote:
 I'm really curious about this topic as well as it speaks to an aspect
 of the CakePHP Model class that has never really clicked with me.
 Andrea's example is perfect for explaining the point.

 In most MVC frameworks, when you retrieve data from storage (ram, db,
 file, etc), you usually get an instance of the model class itself, not
 a map/dictionary structure.  This makes it easy to fetch calculated
 values, such as full_name, age, etc.  In one respect, you could
 consider these helper functions, but I usually think of them as
 smart accessors - you store the minimal data necessary and calculate
 more common derived values on the fly (or you cache them in the rare
 chance it's worthwhile to do so).

 Andrea's suggestion (pre-calculating values in the after_find method)
 makes sense, but it assumes that the value is always worth calculating
 and storing after a lookup.  (Perhaps you don't always need the value,
 or calculating it takes a long time).  So what's the recommended
 convention for CakePHP for handling this stuff?  Better yet, what do
 some of you who are veterans with the framework do?

 I could imagine it could be a helper class available to views where
 the input is the map of values.  I could imagine something like QRCC
 suggests where its a method (static?) on the Model.  Still, none of
 this feels right.  I'm looking for something elegant and hope someone
 out there has already worked through this as a design issue.

 Thanks,
 skylar
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Does calling model methods from elements break MVC pattern?

2008-10-04 Thread Kappa

You are right, but the fact is that in this case we are merging
more principles, and the correct thing (as far as design is concerned)
in my opinion is to let the product to deal with its own
responsibilities
as well, but invoke those operations from the controller and not
from the view.
In that way you don't break nor the encapsulation principle, nor the
MVC pattern, do you agree?



On Oct 3, 5:31 pm, ORCC [EMAIL PROTECTED] wrote:
  ..the comment PERFORM BUSINESS LOGIC should immediately
  let you think that something is not 100% MVC ..you are
  performing LOGIC inside a MODEL, and not just model data
  handling.

 So, where I have to do the business logic?

 For example, I have a Product Object, and the product has some taxes
 that depends on the product itself (Its category, price, origin
 country, etc).

 Following the encapsulation principle of the object oriented
 programming, I think that the object to have to know how to calculate
 the product tax is the product itself. So I add a method
 Product::calculateTax(); into the class Product. Tax calculation is
 part of the business rules and tax value is inherent for each product,
 hence, I guess that the responsibility of tax calculation lays in
 the Product class.

 Why this could violate the MVC pattern? Or is the product controller
 who has to know how to calculate the product's tax?
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Does calling model methods from elements break MVC pattern?

2008-10-03 Thread Kappa

hi everybody,
I'm not a MVC guru,but in my opinion, the fact
that a view can import something and invoke
actions not performed by a controller, but from
a model, breaks somehow the MVC pattern.
Maybe if you need some action to manipulate data
of a model before display them, you should use
the afterfind of the model.

For example if you are handling Users, and you want
the full name with initials, but in the DB you have
name,surname and initials, you should put in after find
method something that bind together the 3 fields
and create a new key  $user['User']['fullname'].



Moreover since in your example you wrote this:

class Product extends AppModel {
 //... all attributes and methods

 function performSomeProductCalculation () {
   //perform business logic
   return $result;
 }

}

..the comment PERFORM BUSINESS LOGIC should immediately
let you think that something is not 100% MVC ..you are
performing LOGIC inside a MODEL, and not just model data
handling.

Anyway i don't want say that's wrong, but only that's not
100% MVC and in this way answering to your question.

Bye!!
  Andrea

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Does calling model methods from elements break MVC pattern?

2008-10-03 Thread Kappa

hi everybody,
I'm not a MVC guru,but in my opinion, the fact
that a view can import something and invoke
actions not performed by a controller, but from
a model, breaks somehow the MVC pattern.
Maybe if you need some action to manipulate data
of a model before display them, you should use
the afterfind of the model.

For example if you are handling Users, and you want
the full name with initials, but in the DB you have
name,surname and initials, you should put in after find
method something that bind together the 3 fields
and create a new key  $user['User']['fullname'].



Moreover since in your example you wrote this:

class Product extends AppModel {
 //... all attributes and methods

 function performSomeProductCalculation () {
   //perform business logic
   return $result;
 }

}

..the comment PERFORM BUSINESS LOGIC should immediately
let you think that something is not 100% MVC ..you are
performing LOGIC inside a MODEL, and not just model data
handling.

Anyway i don't want say that's wrong, but only that's not
100% MVC and in this way answering to your question.

Bye!!
  Andrea

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Does calling model methods from elements break MVC pattern?

2008-10-03 Thread ORCC


 ..the comment PERFORM BUSINESS LOGIC should immediately
 let you think that something is not 100% MVC ..you are
 performing LOGIC inside a MODEL, and not just model data
 handling.

So, where I have to do the business logic?

For example, I have a Product Object, and the product has some taxes
that depends on the product itself (Its category, price, origin
country, etc).

Following the encapsulation principle of the object oriented
programming, I think that the object to have to know how to calculate
the product tax is the product itself. So I add a method
Product::calculateTax(); into the class Product. Tax calculation is
part of the business rules and tax value is inherent for each product,
hence, I guess that the responsibility of tax calculation lays in
the Product class.

Why this could violate the MVC pattern? Or is the product controller
who has to know how to calculate the product's tax?
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Does calling model methods from elements break MVC pattern?

2008-10-02 Thread ORCC

It is a question about MVC pattern in cakephp.

I have a calculated value that I want to display in many views, so I
decided to create an element. The business logic has to be performed
in the model, i.e:

models/product.php

class Product extends AppModel {
 //... all attributes and methods

 function performSomeProductCalculation () {
   //perform business logic
   return $result;
 }
}

There are two options to create an element that renders the result
returned by the above method:

- Option 1: Call the model method directly from element:

views/elements/foo.ctp

?php
App::import('Model','Product');
$p = New Product();
?
div class=result
 The result is ?php echo $p-performSomeProductCalculation() ?
/div

- Option 2: Create a controller method for rendering the result, and
call that method in the element via requestAction

controllers/products_controller.php

class ProductsController extends AppController {
  //... all attributes and method

  function my_result() {
return $this-Product-performSomeProductCalculation();
   }
}


views/elements/foo.ctp

?php
$r = $this-requestAction('/products/my_result');
?
div class=result
 The result is ?php echo $r; ?
/div


Wich options is the correct one in order to respect 100% the MVC
pattern? ? ?
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Does calling model methods from elements break MVC pattern?

2008-10-02 Thread Sam Sherlock

 Does calling model methods from elements break MVC pattern?


yep.

Views are for laying out stuff.  controllers for pasing data to view and
models for obtaining data.

If your model passes the data to the controller so that the controller has
to process the data as little as possible for the view

http://gluei.com/blog/view/cakephp-best-practices-fat-models-and-skinny-controllers
http://cakebaker.42dh.com/2008/01/19/fat-models-and-how-they-change-how-you-use-the-model-class/

There are some performance drawbacks with using requestaction like that.

Mark Story provides details of a solution around requestaction (though this
method also has its critics)
http://mark-story.com/Posts/view/reducing-requestaction-use-in-your-cakephp-sites-with-fat-models

hth -S


2008/10/2 ORCC [EMAIL PROTECTED]


 It is a question about MVC pattern in cakephp.

 I have a calculated value that I want to display in many views, so I
 decided to create an element. The business logic has to be performed
 in the model, i.e:

 models/product.php

 class Product extends AppModel {
 //... all attributes and methods

 function performSomeProductCalculation () {
   //perform business logic
   return $result;
 }
 }

 There are two options to create an element that renders the result
 returned by the above method:

 - Option 1: Call the model method directly from element:

 views/elements/foo.ctp

 ?php
 App::import('Model','Product');
 $p = New Product();
 ?
 div class=result
 The result is ?php echo $p-performSomeProductCalculation() ?
 /div

 - Option 2: Create a controller method for rendering the result, and
 call that method in the element via requestAction

 controllers/products_controller.php

 class ProductsController extends AppController {
  //... all attributes and method

  function my_result() {
return $this-Product-performSomeProductCalculation();
   }
 }


 views/elements/foo.ctp

 ?php
 $r = $this-requestAction('/products/my_result');
 ?
 div class=result
 The result is ?php echo $r; ?
 /div


 Wich options is the correct one in order to respect 100% the MVC
 pattern? ? ?
 


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Does calling model methods from elements break MVC pattern?

2008-10-02 Thread Samuel DeVore

 Mark Story provides details of a solution around requestaction (though this
 method also has its critics)
 http://mark-story.com/Posts/view/reducing-requestaction-use-in-your-cakephp-sites-with-fat-models


yea but since it is MarkStory Day you can't critique him right now.
So using that method right now should be perfect ;)

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Does calling model methods from elements break MVC pattern?

2008-10-02 Thread ORCC

Ok, that means that I would consider to pass parameters to my elements
(parameters calculated inside the controller) to avoid a requestAction
call, doesn't it?


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---