why not just the data? Also your case specifies (as far as I
understand) a single person you want returned, so you really just want
a single entry, or a failure handling (null return is popular for
that).
Then you create two functions where one would do (you duplicate code).
class Model_Subscriber {
public function getSubscriber($email, $active = false) {
// your logic to make the query here
// query the db
// get the result
// if something is wrong, return null, otherwise return the values
}
}
Now this is just written from the top of my head. There is more to
this, like a gateway class you could also use, and tons of varying
preferences on how to handle models. You can find tons of blog posts
about this and application layers everywhere on the net (not only
about PHP).
Good luck :)
On Thu, Dec 23, 2010 at 2:29 PM, Fernando Morgenstern
<contato@fernandomarcelo.com> wrote:
> Hello,
>
> I am looking for some input about best practices in models/controllers.
>
> The application is still pretty simple, but i would like to make it right from the beginning. I have two actions:
>
> 1) I have to select a specified subscriber that is active ( active = 1 in database )
> 2) I have to select a specified subscriber, it does not matter if active or not.
>
> My initial approach was this in controller:
>
> $model = new Model_Subscriber();
> $model->fetchAll(array(
> 'email = ?' => $email,
> 'active = 1'
> ));
>
> I soon realized that this is bad since i am writing SQL and business logic in controller. So i created two functions in model:
>
> 1) getActiveSubscriberQuery($email)
> 2) getSubscriberQuery($email)
>
> Both return a Zend_Db_Select, which i can use in fetchAll().
>
> My question: Is this the best way to do things? I mean, yhe application might get complex and i am worried about having lots and lots of functions for each type of query in model.
>
> Regards,
>
> Fernando Morgenstern
> contato@fernandomarcelo.com
>
>
>
>
没有评论:
发表评论