2008年8月20日星期三

Re: [fw-db] find() returns rowset

I completely agree that is it much better to just chain ->current()
than test for 3 different types.

Adam

On Aug 20, 2008, at 2:36 PM, Bill Karwin wrote:

>
>
> ah125i wrote:
>>
>> I was under the impression we have Zend_Db_Table_Rowset because it
>> implements SeekableIterator and Countable
>>
>
> Yes. The Rowset class has some other object methods on it, like
> toArray().
> It's also serializable, so you can put a Rowset in a cache.
>
> The find() method may result in zero, one, or more rows. When I
> joined the
> ZF team, this method returned either a Row or Rowset object, but I
> changed
> it. It seemed confusing that a method may return two different types,
> depending on its arguments. Also, what should it return if the search
> matches zero rows in the database? Null? So now I would have to
> test the
> return value for three possible types? (null/Row/Rowset)
>
> $result = $table->find( ...args ... );
> if ($result === null) {
> // zero rows matched
> } elsif ($results instanceof Zend_Db_Table_Row) {
> // one row matched
> } elsif ($results instanceof Zend_Db_Table_Rowset) {
> if (count($results) == 0) {
> // zero rows matched
> } else {
> // multiple rows matched
> }
> }
>
> Note that some overlap cases exist. You can give two values (123,
> 456), so
> you're asking for multiple rows. But suppose only a row matching 123
> exists, no row matching 456. Should find() return a Rowset or a
> Row? Does
> the return type depend on what you ask for, or what the result is?
>
> I felt the best way to make it "simple" was always to return a
> Rowset. That
> handles all three cases of zero rows, one row, or several rows. The
> Rowset
> has an interface for testing if it's empty, getting the first row, or
> iterating over multiple rows. But the point is that you know what
> that
> interface is, without having to branch on the object's type.
>
> If it's a little awkward to call $rowset->current() to get the first
> row,
> surely it's much more awkward to write a big if/elsif/else structure
> to test
> the result type every time you call find().
>
> Regards,
> Bill Karwin
> --
> View this message in context: http://www.nabble.com/find%28%29-returns-rowset-tp13527277p19075579.html
> Sent from the Zend DB mailing list archive at Nabble.com.
>

没有评论: