2010年5月1日星期六

Re: [fw-db] Unexpected behavior from Zend_Db_Table_Row_Abstract::setFromArray()

Hey Jeremy,

Try something like this:

$row = $table->createRow($dataArray);

This will create a row where it is aware of what table it belongs to.
This is important since the row has to understand the metadata of the
table, and which columns are in it, and more importantly, which is the
primary key and indexes.

-ralph

jeremykendall wrote:
> I'm getting some unexpected behavior from
> Zend_Db_Table_Row_Abstract::setFromArray(). If I instantiate a new row
> without passing any data and subsequently call setFromArray() to load data,
> Zend_Db_Table_Row::toArray() returns an empty array.
>
> I've never tried to instantiate a row in that manner in an application. I
> ran into this while writing some unit tests. I couldn't find a test in ZF
> that covered this use case, so I whipped one up to demonstrate the issue.
>
> Line 663 in Zend_Db_Table_Row_Abstract seems to be causing the issue (unless
> it's my improper use of the class that's the issue) when it calls
> array_intersect with an empty array as the second argument.
>
> Thoughts?
>
> <?php
>
> /**
> * Zend_Db_Table_Row test case.
> */
> class Kendall_Db_Table_Row_ZendRowTest extends PHPUnit_Framework_TestCase {
>
> /**
> * @var Zend_Db_Table_Row
> */
> private $_row;
>
> /**
> * @var array Initial dataset
> */
> private $_data = array();
>
> /**
> * Prepares the environment before running a test.
> */
> protected function setUp() {
>
> parent::setUp();
>
> $this->_data = array(
> 'row_one' => 1,
> 'row_two' => 2,
> 'row_three' => 3
> );
>
> $this->_row = new Zend_Db_Table_Row();
>
> }
>
> /**
> * Cleans up the environment after running a test.
> */
> protected function tearDown() {
> // TODO Auto-generated Kendall_Db_Table_Row_ZendRowTest::tearDown()
> $this->_row = null;
> parent::tearDown();
> }
>
> /**
> * Tests Zend_Db_Table_Row->setFromArray()
> *
> * Test fails with
> * 1) Kendall_Db_Table_Row_ZendRowTest::testSetFromArray
> * Failed asserting that <boolean:false> is true.
> */
> public function testSetFromArrayFailsToSetData() {
> $this->_row->setFromArray($this->_data);
> $this->assertTrue(array_key_exists('row_one',
> $this->_row->toArray()));
> }
>
> /**
> * Tests Zend_Db_Table_Row->setFromArray()
> *
> * This test passes.
> *
> * I think the problem is on Zend_Db_Table_Row_Abstract->setFromArray()
> line 663.
> * Calling array intersect on ($array, $emptyArray) always returns an
> empty array
> */
> public function testSetFromArrayReturnsEmptyArray() {
> $this->_row->setFromArray($this->_data);
> $this->assertEquals(0, count($this->_row->toArray()));
> }
>
> }
>

没有评论: