Added synchronizeWithArray method and unset functionality for relations
The unset functionality is not working for foreignKey relations
This commit is contained in:
parent
014b18f4f6
commit
78195944be
@ -678,6 +678,33 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* synchronizeWithArray
|
||||
* synchronizes a Doctrine_Collection with data from an array
|
||||
*
|
||||
* it expects an array representation of a Doctrine_Collection similar to the return
|
||||
* value of the toArray() method. It will create Dectrine_Records that don't exist
|
||||
* on the collection, update the ones that do and remove the ones missing in the $array
|
||||
*
|
||||
* @param array $array representation of a Doctrine_Collection
|
||||
*/
|
||||
public function synchronizeWithArray(array $array)
|
||||
{
|
||||
foreach ($this as $key => $record) {
|
||||
if (isset($array[$key])) {
|
||||
$record->synchronizeWithArray($array[$key]);
|
||||
unset($array[$key]);
|
||||
} else {
|
||||
// remove records that don't exist in the array
|
||||
$this->remove($key);
|
||||
}
|
||||
}
|
||||
// create new records for each new row in the array
|
||||
foreach ($array as $rowKey => $row) {
|
||||
$this[$rowKey]->fromArray($row);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* exportTo
|
||||
*
|
||||
|
@ -1000,7 +1000,14 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
|
||||
if (isset($this->_data[$fieldName])) {
|
||||
$this->_data[$fieldName] = array();
|
||||
}
|
||||
// todo: what to do with references ?
|
||||
if (isset($this->_references[$fieldName])) {
|
||||
if ($this->_references[$fieldName] instanceof Doctrine_Record) {
|
||||
// todo: delete related record when saving $this
|
||||
$this->_references[$fieldName] = self::$_null;
|
||||
} elseif ($this->_references[$fieldName] instanceof Doctrine_Collection) {
|
||||
$this->_references[$fieldName]->setData(array());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1256,6 +1263,34 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* synchronizeWithArray
|
||||
* synchronizes a Doctrine_Record and its relations with data from an array
|
||||
*
|
||||
* it expects an array representation of a Doctrine_Record similar to the return
|
||||
* value of the toArray() method. If the array contains relations it will create
|
||||
* those that don't exist, update the ones that do, and delete the ones missing
|
||||
* on the array but available on the Doctrine_Record
|
||||
*
|
||||
* @param array $array representation of a Doctrine_Record
|
||||
*/
|
||||
public function synchronizeWithArray(array $array)
|
||||
{
|
||||
foreach ($array as $key => $value) {
|
||||
if ($this->getTable()->hasRelation($key)) {
|
||||
$this->get($key)->synchronizeWithArray($value);
|
||||
} else if ($this->getTable()->hasColumn($key)) {
|
||||
$this->set($key, $value);
|
||||
}
|
||||
}
|
||||
// eliminate relationships missing in the $array
|
||||
foreach ($this->_references as $name => $obj) {
|
||||
if (!isset($array[$name])) {
|
||||
unset($this->$name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* exportTo
|
||||
*
|
||||
|
121
tests/Record/SynchronizeTestCase.php
Normal file
121
tests/Record/SynchronizeTestCase.php
Normal file
@ -0,0 +1,121 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Doctrine_Record_State_TestCase
|
||||
*
|
||||
* @package Doctrine
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
*/
|
||||
class Doctrine_Record_Synchronize_TestCase extends Doctrine_UnitTestCase
|
||||
{
|
||||
public function prepareTables()
|
||||
{
|
||||
parent::prepareTables();
|
||||
}
|
||||
|
||||
public function prepareData()
|
||||
{
|
||||
$user = new User();
|
||||
$user->name = 'John';
|
||||
$user->Email->address = 'john@mail.com';
|
||||
$user->Phonenumber[0]->phonenumber = '555 123';
|
||||
$user->Phonenumber[1]->phonenumber = '555 448';
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function testSynchronizeRecord()
|
||||
{
|
||||
$user = Doctrine_Query::create()->from('User u, u.Email, u.Phonenumber')->fetchOne();
|
||||
$userArray = $user->toArray(true);
|
||||
$this->assertEqual($user->Phonenumber->count(), 2);
|
||||
$this->assertEqual($user->Phonenumber[0]->phonenumber, '555 123');
|
||||
|
||||
// modify a Phonenumber
|
||||
$userArray['Phonenumber'][0]['phonenumber'] = '555 321';
|
||||
|
||||
// delete a Phonenumber
|
||||
array_pop($userArray['Phonenumber']);
|
||||
|
||||
$user->synchronizeWithArray($userArray);
|
||||
$this->assertEqual($user->Phonenumber->count(), 1);
|
||||
$this->assertEqual($user->Phonenumber[0]->phonenumber, '555 321');
|
||||
|
||||
// change Email
|
||||
$userArray['Email']['address'] = 'johndow@mail.com';
|
||||
$user->synchronizeWithArray($userArray);
|
||||
$this->assertEqual($user->Email->address, 'johndow@mail.com');
|
||||
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function testSynchronizeAfterSaveRecord()
|
||||
{
|
||||
$user = Doctrine_Query::create()->from('User u, u.Email, u.Phonenumber')->fetchOne();
|
||||
$this->assertEqual($user->Phonenumber->count(), 1);
|
||||
$this->assertEqual($user->Phonenumber[0]->phonenumber, '555 321');
|
||||
$this->assertEqual($user->Email->address, 'johndow@mail.com');
|
||||
}
|
||||
|
||||
public function testSynchronizeAddRecord()
|
||||
{
|
||||
$user = Doctrine_Query::create()->from('User u, u.Email, u.Phonenumber')->fetchOne();
|
||||
$userArray = $user->toArray(true);
|
||||
$userArray['Phonenumber'][] = array('phonenumber' => '333 238');
|
||||
|
||||
$user->synchronizeWithArray($userArray);
|
||||
$this->assertEqual($user->Phonenumber->count(), 2);
|
||||
$this->assertEqual($user->Phonenumber[1]->phonenumber, '333 238');
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function testSynchronizeAfterAddRecord()
|
||||
{
|
||||
$user = Doctrine_Query::create()->from('User u, u.Email, u.Phonenumber')->fetchOne();
|
||||
$this->assertEqual($user->Phonenumber->count(), 2);
|
||||
$this->assertEqual($user->Phonenumber[1]->phonenumber, '333 238');
|
||||
}
|
||||
|
||||
public function testSynchronizeRemoveRecord()
|
||||
{
|
||||
$user = Doctrine_Query::create()->from('User u, u.Email, u.Phonenumber')->fetchOne();
|
||||
$userArray = $user->toArray(true);
|
||||
unset($userArray['Phonenumber']);
|
||||
unset($userArray['Email']);
|
||||
|
||||
$user->synchronizeWithArray($userArray);
|
||||
$this->assertEqual($user->Phonenumber->count(), 0);
|
||||
$this->assertTrue(!isset($user->Email));
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function testSynchronizeAfterRemoveRecord()
|
||||
{
|
||||
$user = Doctrine_Query::create()->from('User u, u.Email, u.Phonenumber')->fetchOne();
|
||||
$this->assertEqual($user->Phonenumber->count(), 0);
|
||||
$this->assertTrue(!isset($user->Email));
|
||||
}
|
||||
}
|
@ -218,6 +218,7 @@ $record->addTestCase(new Doctrine_Record_Lock_TestCase());
|
||||
$record->addTestCase(new Doctrine_Record_ZeroValues_TestCase());
|
||||
//$record->addTestCase(new Doctrine_Record_SaveBlankRecord_TestCase());
|
||||
$record->addTestCase(new Doctrine_Record_Inheritance_TestCase());
|
||||
$record->addTestCase(new Doctrine_Record_Synchronize_TestCase());
|
||||
$test->addTestCase($record);
|
||||
|
||||
$test->addTestCase(new Doctrine_CustomPrimaryKey_TestCase());
|
||||
|
Loading…
Reference in New Issue
Block a user