PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Oct 14, 2019 8:53 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Tue Oct 09, 2012 6:49 am 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia
So, I have always thought that a model should have a bunch of getters to get exactly what was needed out the DB, no more and no less. When all I needed was the column "message" I would always have something like:

Syntax: [ Download ] [ Hide ]
$thing = $model
->select()
->from($model, 'field_name')
->where('id = ?', $id)
->query()
->fetch();
 


now I am starting to rethink this. If I am using Zend, I can always just do $model->find($id). This will return _all_ fields from this row though instead of just the 1 field I need. I always thought that returning too many fields was just a total waste but now that I have bigger and bigger code bases, all of these getters are starting to become combersom.

So, really, how much is really gained by only returning the fields that you need? Is it worth writing all the extra code to get just those or is it ok to just return all columns from a table and just ignore the ones that you dont need?


Top
 Profile  
 
PostPosted: Tue Oct 09, 2012 7:52 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
All what extra code? Your ORM should already handle this and should accept an array of parameters including which fields to get.

_________________


Top
 Profile  
 
PostPosted: Tue Oct 09, 2012 8:02 am 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia


Top
 Profile  
 
PostPosted: Tue Oct 09, 2012 1:29 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Tue Oct 09, 2012 1:40 pm 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia


Top
 Profile  
 
PostPosted: Tue Oct 09, 2012 2:13 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
I often have a generic find($where, $fields='*', $sort='') type method in my Models that the other methods call. That makes them essentially two-line methods with the SQL and call to find(). It reduces code while still provideing a clear interface, encapsulateinh the SQL, and can centralize any model specific processing in find().

As for eliminating getters, not sure how you can do that because they are how Views get their data. For other operations, you might want to research "Tell, don't ask". That may help you get away from procedural style. And by procedural style, I hope you don't mean truly procedural things like:
Syntax: [ Download ] [ Hide ]
$data = $model->getSomeData();
$model->doSomething($mode);

// or
$model->mustBeDoneFirst();
$model->hasToBeDoneSecond();

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Oct 10, 2012 3:51 am 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia
My problem is I always end up with things like this:
Syntax: [ Download ] [ Hide ]
$companiesModel = new Model_Admin_Companies;
$clientsModel = new Model_Admin_Clients;

foreach ($this->view->invoices as &$invoice)
{
        $invoice['totalPaid'] = $this->invoicePaymentsModel
        ->getTotalPaid($invoice['invoiceid']);
       
        $invoice['latestPayment'] = $this->invoicePaymentsModel
        ->getLatestPayment($invoice['invoiceid']);
       
        $invoice['clientNumber'] = $clientsModel
        ->getClientNumber($invoice['companyid']);
}

I am not sure exactly how to get away from those though. That is really where I am having an issue as this doesn't seam right. Any suggestions?


Top
 Profile  
 
PostPosted: Wed Oct 10, 2012 5:10 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada

_________________


Top
 Profile  
 
PostPosted: Wed Oct 10, 2012 6:34 am 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia
Is an ORM really needed? Seams like a bunch of extra layers that could get even more complicated


Top
 Profile  
 
PostPosted: Wed Oct 10, 2012 7:02 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
No, it certainly isn't required, but I find them very handy.

Syntax: [ Download ] [ Hide ]
$this->Model->RelatedModel->find('fields' => array('foo', 'bar'));


Looks like a good fit for what you're trying to accomplish.

_________________


Top
 Profile  
 
PostPosted: Wed Oct 10, 2012 1:33 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
Syntax: [ Download ] [ Hide ]
$this->Model->RelatedModel->find('fields' => array('foo', 'bar'));

That is technically not only an ORM example, but is an OOSQL query interface. Most ORMs have them, but so does most ActiveRecord and TableDataGateway implementations as well. Sorry to nitpick. ;)

_________________
(#10850)


Top
 Profile  
 
PostPosted: Thu Oct 11, 2012 1:53 am 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia
Aye, thanks for the suggestions. I will try to get this all running and if I have any problems, you know where I will be posting.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group