Commit 73ad0ac4 authored by romanb's avatar romanb

more tests and first basic update/delete

parent b6e385d2
...@@ -443,9 +443,9 @@ class Doctrine_ORM_EntityManager ...@@ -443,9 +443,9 @@ class Doctrine_ORM_EntityManager
/** /**
* Deletes the persistent state of the given entity. * Deletes the persistent state of the given entity.
* *
* @param Doctrine\ORM\Entity $entity * @param object $entity
*/ */
public function delete(Doctrine_ORM_Entity $entity) public function delete($entity)
{ {
$this->_errorIfNotActiveOrClosed(); $this->_errorIfNotActiveOrClosed();
$this->_unitOfWork->delete($entity); $this->_unitOfWork->delete($entity);
......
...@@ -80,6 +80,23 @@ class Doctrine_ORM_Export_ClassExporter ...@@ -80,6 +80,23 @@ class Doctrine_ORM_Export_ClassExporter
$columns[$mapping['columnName']] = $column; $columns[$mapping['columnName']] = $column;
} }
foreach ($class->getAssociationMappings() as $mapping) {
if ($mapping->isOneToOne() && $mapping->isOwningSide()) {
foreach ($mapping->getSourceToTargetKeyColumns() as $sourceColumn => $targetColumn) {
$column = array();
$column['name'] = $sourceColumn;
$column['type'] = $this->_em->getClassMetadata($mapping->getTargetEntityName())
->getTypeOfColumn($targetColumn);
$columns[$sourceColumn] = $column;
}
} else if ($mapping->isOneToMany() && $mapping->usesJoinTable()) {
//... create join table, one-many through join table supported later
throw new Doctrine_Exception("Not yet implemented.");
} else if ($mapping->isManyToMany() && $mapping->isOwningSide()) {
//... create join table
}
}
$this->_sm->createTable($class->getTableName(), $columns, $options); $this->_sm->createTable($class->getTableName(), $columns, $options);
} }
} }
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
* *
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @since 2.0 * @since 2.0
* @todo Rename to AssociationMapping.
*/ */
abstract class Doctrine_ORM_Mapping_AssociationMapping abstract class Doctrine_ORM_Mapping_AssociationMapping
{ {
...@@ -362,6 +361,11 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping ...@@ -362,6 +361,11 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping
return false; return false;
} }
public function usesJoinTable()
{
return (bool)$this->_joinTable;
}
abstract public function lazyLoadFor($entity, $entityManager); abstract public function lazyLoadFor($entity, $entityManager);
} }
...@@ -77,6 +77,7 @@ class Doctrine_ORM_Mapping_Driver_AnnotationDriver { ...@@ -77,6 +77,7 @@ class Doctrine_ORM_Mapping_Driver_AnnotationDriver {
} else if ($oneToManyAnnot = $property->getAnnotation('DoctrineOneToMany')) { } else if ($oneToManyAnnot = $property->getAnnotation('DoctrineOneToMany')) {
$mapping['mappedBy'] = $oneToManyAnnot->mappedBy; $mapping['mappedBy'] = $oneToManyAnnot->mappedBy;
$mapping['targetEntity'] = $oneToManyAnnot->targetEntity; $mapping['targetEntity'] = $oneToManyAnnot->targetEntity;
$mapping['cascade'] = $oneToManyAnnot->cascade;
$metadata->mapOneToMany($mapping); $metadata->mapOneToMany($mapping);
} else if ($manyToOneAnnot = $property->getAnnotation('DoctrineManyToOne')) { } else if ($manyToOneAnnot = $property->getAnnotation('DoctrineManyToOne')) {
$mapping['joinColumns'] = $manyToOneAnnot->joinColumns; $mapping['joinColumns'] = $manyToOneAnnot->joinColumns;
......
...@@ -100,11 +100,17 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister ...@@ -100,11 +100,17 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
/** /**
* Updates an entity. * Updates an entity.
* *
* @param Doctrine\ORM\Entity $entity The entity to update. * @param object $entity The entity to update.
* @return void * @return void
*/ */
public function update(Doctrine_ORM_Entity $entity) public function update($entity)
{ {
$updateData = array();
$this->_prepareData($entity, $updateData);
$id = array_combine($this->_classMetadata->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity));
$this->_conn->update($this->_classMetadata->getTableName(), $updateData, $id);
/*$dataChangeSet = $entity->_getDataChangeSet(); /*$dataChangeSet = $entity->_getDataChangeSet();
$referenceChangeSet = $entity->_getReferenceChangeSet(); $referenceChangeSet = $entity->_getReferenceChangeSet();
...@@ -126,12 +132,14 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister ...@@ -126,12 +132,14 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
/** /**
* Deletes an entity. * Deletes an entity.
* *
* @param Doctrine\ORM\Entity $entity The entity to delete. * @param object $entity The entity to delete.
* @return void * @return void
*/ */
public function delete(Doctrine_ORM_Entity $entity) public function delete($entity)
{ {
//TODO: perform delete $id = array_combine($this->_classMetadata->getIdentifierFieldNames(),
$this->_em->getUnitOfWork()->getEntityIdentifier($entity));
$this->_conn->delete($this->_classMetadata->getTableName(), $id);
} }
/** /**
...@@ -232,7 +240,7 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister ...@@ -232,7 +240,7 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
*/ */
protected function _prepareData($entity, array &$result, $isInsert = false) protected function _prepareData($entity, array &$result, $isInsert = false)
{ {
foreach ($this->_em->getUnitOfWork()->getDataChangeSet($entity) as $field => $change) { foreach ($this->_em->getUnitOfWork()->getEntityChangeSet($entity) as $field => $change) {
if (is_array($change)) { if (is_array($change)) {
list ($oldVal, $newVal) = each($change); list ($oldVal, $newVal) = each($change);
} else { } else {
...@@ -244,16 +252,21 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister ...@@ -244,16 +252,21 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
$columnName = $this->_classMetadata->getColumnName($field); $columnName = $this->_classMetadata->getColumnName($field);
if ($this->_classMetadata->hasAssociation($field)) { if ($this->_classMetadata->hasAssociation($field)) {
$assocMapping = $this->_classMetadata->getAssociationMapping($field); if ($newVal !== null) {
if ( ! $assocMapping->isOneToOne() || $assocMapping->isInverseSide()) { $assocMapping = $this->_classMetadata->getAssociationMapping($field);
//echo "NOT TO-ONE OR INVERSE!"; if ( ! $assocMapping->isOneToOne() || $assocMapping->isInverseSide()) {
continue; //echo "NOT TO-ONE OR INVERSE!";
} continue;
foreach ($assocMapping->getSourceToTargetKeyColumns() as $sourceColumn => $targetColumn) { }
//TODO: throw exc if field not set //echo "HERE!!!";
$otherClass = $this->_em->getClassMetadata($assocMapping->getTargetEntityName()); foreach ($assocMapping->getSourceToTargetKeyColumns() as $sourceColumn => $targetColumn) {
$result[$sourceColumn] = $otherClass->getReflectionProperty( //TODO: throw exc if field not set
$otherClass->getFieldName($targetColumn))->getValue($newVal); $otherClass = $this->_em->getClassMetadata($assocMapping->getTargetEntityName());
$result[$sourceColumn] = $otherClass->getReflectionProperty(
$otherClass->getFieldName($targetColumn))->getValue($newVal);
}
} else if ( ! $isInsert) {
echo "NO INSERT AND NEWVAL NULL ON 1-1 ASSOC, OWNING SIDE";
} }
} else if (is_null($newVal)) { } else if (is_null($newVal)) {
$result[$columnName] = null; $result[$columnName] = null;
......
This diff is collapsed.
...@@ -8,14 +8,14 @@ require_once 'lib/DoctrineTestInit.php'; ...@@ -8,14 +8,14 @@ require_once 'lib/DoctrineTestInit.php';
* @author robo * @author robo
*/ */
class Orm_Functional_BasicCRUDTest extends Doctrine_OrmFunctionalTestCase { class Orm_Functional_BasicCRUDTest extends Doctrine_OrmFunctionalTestCase {
public function testFoo() { public function testSingleEntityCRUD() {
$em = $this->_getEntityManager(); $em = $this->_getEntityManager();
$exporter = new Doctrine_ORM_Export_ClassExporter($em); $exporter = new Doctrine_ORM_Export_ClassExporter($em);
$exporter->exportClasses(array( $exporter->exportClasses(array(
$em->getClassMetadata('CmsUser'), $em->getClassMetadata('CmsUser'),
$em->getClassMetadata('CmsPhonenumber') $em->getClassMetadata('CmsPhonenumber')
)); ));
// Create // Create
$user = new CmsUser; $user = new CmsUser;
...@@ -24,25 +24,33 @@ class Orm_Functional_BasicCRUDTest extends Doctrine_OrmFunctionalTestCase { ...@@ -24,25 +24,33 @@ class Orm_Functional_BasicCRUDTest extends Doctrine_OrmFunctionalTestCase {
$this->assertTrue(is_numeric($user->id)); $this->assertTrue(is_numeric($user->id));
$this->assertTrue($em->contains($user)); $this->assertTrue($em->contains($user));
$user2 = new CmsUser;
$user2->name = 'jwage';
$em->save($user2);
$this->assertTrue(is_numeric($user2->id));
$this->assertTrue($em->contains($user2));
// Read // Read
$user3 = $em->find('CmsUser', $user->id); $user2 = $em->find('CmsUser', $user->id);
$this->assertTrue($user === $user3); $this->assertTrue($user === $user2);
$user4 = $em->find('CmsUser', $user2->id);
$this->assertTrue($user2 === $user4);
// Add a phonenumber
$ph = new CmsPhonenumber; $ph = new CmsPhonenumber;
$ph->phonenumber = "12345"; $ph->phonenumber = "12345";
$user->addPhonenumber($ph);
$em->flush();
$this->assertTrue($em->contains($ph));
$this->assertTrue($em->contains($user));
$user->phonenumbers[] = $ph; // Update
$user->name = 'guilherme';
$em->flush();
$this->assertEquals('guilherme', $user->name);
// Delete
$em->delete($user);
$this->assertTrue($em->getUnitOfWork()->isRegisteredRemoved($user));
$em->flush();
$this->assertFalse($em->getUnitOfWork()->isRegisteredRemoved($user));
}
//var_dump($em->getUnitOfWork()) public function testMore() {
} }
} }
...@@ -119,15 +119,21 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase ...@@ -119,15 +119,21 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
$this->assertEquals(0, count($avatarPersister->getDeletes())); $this->assertEquals(0, count($avatarPersister->getDeletes()));
} }
public function testComputeDataChangeSet() public function testComputeEntityChangeSets()
{ {
// We need an ID generator for ForumAvatar, because we attach a NEW ForumAvatar
// to a (faked) MANAGED instance. During changeset computation this will result
// in the UnitOfWork requesting the Id generator of ForumAvatar.
$avatarIdGeneratorMock = new Doctrine_IdentityIdGeneratorMock($this->_emMock);
$this->_emMock->setIdGenerator('ForumAvatar', $avatarIdGeneratorMock);
$user1 = new ForumUser(); $user1 = new ForumUser();
$user1->id = 1; $user1->id = 1;
$user1->username = "romanb"; $user1->username = "romanb";
$user1->avatar = new ForumAvatar(); $user1->avatar = new ForumAvatar();
// Fake managed state // Fake managed state
$this->_unitOfWork->setEntityState($user1, Doctrine_ORM_UnitOfWork::STATE_MANAGED); $this->_unitOfWork->setEntityState($user1, Doctrine_ORM_UnitOfWork::STATE_MANAGED);
$user2 = new ForumUser(); $user2 = new ForumUser();
$user2->id = 2; $user2->id = 2;
$user2->username = "jwage"; $user2->username = "jwage";
...@@ -143,10 +149,10 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase ...@@ -143,10 +149,10 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
)); ));
// Go // Go
$this->_unitOfWork->computeDataChangeSet(array($user1, $user2)); $this->_unitOfWork->computeEntityChangeSets(array($user1, $user2));
// Verify // Verify
$user1ChangeSet = $this->_unitOfWork->getDataChangeSet($user1); $user1ChangeSet = $this->_unitOfWork->getEntityChangeSet($user1);
$this->assertTrue(is_array($user1ChangeSet)); $this->assertTrue(is_array($user1ChangeSet));
$this->assertEquals(2, count($user1ChangeSet)); $this->assertEquals(2, count($user1ChangeSet));
$this->assertTrue(isset($user1ChangeSet['username'])); $this->assertTrue(isset($user1ChangeSet['username']));
...@@ -154,7 +160,7 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase ...@@ -154,7 +160,7 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
$this->assertTrue(isset($user1ChangeSet['avatar'])); $this->assertTrue(isset($user1ChangeSet['avatar']));
$this->assertSame(array(null => $user1->avatar), $user1ChangeSet['avatar']); $this->assertSame(array(null => $user1->avatar), $user1ChangeSet['avatar']);
$user2ChangeSet = $this->_unitOfWork->getDataChangeSet($user2); $user2ChangeSet = $this->_unitOfWork->getEntityChangeSet($user2);
$this->assertTrue(is_array($user2ChangeSet)); $this->assertTrue(is_array($user2ChangeSet));
$this->assertEquals(1, count($user2ChangeSet)); $this->assertEquals(1, count($user2ChangeSet));
$this->assertTrue(isset($user2ChangeSet['username'])); $this->assertTrue(isset($user2ChangeSet['username']));
......
...@@ -81,6 +81,7 @@ class Doctrine_EntityManagerMock extends Doctrine_ORM_EntityManager ...@@ -81,6 +81,7 @@ class Doctrine_EntityManagerMock extends Doctrine_ORM_EntityManager
$this->_idGenerators[$className] = $generator; $this->_idGenerators[$className] = $generator;
} }
/** @override */
public function getIdGenerator($className) public function getIdGenerator($className)
{ {
if (isset($this->_idGenerators[$className])) { if (isset($this->_idGenerators[$className])) {
......
...@@ -14,10 +14,10 @@ class Doctrine_UnitOfWorkMock extends Doctrine_ORM_UnitOfWork { ...@@ -14,10 +14,10 @@ class Doctrine_UnitOfWorkMock extends Doctrine_ORM_UnitOfWork {
* @param <type> $entity * @param <type> $entity
* @override * @override
*/ */
public function getDataChangeSet($entity) { public function getEntityChangeSet($entity) {
$oid = spl_object_hash($entity); $oid = spl_object_hash($entity);
return isset($this->_mockDataChangeSets[$oid]) ? return isset($this->_mockDataChangeSets[$oid]) ?
$this->_mockDataChangeSets[$oid] : parent::getDataChangeSet($entity); $this->_mockDataChangeSets[$oid] : parent::getEntityChangeSet($entity);
} }
/* MOCK API */ /* MOCK API */
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
/** /**
* @DoctrineEntity * @DoctrineEntity
*/ */
class CmsPhonenumber implements Doctrine_ORM_Entity class CmsPhonenumber
{ {
/** /**
* @DoctrineColumn(type="varchar", length=50) * @DoctrineColumn(type="varchar", length=50)
...@@ -14,4 +14,9 @@ class CmsPhonenumber implements Doctrine_ORM_Entity ...@@ -14,4 +14,9 @@ class CmsPhonenumber implements Doctrine_ORM_Entity
* @DoctrineManyToOne(targetEntity="CmsUser", joinColumns={"user_id" = "id"}) * @DoctrineManyToOne(targetEntity="CmsUser", joinColumns={"user_id" = "id"})
*/ */
public $user; public $user;
public function setUser(CmsUser $user) {
$this->user = $user;
$user->addPhonenumber($this);
}
} }
...@@ -33,4 +33,14 @@ class CmsUser ...@@ -33,4 +33,14 @@ class CmsUser
* @DoctrineOneToMany(targetEntity="CmsArticle", mappedBy="user") * @DoctrineOneToMany(targetEntity="CmsArticle", mappedBy="user")
*/ */
public $articles; public $articles;
/**
* Adds a phonenumber to the user.
*
* @param <type> $phone
*/
public function addPhonenumber(CmsPhonenumber $phone) {
$this->phonenumbers[] = $phone;
$phone->user = $this;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment