PHP Developers Network
http://forums.devnetwork.net/

Select * or Select [field1, ...?
http://forums.devnetwork.net/viewtopic.php?f=19&t=136870
Page 1 of 1

Author:  shiznatix [ Tue Oct 09, 2012 6:49 am ]
Post subject:  Select * or Select [field1, ...?

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?

Author:  Celauran [ Tue Oct 09, 2012 7:52 am ]
Post subject:  Re: Select * or Select [field1, ...?

All what extra code? Your ORM should already handle this and should accept an array of parameters including which fields to get.

Author:  shiznatix [ Tue Oct 09, 2012 8:02 am ]
Post subject:  Re: Select * or Select [field1, ...?


Author:  Christopher [ Tue Oct 09, 2012 1:29 pm ]
Post subject:  Re: Select * or Select [field1, ...?


Author:  shiznatix [ Tue Oct 09, 2012 1:40 pm ]
Post subject:  Re: Select * or Select [field1, ...?


Author:  Christopher [ Tue Oct 09, 2012 2:13 pm ]
Post subject:  Re: Select * or Select [field1, ...?

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();

Author:  shiznatix [ Wed Oct 10, 2012 3:51 am ]
Post subject:  Re: Select * or Select [field1, ...?

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?

Author:  Celauran [ Wed Oct 10, 2012 5:10 am ]
Post subject:  Re: Select * or Select [field1, ...?


Author:  shiznatix [ Wed Oct 10, 2012 6:34 am ]
Post subject:  Re: Select * or Select [field1, ...?

Is an ORM really needed? Seams like a bunch of extra layers that could get even more complicated

Author:  Celauran [ Wed Oct 10, 2012 7:02 am ]
Post subject:  Re: Select * or Select [field1, ...?

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.

Author:  Christopher [ Wed Oct 10, 2012 1:33 pm ]
Post subject:  Re: Select * or Select [field1, ...?

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. ;)

Author:  shiznatix [ Thu Oct 11, 2012 1:53 am ]
Post subject:  Re: Select * or Select [field1, ...?

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.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/