Re: To model or not to model

2008-09-25 Thread Rafael Bandeira aka rafaelbandeira3

> > Still thinks that executing queries is needed?
>
> Yes. Sometimes You have uncommon database design, and You can't
> EFFECTIVELY receive data just by using standard, even magic, model

Definetly correct, my sentence wasn't much happy. I meant for the
cases he presented.
Anyway, keep in mind that you should - as a good design principle -
encapsulate hand made sql queries in Model methods,
so you'll avoid having data fetching logic in your Controller. And
take a good look at the built-in query structure, it covers lots of
usages
I got pretty complex queries for data reporting and they're all using
cake's query structure.

rafaelbandeira3
http://rafaelbandeira3.wordpress.com
--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-25 Thread Grzegorz Pawlik

> Still thinks that executing queries is needed?
Yes. Sometimes You have uncommon database design, and You can't
EFFECTIVELY receive data just by using standard, even magic, model
functions. In that case you should create method in model that
executes your custom query with inner, outer, left joins, subqueries
and other fancy stuff :)
But I agree that one should check if it's possible to do this with
standard model, and then if isn't - write some queries.
In my team I has some people that likes sql so much, that $this->Model-
>query was substitute even for findAll (sic!). I spent quite a long
time explaining why it's not good.


On Sep 24, 1:32 pm, Rafael Bandeira aka rafaelbandeira3
<[EMAIL PROTECTED]> wrote:
> First of all people: what Rule? It's not made of rules, it's a pattern
> to follow - but follow as you want.
> Second, no, it doesn't "break the Rule" it only makes your code uglier
> and hard to read - see above - but the thing is,
> why to use a hand made query when you can easily/prettier/"readablier"
> use Model built-in methods to accomplish the same?
>
> About performing custom find methods, with default/mandatory/pre-
> defined params you should read:
>
> >  http://rafaelbandeira3.wordpress.com/2008/08/28/the-new-way-to-define...
> >  http://cakebaker.42dh.com/2008/09/23/an-alternative-way-to-define-cus...
>
> About the rules:
> Even being annoying, executing hand made queries on the controller
> still leaves all the data transaction and structuration on the Model
> side,
> and it lets the pattern intended to be followed intact, but it's not
> the best practice. If even following the links presented - and links
> cited inside them, you
> still not find the solution for a more complex and featured query, try
> to implement it inside a model method to make your controller cleaner:
>
> $this->Model->find("where id=xyz")
> becomes
> $this->Model->find('axis', array('id' => 'xyz')); // following those
> links
> or
> $this->Model->getByAxis($axis);
>
> But in the specific case you presented, you could easily use Model
> methods overload by calling $this->Model->findById($id), $this->Model-
>
> >findBy($fieldValue);
>
> Still thinks that executing queries is needed?
>
> rafaelbandeira3http://rafaelbandeira3.wordpress.com
>
> > but i heared if you direct query in controller will violate the MVC
> > rule
>
> > i.e if you use $this->Model->query("select * from models");
>
> > the above query will violate the MVC rule.
>
> > i heared the queries shouldnotbe written in controller it should be
> > inmodeli.e in urmodel.php
>
> > even i am alsonotabsolute on this part
>
> > On Sep 23, 2:33 pm, forrestgump <[EMAIL PROTECTED]> wrote:
>
> > > Hey guys,
> > >  Considering the fact that CakePhp follows the MVC architecture, id
> > > like to know...y we dont make function calls such as $this->find("where 
> > > id=xyz") from function blocks typed out in Models rather
>
> > > than controllersim sure there is a logical reason to it...but
> > > someone asked me this question and i couldnotgive her a good enuf
> > > answerdoes making function calls to queries like find(), save()
> > > work better in models or controllers?.and is it good practice to
> > > pass conditions in your function call while using it in a controller?
> > > eg find("where id=1") ;
>
> > > Does using $this->find() in controller violate the MVC architecture in
> > > any manner?
>
> > > Can someone please clarify these question for us?
>
> > > Thanks in advance,
> > > forrestgump
--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-24 Thread Rafael Bandeira aka rafaelbandeira3

>
> Rule of thumb perhaps? :)
>

If you really mean it: Let's say rule of thumb isn't exactly a rule.
Now, if you were being ironic: Yay right, that's the kind of rule that
can be applied to a 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: To model or not to model

2008-09-24 Thread dr. Hannibal Lecter
On Wed, Sep 24, 2008 at 1:32 PM, Rafael Bandeira aka rafaelbandeira3 <
[EMAIL PROTECTED]> wrote:

>
> First of all people: what Rule?


Rule of thumb perhaps? :)

-- 
Misanthropy unleashed:
http://dsi.vozibrale.com/

--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-24 Thread forrestgump

Ohk...sry rafael...i did not read the entire thread

On Sep 24, 5:17 pm, forrestgump <[EMAIL PROTECTED]> wrote:
> I dont think i asked anything bout using queries..i just wanted to
> know wht would be the best practice..but thnx for the info...will
> avoid $this->Model->query()
>
> On Sep 24, 4:32 pm, Rafael Bandeira aka rafaelbandeira3
>
> <[EMAIL PROTECTED]> wrote:
> > First of all people: what Rule? It's not made of rules, it's a pattern
> > to follow - but follow as you want.
> > Second, no, it doesn't "break the Rule" it only makes your code uglier
> > and hard to read - see above - but the thing is,
> > why to use a hand made query when you can easily/prettier/"readablier"
> > use Model built-in methods to accomplish the same?
>
> > About performing custom find methods, with default/mandatory/pre-
> > defined params you should read:
>
> > >  http://rafaelbandeira3.wordpress.com/2008/08/28/the-new-way-to-define...
> > >  http://cakebaker.42dh.com/2008/09/23/an-alternative-way-to-define-cus...
>
> > About the rules:
> > Even being annoying, executing hand made queries on the controller
> > still leaves all the data transaction and structuration on the Model
> > side,
> > and it lets the pattern intended to be followed intact, but it's not
> > the best practice. If even following the links presented - and links
> > cited inside them, you
> > still not find the solution for a more complex and featured query, try
> > to implement it inside a model method to make your controller cleaner:
>
> > $this->Model->find("where id=xyz")
> > becomes
> > $this->Model->find('axis', array('id' => 'xyz')); // following those
> > links
> > or
> > $this->Model->getByAxis($axis);
>
> > But in the specific case you presented, you could easily use Model
> > methods overload by calling $this->Model->findById($id), $this->Model-
>
> > >findBy($fieldValue);
>
> > Still thinks that executing queries is needed?
>
> > rafaelbandeira3http://rafaelbandeira3.wordpress.com
>
> > > but i heared if you direct query in controller will violate the MVC
> > > rule
>
> > > i.e if you use $this->Model->query("select * from models");
>
> > > the above query will violate the MVC rule.
>
> > > i heared the queries shouldnotbe written in controller it should be
> > > inmodeli.e in urmodel.php
>
> > > even i am alsonotabsolute on this part
>
> > > On Sep 23, 2:33 pm, forrestgump <[EMAIL PROTECTED]> wrote:
>
> > > > Hey guys,
> > > >  Considering the fact that CakePhp follows the MVC architecture, id
> > > > like to know...y we dont make function calls such as $this->find("where 
> > > > id=xyz") from function blocks typed out in Models rather
>
> > > > than controllersim sure there is a logical reason to it...but
> > > > someone asked me this question and i couldnotgive her a good enuf
> > > > answerdoes making function calls to queries like find(), save()
> > > > work better in models or controllers?.and is it good practice to
> > > > pass conditions in your function call while using it in a controller?
> > > > eg find("where id=1") ;
>
> > > > Does using $this->find() in controller violate the MVC architecture in
> > > > any manner?
>
> > > > Can someone please clarify these question for us?
>
> > > > Thanks in advance,
> > > > forrestgump
--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-24 Thread forrestgump

I dont think i asked anything bout using queries..i just wanted to
know wht would be the best practice..but thnx for the info...will
avoid $this->Model->query()

On Sep 24, 4:32 pm, Rafael Bandeira aka rafaelbandeira3
<[EMAIL PROTECTED]> wrote:
> First of all people: what Rule? It's not made of rules, it's a pattern
> to follow - but follow as you want.
> Second, no, it doesn't "break the Rule" it only makes your code uglier
> and hard to read - see above - but the thing is,
> why to use a hand made query when you can easily/prettier/"readablier"
> use Model built-in methods to accomplish the same?
>
> About performing custom find methods, with default/mandatory/pre-
> defined params you should read:
>
> >  http://rafaelbandeira3.wordpress.com/2008/08/28/the-new-way-to-define...
> >  http://cakebaker.42dh.com/2008/09/23/an-alternative-way-to-define-cus...
>
> About the rules:
> Even being annoying, executing hand made queries on the controller
> still leaves all the data transaction and structuration on the Model
> side,
> and it lets the pattern intended to be followed intact, but it's not
> the best practice. If even following the links presented - and links
> cited inside them, you
> still not find the solution for a more complex and featured query, try
> to implement it inside a model method to make your controller cleaner:
>
> $this->Model->find("where id=xyz")
> becomes
> $this->Model->find('axis', array('id' => 'xyz')); // following those
> links
> or
> $this->Model->getByAxis($axis);
>
> But in the specific case you presented, you could easily use Model
> methods overload by calling $this->Model->findById($id), $this->Model-
>
> >findBy($fieldValue);
>
> Still thinks that executing queries is needed?
>
> rafaelbandeira3http://rafaelbandeira3.wordpress.com
>
> > but i heared if you direct query in controller will violate the MVC
> > rule
>
> > i.e if you use $this->Model->query("select * from models");
>
> > the above query will violate the MVC rule.
>
> > i heared the queries shouldnotbe written in controller it should be
> > inmodeli.e in urmodel.php
>
> > even i am alsonotabsolute on this part
>
> > On Sep 23, 2:33 pm, forrestgump <[EMAIL PROTECTED]> wrote:
>
> > > Hey guys,
> > >  Considering the fact that CakePhp follows the MVC architecture, id
> > > like to know...y we dont make function calls such as $this->find("where 
> > > id=xyz") from function blocks typed out in Models rather
>
> > > than controllersim sure there is a logical reason to it...but
> > > someone asked me this question and i couldnotgive her a good enuf
> > > answerdoes making function calls to queries like find(), save()
> > > work better in models or controllers?.and is it good practice to
> > > pass conditions in your function call while using it in a controller?
> > > eg find("where id=1") ;
>
> > > Does using $this->find() in controller violate the MVC architecture in
> > > any manner?
>
> > > Can someone please clarify these question for us?
>
> > > Thanks in advance,
> > > forrestgump
--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-24 Thread Rafael Bandeira aka rafaelbandeira3

First of all people: what Rule? It's not made of rules, it's a pattern
to follow - but follow as you want.
Second, no, it doesn't "break the Rule" it only makes your code uglier
and hard to read - see above - but the thing is,
why to use a hand made query when you can easily/prettier/"readablier"
use Model built-in methods to accomplish the same?

About performing custom find methods, with default/mandatory/pre-
defined params you should read:

>  
> http://rafaelbandeira3.wordpress.com/2008/08/28/the-new-way-to-define-find-methods/
>  
> http://cakebaker.42dh.com/2008/09/23/an-alternative-way-to-define-custom-find-types/

About the rules:
Even being annoying, executing hand made queries on the controller
still leaves all the data transaction and structuration on the Model
side,
and it lets the pattern intended to be followed intact, but it's not
the best practice. If even following the links presented - and links
cited inside them, you
still not find the solution for a more complex and featured query, try
to implement it inside a model method to make your controller cleaner:

$this->Model->find("where id=xyz")
becomes
$this->Model->find('axis', array('id' => 'xyz')); // following those
links
or
$this->Model->getByAxis($axis);

But in the specific case you presented, you could easily use Model
methods overload by calling $this->Model->findById($id), $this->Model-
>findBy($fieldValue);

Still thinks that executing queries is needed?

rafaelbandeira3
http://rafaelbandeira3.wordpress.com

> but i heared if you direct query in controller will violate the MVC
> rule
>
> i.e if you use $this->Model->query("select * from models");
>
> the above query will violate the MVC rule.
>
> i heared the queries shouldnotbe written in controller it should be
> inmodeli.e in urmodel.php
>
> even i am alsonotabsolute on this part
>
> On Sep 23, 2:33 pm, forrestgump <[EMAIL PROTECTED]> wrote:
>
> > Hey guys,
> >  Considering the fact that CakePhp follows the MVC architecture, id
> > like to know...y we dont make function calls such as $this->find("where 
> > id=xyz") from function blocks typed out in Models rather
>
> > than controllersim sure there is a logical reason to it...but
> > someone asked me this question and i couldnotgive her a good enuf
> > answerdoes making function calls to queries like find(), save()
> > work better in models or controllers?.and is it good practice to
> > pass conditions in your function call while using it in a controller?
> > eg find("where id=1") ;
>
> > Does using $this->find() in controller violate the MVC architecture in
> > any manner?
>
> > Can someone please clarify these question for us?
>
> > Thanks in advance,
> > forrestgump
--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-23 Thread forrestgump

Thanks for the info guys...really appreciate it
@RichardAtHome.whoops i forgot to mention the model
inbetween"typo"...thnx for pointing tht out

Forrestgump

On Sep 23, 2:54 pm, techiguy <[EMAIL PROTECTED]> wrote:
> hi forrestgump,
>
> using $this->find() in controller doesn't voilate the MVC architecture
> even if you use $this->find('all')
>
> but i heared if you direct query in controller will violate the MVC
> rule
>
> i.e if you use $this->Model->query("select * from models");
>
> the above query will violate the MVC rule.
>
> i heared the queries should not be written in controller it should be
> in model i.e in ur model.php
>
> even i am also not absolute on this part
>
> On Sep 23, 2:33 pm, forrestgump <[EMAIL PROTECTED]> wrote:
>
> > Hey guys,
> >  Considering the fact that CakePhp follows the MVC architecture, id
> > like to know...y we dont make function calls such as $this->find("where 
> > id=xyz") from function blocks typed out in Models rather
>
> > than controllersim sure there is a logical reason to it...but
> > someone asked me this question and i could not give her a good enuf
> > answerdoes making function calls to queries like find(), save()
> > work better in models or controllers?.and is it good practice to
> > pass conditions in your function call while using it in a controller?
> > eg find("where id=1") ;
>
> > Does using $this->find() in controller violate the MVC architecture in
> > any manner?
>
> > Can someone please clarify these question for us?
>
> > Thanks in advance,
> > forrestgump
--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-23 Thread techiguy

hi forrestgump,

using $this->find() in controller doesn't voilate the MVC architecture
even if you use $this->find('all')

but i heared if you direct query in controller will violate the MVC
rule

i.e if you use $this->Model->query("select * from models");

the above query will violate the MVC rule.

i heared the queries should not be written in controller it should be
in model i.e in ur model.php

even i am also not absolute on this part

On Sep 23, 2:33 pm, forrestgump <[EMAIL PROTECTED]> wrote:
> Hey guys,
>  Considering the fact that CakePhp follows the MVC architecture, id
> like to know...y we dont make function calls such as $this->find("where 
> id=xyz") from function blocks typed out in Models rather
>
> than controllersim sure there is a logical reason to it...but
> someone asked me this question and i could not give her a good enuf
> answerdoes making function calls to queries like find(), save()
> work better in models or controllers?.and is it good practice to
> pass conditions in your function call while using it in a controller?
> eg find("where id=1") ;
>
> Does using $this->find() in controller violate the MVC architecture in
> any manner?
>
> Can someone please clarify these question for us?
>
> Thanks in advance,
> forrestgump

--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-23 Thread RichardAtHome

Also worth noting: A controller can have multiple Models.

Which model would $this->find() use?

On Sep 23, 12:28 pm, "dr. Hannibal Lecter" <[EMAIL PROTECTED]>
wrote:
> Well, calling find() in a controller is not a violation, but not "the
> best practice" if overused.
>
> Of course, calling find() from a controller is the fastest way to get
> your data, but usually, the find() call has other stuff around it,
> stuff that might be common to each call. The idea is to make "fat
> models, thin controllers", meaning that you put as much as you can in
> your models.
>
> Maybe an example is better. Let's say you have a certain criteria for
> getting an article from the Article model. You can call this from a
> controller each time you want to get an article:
>
> $article = $this->Article->find
>         (
>                 'first',
>                 array
>                 (
>                         'conditions' => array('Article.slug' => $slug),
>                         'contain' => array('ArticleCategory', 'Rating')
>                 )
>         );
>
> or you can make it cleaner and easier by declaring a function in your
> model, for example "getArticle":
>
> function getArticle($slug)
> {
>         $_slug = Sanitize::escape($slug);
>
>         return $this->find
>                 (
>                         'first',
>                         array
>                         (
>                                 'conditions' => array('Article.slug' => 
> $_slug),
>                                 'contain' => array('ArticleCategory', 
> 'Rating')
>                         )
>                 );
>
> }
>
> this makes your controller much thinner and easier to follow:
>
> $article = $this->Article->getSingle($slug);
>
> All you have to remember is that all apps work with some sort of data,
> and in Cake, models work with data, not controllers.
>
> I hope that helps a bit. ;)
>
> On Sep 23, 11:33 am, forrestgump <[EMAIL PROTECTED]> wrote:
>
> > Hey guys,
> >  Considering the fact that CakePhp follows the MVC architecture, id
> > like to know...y we dont make function calls such as $this->find("where 
> > id=xyz") from function blocks typed out in Models rather
>
> > than controllersim sure there is a logical reason to it...but
> > someone asked me this question and i could not give her a good enuf
> > answerdoes making function calls to queries like find(), save()
> > work better in models or controllers?.and is it good practice to
> > pass conditions in your function call while using it in a controller?
> > eg find("where id=1") ;
>
> > Does using $this->find() in controller violate the MVC architecture in
> > any manner?
>
> > Can someone please clarify these question for us?
>
> > Thanks in advance,
> > forrestgump
--~--~-~--~~~---~--~~
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: To model or not to model

2008-09-23 Thread dr. Hannibal Lecter

Well, calling find() in a controller is not a violation, but not "the
best practice" if overused.

Of course, calling find() from a controller is the fastest way to get
your data, but usually, the find() call has other stuff around it,
stuff that might be common to each call. The idea is to make "fat
models, thin controllers", meaning that you put as much as you can in
your models.

Maybe an example is better. Let's say you have a certain criteria for
getting an article from the Article model. You can call this from a
controller each time you want to get an article:

$article = $this->Article->find
(
'first',
array
(
'conditions' => array('Article.slug' => $slug),
'contain' => array('ArticleCategory', 'Rating')
)
);

or you can make it cleaner and easier by declaring a function in your
model, for example "getArticle":

function getArticle($slug)
{
$_slug = Sanitize::escape($slug);

return $this->find
(
'first',
array
(
'conditions' => array('Article.slug' => $_slug),
'contain' => array('ArticleCategory', 'Rating')
)
);
}

this makes your controller much thinner and easier to follow:

$article = $this->Article->getSingle($slug);

All you have to remember is that all apps work with some sort of data,
and in Cake, models work with data, not controllers.

I hope that helps a bit. ;)

On Sep 23, 11:33 am, forrestgump <[EMAIL PROTECTED]> wrote:
> Hey guys,
>  Considering the fact that CakePhp follows the MVC architecture, id
> like to know...y we dont make function calls such as $this->find("where 
> id=xyz") from function blocks typed out in Models rather
>
> than controllersim sure there is a logical reason to it...but
> someone asked me this question and i could not give her a good enuf
> answerdoes making function calls to queries like find(), save()
> work better in models or controllers?.and is it good practice to
> pass conditions in your function call while using it in a controller?
> eg find("where id=1") ;
>
> Does using $this->find() in controller violate the MVC architecture in
> any manner?
>
> Can someone please clarify these question for us?
>
> Thanks in advance,
> forrestgump
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---