Re: Using model objects in the controllers

2008-01-07 Thread Deane

Good enough.  I'll concede this one, then.

Thanks, everyone, for your input.

Deane


On Jan 7, 5:21 pm, AD7six <[EMAIL PROTECTED]> wrote:
> On Jan 7, 11:35 pm, Deane <[EMAIL PROTECTED]> wrote:
>
> > Assume this scenario:
>
> > I need to retrieve 20 records, and call a method on each of them.  So
> > I do my Find, and get back an array.  I then spin this, and do this:
>
> > $this->Widget->read(null, $id);
>
> > What has just happened from a SQL perspective?  21 separate SQL
> > executions?  (One for the loop, and 20 to hydrate the resulting
> > objects, one-by-one...)
>
> Are you deliberately trying to be difficult ;).
>
> $data = $this->Widget->find('all');
> foreach ($data as $results) {
> $this->Widget->create($result);
> $this->Widget->stuff();
>
> }
>
> Total sql calls: 1
>
> And that's unless you write stuff() to be able to batch process data.
>
> I feel it's time you either showed a real example of what you /can't/
> do, or at least try things the cake way.
>
> AD
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread AD7six



On Jan 7, 11:35 pm, Deane <[EMAIL PROTECTED]> wrote:
> Assume this scenario:
>
> I need to retrieve 20 records, and call a method on each of them.  So
> I do my Find, and get back an array.  I then spin this, and do this:
>
> $this->Widget->read(null, $id);
>
> What has just happened from a SQL perspective?  21 separate SQL
> executions?  (One for the loop, and 20 to hydrate the resulting
> objects, one-by-one...)

Are you deliberately trying to be difficult ;).

$data = $this->Widget->find('all');
foreach ($data as $results) {
$this->Widget->create($result);
$this->Widget->stuff();
}

Total sql calls: 1

And that's unless you write stuff() to be able to batch process data.

I feel it's time you either showed a real example of what you /can't/
do, or at least try things the cake way.

AD
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread Deane

Assume this scenario:

I need to retrieve 20 records, and call a method on each of them.  So
I do my Find, and get back an array.  I then spin this, and do this:

$this->Widget->read(null, $id);

What has just happened from a SQL perspective?  21 separate SQL
executions?  (One for the loop, and 20 to hydrate the resulting
objects, one-by-one...)

Deane



On Jan 7, 4:12 pm, the_woodsman <[EMAIL PROTECTED]> wrote:
> I had similar feelings to this when I first had to get some complex
> data through CakePHP, thinking the results of a simple mysql_query or
> an object would be much easier to work with than a 5 level deep array!
>
> >>Yet I still have nothing but these big arrays to deal with.
>
> That's the key point, and there are other ways to deal with it apart
> from getting objects back from your results:
>
> -- Use generateList where you can
>
> generateList returns a simple array, and takes various params- it's
> surprisingly powerful.
>
> -- Filter the results of your cake queries to make the arrays simpler
>
> The set class can help return simpler arrays  (see the thinkingphp
> blog for a good post on this), there's also a behaviour in the bakery
> for simpler array results, and (perhaps in response to your previous
> post) someone recently linked to a way to objectify the result arrays.
>
> -- run sql manually via model->query() in a method in your model,
> like
> ::getSimpleResultsForMyBigQuery(), and return the data just how you'd
> like.
>
> -- use SQL views: often a SQL view is a nice way to take complex
> queries out of your code.
>
> I hope with all those tools at your disposal, the use of results
> arrays won't be a 'deal breaker' for you - It wasn't for me.
>
> On Jan 7, 8:42 pm, Robby Anderson <[EMAIL PROTECTED]> wrote:
>
> > Two ways jump out at me right off the bat. First, think of the default
> > cake Model as a data access class only, and then define a business-
> > logic class (that doesn't extend AppModel) to encapsulate your desired
> > model functionality.
>
> > // Get results from data access model
> > $results = $this->SomeModel->findWhatever();
>
> > // Instantiate business logic class, assigning results to it, and then
> > slice bread
> > $myModel = new MyModelWithBusinessLogic($results);
> > $myModel->sliceBread();
>
> > Or just use afterFind() in a model, assigning the results to a model
> > property, and build your business logic into the model that way.
>
> > function afterFind( $results ) {
> > $this->_results = $results;
>
> > }
>
> > function sliceBread() {
> > if ( empty($this->_results) {
> > return false;
> > }
>
> > // slice bread!
>
> > }
>
> > $this->SomeModel->findWhatever();
> > $this->SomeModel->sliceBread();
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread the_woodsman

I had similar feelings to this when I first had to get some complex
data through CakePHP, thinking the results of a simple mysql_query or
an object would be much easier to work with than a 5 level deep array!

>>Yet I still have nothing but these big arrays to deal with.

That's the key point, and there are other ways to deal with it apart
from getting objects back from your results:

-- Use generateList where you can

generateList returns a simple array, and takes various params- it's
surprisingly powerful.

-- Filter the results of your cake queries to make the arrays simpler

The set class can help return simpler arrays  (see the thinkingphp
blog for a good post on this), there's also a behaviour in the bakery
for simpler array results, and (perhaps in response to your previous
post) someone recently linked to a way to objectify the result arrays.

-- run sql manually via model->query() in a method in your model,
like
::getSimpleResultsForMyBigQuery(), and return the data just how you'd
like.

-- use SQL views: often a SQL view is a nice way to take complex
queries out of your code.

I hope with all those tools at your disposal, the use of results
arrays won't be a 'deal breaker' for you - It wasn't for me.


On Jan 7, 8:42 pm, Robby Anderson <[EMAIL PROTECTED]> wrote:
> Two ways jump out at me right off the bat. First, think of the default
> cake Model as a data access class only, and then define a business-
> logic class (that doesn't extend AppModel) to encapsulate your desired
> model functionality.
>
> // Get results from data access model
> $results = $this->SomeModel->findWhatever();
>
> // Instantiate business logic class, assigning results to it, and then
> slice bread
> $myModel = new MyModelWithBusinessLogic($results);
> $myModel->sliceBread();
>
> Or just use afterFind() in a model, assigning the results to a model
> property, and build your business logic into the model that way.
>
> function afterFind( $results ) {
>     $this->_results = $results;
>
> }
>
> function sliceBread() {
>     if ( empty($this->_results) {
>         return false;
>     }
>
>     // slice bread!
>
> }
>
> $this->SomeModel->findWhatever();
> $this->SomeModel->sliceBread();
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread Robby Anderson


Two ways jump out at me right off the bat. First, think of the default
cake Model as a data access class only, and then define a business-
logic class (that doesn't extend AppModel) to encapsulate your desired
model functionality.

// Get results from data access model
$results = $this->SomeModel->findWhatever();

// Instantiate business logic class, assigning results to it, and then
slice bread
$myModel = new MyModelWithBusinessLogic($results);
$myModel->sliceBread();


Or just use afterFind() in a model, assigning the results to a model
property, and build your business logic into the model that way.

function afterFind( $results ) {
$this->_results = $results;
}

function sliceBread() {
if ( empty($this->_results) {
return false;
}

// slice bread!

}


$this->SomeModel->findWhatever();
$this->SomeModel->sliceBread();


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread AD7six



On Jan 7, 9:26 pm, Deane <[EMAIL PROTECTED]> wrote:
> That example you gave -- is that a static method on the class?  What
> if I want to instantiate a specific Widget object (from a specific row
> in the database) and run an instance method on it?

$this->Model->read(null, $id);
$this->Model->funkylogic();

in your model

function funkylogic() {
// refer to $this->data
}

hth,

AD
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread Deane

That example you gave -- is that a static method on the class?  What
if I want to instantiate a specific Widget object (from a specific row
in the database) and run an instance method on it?


On Jan 7, 10:32 am, kristofer <[EMAIL PROTECTED]> wrote:
> The models are available in the controller..
>
> $results = $this->ModelName-
>
> >someFunctionYouWroteToGetSomDataBackFromTheDb();
>
> unfortunately cake returns an array of results instead of an object..
> so there no doing something like..
> $results->sortBy('date ASC');
>
> On Jan 7, 7:38 am, Deane <[EMAIL PROTECTED]> wrote:
>
> > A couple days ago, I lamented that Find* didn't return an array of
> > model objects.  I posted about it here, and I was told that one of the
> > reasons for this was that it would be "too tempting" to start
> > inserting business logic in the views if you had objects available
> > there.
>
> > Fine.  I'll concede that point for now.
>
> > But what about the controllers?  Shouldn't I be able to use models in
> > the controllers?  If I do a Find* in a controller to retrieve some
> > data (that I have no intention of sending to the view) , shouldn't I
> > get model objects back and be able to use methods on those objects?
>
> > Controllers are where the business logic *should* live, so there
> > doesn't seem to be much argument against using model objects there.
> > Yet I still have nothing but these big arrays to deal with.
>
> > How does everyone else manage this?  Say you *need* to run some method
> > on a model object in one of your controllers.  What is the accepted
> > way of doing this?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread kristofer

The models are available in the controller..

$results = $this->ModelName-
>someFunctionYouWroteToGetSomDataBackFromTheDb();


unfortunately cake returns an array of results instead of an object..
so there no doing something like..
$results->sortBy('date ASC');

On Jan 7, 7:38 am, Deane <[EMAIL PROTECTED]> wrote:
> A couple days ago, I lamented that Find* didn't return an array of
> model objects.  I posted about it here, and I was told that one of the
> reasons for this was that it would be "too tempting" to start
> inserting business logic in the views if you had objects available
> there.
>
> Fine.  I'll concede that point for now.
>
> But what about the controllers?  Shouldn't I be able to use models in
> the controllers?  If I do a Find* in a controller to retrieve some
> data (that I have no intention of sending to the view) , shouldn't I
> get model objects back and be able to use methods on those objects?
>
> Controllers are where the business logic *should* live, so there
> doesn't seem to be much argument against using model objects there.
> Yet I still have nothing but these big arrays to deal with.
>
> How does everyone else manage this?  Say you *need* to run some method
> on a model object in one of your controllers.  What is the accepted
> way of doing this?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread Chris Hartjes

On Jan 7, 2008 10:29 AM, Deane <[EMAIL PROTECTED]> wrote:
>
> If I can't get clean access to model objects in my controllers, how am
> I to do this?
>
> Deane

I don't understand why it's so hard to accept that a call to a CakePHP
DB model returns an array of results?  I may have taken computer
science at college a long time ago, but I don't remember there being a
rule that a model can only return a model.

In cake, you usually get results from your models in your controller like this:

$results = $this->ModelName->findAllByFoo($foo);

And now $results contains a nice nested array.  I might not be sharing
the same definition as you, but that looks like "clean access to model
objects" to me.

Perhaps if you were to give a coding example it would help us to
understand the problem you are facing.

-- 
Chris Hartjes
Internet Loudmouth
Motto for 2008: "Moving from herding elephants to handling snakes..."
@TheKeyBoard: http://www.littlehart.net/atthekeyboard

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread Deane

MyTufty:

>the models should be responsible for the business logic, not the controllers

Fine, I'll concede that point and code up a ton of business logic in
my models.

Still, my controller needs to call the business logic at some point.
The controller needs to say, "Hey, model, do *this*..."

If I can't get clean access to model objects in my controllers, how am
I to do this?

Deane


On Jan 7, 8:14 am, MrTufty <[EMAIL PROTECTED]> wrote:
> Deane,
>
> I have to disagree with your opinion that the business logic belongs
> in the controllers. From my understanding of MVC best practices,
> specifically "Fat models, skinny controllers" - the models should be
> responsible for the business logic, not the controllers. The
> controllers are pretty much only there in order to wire together
> models and views.
>
> But that said, it is only my opinion, and I'd be interested to know
> why you think differently.
>
> On Jan 7, 12:38 pm, Deane <[EMAIL PROTECTED]> wrote:
>
> > A couple days ago, I lamented that Find* didn't return an array of
> > model objects.  I posted about it here, and I was told that one of the
> > reasons for this was that it would be "too tempting" to start
> > inserting business logic in the views if you had objects available
> > there.
>
> > Fine.  I'll concede that point for now.
>
> > But what about the controllers?  Shouldn't I be able to use models in
> > the controllers?  If I do a Find* in a controller to retrieve some
> > data (that I have no intention of sending to the view) , shouldn't I
> > get model objects back and be able to use methods on those objects?
>
> > Controllers are where the business logic *should* live, so there
> > doesn't seem to be much argument against using model objects there.
> > Yet I still have nothing but these big arrays to deal with.
>
> > How does everyone else manage this?  Say you *need* to run some method
> > on a model object in one of your controllers.  What is the accepted
> > way of doing this?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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: Using model objects in the controllers

2008-01-07 Thread MrTufty

Deane,

I have to disagree with your opinion that the business logic belongs
in the controllers. From my understanding of MVC best practices,
specifically "Fat models, skinny controllers" - the models should be
responsible for the business logic, not the controllers. The
controllers are pretty much only there in order to wire together
models and views.

But that said, it is only my opinion, and I'd be interested to know
why you think differently.

On Jan 7, 12:38 pm, Deane <[EMAIL PROTECTED]> wrote:
> A couple days ago, I lamented that Find* didn't return an array of
> model objects.  I posted about it here, and I was told that one of the
> reasons for this was that it would be "too tempting" to start
> inserting business logic in the views if you had objects available
> there.
>
> Fine.  I'll concede that point for now.
>
> But what about the controllers?  Shouldn't I be able to use models in
> the controllers?  If I do a Find* in a controller to retrieve some
> data (that I have no intention of sending to the view) , shouldn't I
> get model objects back and be able to use methods on those objects?
>
> Controllers are where the business logic *should* live, so there
> doesn't seem to be much argument against using model objects there.
> Yet I still have nothing but these big arrays to deal with.
>
> How does everyone else manage this?  Say you *need* to run some method
> on a model object in one of your controllers.  What is the accepted
> way of doing this?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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
-~--~~~~--~~--~--~---