1
0
mirror of synced 2024-12-14 15:16:04 +03:00

Merge remote branch 'origin/master'

This commit is contained in:
Benjamin Eberlei 2011-02-02 19:49:23 +01:00
commit 277e0aee8c
18 changed files with 307 additions and 40 deletions

View File

@ -458,6 +458,16 @@ abstract class AbstractQuery
return isset($this->_hints[$name]) ? $this->_hints[$name] : false;
}
/**
* Return the key value map of query hints that are currently set.
*
* @return array
*/
public function getHints()
{
return $this->_hints;
}
/**
* Executes the query and returns an IterableResult that can be used to incrementally
* iterate over the result.
@ -496,10 +506,10 @@ abstract class AbstractQuery
// Check result cache
if ($this->_useResultCache && $cacheDriver = $this->getResultCacheDriver()) {
$id = $this->_getResultCacheId();
list($id, $hash) = $this->getResultCacheId();
$cached = $this->_expireResultCache ? false : $cacheDriver->fetch($id);
if ($cached === false) {
if ($cached === false || !isset($cached[$id])) {
// Cache miss.
$stmt = $this->_doExecute();
@ -512,7 +522,7 @@ abstract class AbstractQuery
return $result;
} else {
// Cache hit.
return $cached;
return $cached[$id];
}
}
@ -546,12 +556,12 @@ abstract class AbstractQuery
* Will return the configured id if it exists otherwise a hash will be
* automatically generated for you.
*
* @return string $id
* @return array ($id, $hash)
*/
protected function _getResultCacheId()
protected function getResultCacheId()
{
if ($this->_resultCacheId) {
return $this->_resultCacheId;
return array($this->_resultCacheId, $this->_resultCacheId);
} else {
$params = $this->_params;
foreach ($params AS $key => $value) {
@ -563,13 +573,16 @@ abstract class AbstractQuery
$idValues = $class->getIdentifierValues($value);
}
$params[$key] = $idValues;
} else {
$params[$key] = $value;
}
}
$sql = $this->getSql();
ksort($this->_hints);
return md5(implode(";", (array)$sql) . var_export($params, true) .
var_export($this->_hints, true)."&hydrationMode=".$this->_hydrationMode);
$key = implode(";", (array)$sql) . var_export($params, true) .
var_export($this->_hints, true)."&hydrationMode=".$this->_hydrationMode;
return array($key, md5($key));
}
}

View File

@ -194,6 +194,7 @@ abstract class AbstractHydrator
$cache[$key]['isMetaColumn'] = true;
$cache[$key]['fieldName'] = $fieldName;
$cache[$key]['dqlAlias'] = $this->_rsm->columnOwnerMap[$key];
$classMetadata = $this->_em->getClassMetadata($this->_rsm->aliasMap[$cache[$key]['dqlAlias']]);
$cache[$key]['isIdentifier'] = $classMetadata->isIdentifier($fieldName);
}
}

View File

@ -658,8 +658,8 @@ class ClassMetadataInfo
protected function _validateAndCompleteFieldMapping(array &$mapping)
{
// Check mandatory fields
if ( ! isset($mapping['fieldName'])) {
throw MappingException::missingFieldName($this->name, $mapping);
if ( ! isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) {
throw MappingException::missingFieldName($this->name);
}
if ( ! isset($mapping['type'])) {
// Default to string
@ -749,8 +749,8 @@ class ClassMetadataInfo
// Mandatory attributes for both sides
// Mandatory: fieldName, targetEntity
if ( ! isset($mapping['fieldName'])) {
throw MappingException::missingFieldName();
if ( ! isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) {
throw MappingException::missingFieldName($this->name);
}
if ( ! isset($mapping['targetEntity'])) {
throw MappingException::missingTargetEntity($mapping['fieldName']);

View File

@ -48,9 +48,9 @@ class MappingException extends \Doctrine\ORM\ORMException
return new self("Id generators can't be used with a composite id.");
}
public static function missingFieldName()
public static function missingFieldName($entity)
{
return new self("The association mapping misses the 'fieldName' attribute.");
return new self("The field or association mapping misses the 'fieldName' attribute in entity '$entity'.");
}
public static function missingTargetEntity($fieldName)

View File

@ -28,7 +28,9 @@ use PDO,
Doctrine\ORM\Query,
Doctrine\ORM\PersistentCollection,
Doctrine\ORM\Mapping\MappingException,
Doctrine\ORM\Mapping\ClassMetadata;
Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\ORM\Events,
Doctrine\ORM\Event\LifecycleEventArgs;
/**
* A BasicEntityPersiter maps an entity to a single table in a relational database.
@ -223,7 +225,7 @@ class BasicEntityPersister
}
if ($this->_class->isVersioned) {
$this->_assignDefaultVersionValue($this->_class, $entity, $id);
$this->assignDefaultVersionValue($entity, $id);
}
}
@ -238,22 +240,33 @@ class BasicEntityPersister
* by the preceding INSERT statement and assigns it back in to the
* entities version field.
*
* @param Doctrine\ORM\Mapping\ClassMetadata $class
* @param object $entity
* @param mixed $id
*/
protected function _assignDefaultVersionValue($class, $entity, $id)
protected function assignDefaultVersionValue($entity, $id)
{
$versionField = $this->_class->versionField;
$identifier = $this->_class->getIdentifierColumnNames();
$versionFieldColumnName = $this->_class->getColumnName($versionField);
$value = $this->fetchVersionValue($this->_class, $id);
$this->_class->setFieldValue($entity, $this->_class->versionField, $value);
}
/**
* Fetch the current version value of a versioned entity.
*
* @param Doctrine\ORM\Mapping\ClassMetadata $versionedClass
* @param mixed $id
* @return mixed
*/
protected function fetchVersionValue($versionedClass, $id)
{
$versionField = $versionedClass->versionField;
$identifier = $versionedClass->getIdentifierColumnNames();
$versionFieldColumnName = $versionedClass->getColumnName($versionField);
//FIXME: Order with composite keys might not be correct
$sql = "SELECT " . $versionFieldColumnName . " FROM " . $class->getQuotedTableName($this->_platform)
$sql = "SELECT " . $versionFieldColumnName . " FROM " . $versionedClass->getQuotedTableName($this->_platform)
. " WHERE " . implode(' = ? AND ', $identifier) . " = ?";
$value = $this->_conn->fetchColumn($sql, array_values((array)$id));
$value = Type::getType($class->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->_platform);
$this->_class->setFieldValue($entity, $versionField, $value);
return Type::getType($versionedClass->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->_platform);
}
/**
@ -282,7 +295,7 @@ class BasicEntityPersister
if ($this->_class->isVersioned) {
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
$this->_assignDefaultVersionValue($this->_class, $entity, $id);
$this->assignDefaultVersionValue($entity, $id);
}
}
}
@ -693,6 +706,14 @@ class BasicEntityPersister
}
$this->_em->getUnitOfWork()->setOriginalEntityData($entity, $newData);
if (isset($this->_class->lifecycleCallbacks[Events::postLoad])) {
$this->_class->invokeLifecycleCallbacks(Events::postLoad, $entity);
}
$evm = $this->_em->getEventManager();
if ($evm->hasListeners(Events::postLoad)) {
$evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->_em));
}
}
/**

View File

@ -108,10 +108,6 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
return;
}
if ($this->_class->isVersioned) {
$versionedClass = $this->_getVersionedClassMetadata();
}
$postInsertIds = array();
$idGen = $this->_class->idGenerator;
$isPostInsertId = $idGen->isPostInsertGenerator();
@ -177,8 +173,8 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
$stmt->closeCursor();
}
if (isset($versionedClass)) {
$this->_assignDefaultVersionValue($versionedClass, $entity, $id);
if ($this->_class->isVersioned) {
$this->assignDefaultVersionValue($entity, $id);
}
$this->_queuedInserts = array();
@ -208,7 +204,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
$this->_updateTable($entity, $versionedClass->getQuotedTableName($this->_platform), array(), true);
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
$this->_assignDefaultVersionValue($this->_class, $entity, $id);
$this->assignDefaultVersionValue($entity, $id);
}
}
}
@ -428,4 +424,13 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
return $columns;
}
/**
* {@inheritdoc}
*/
protected function assignDefaultVersionValue($entity, $id)
{
$value = $this->fetchVersionValue($this->_getVersionedClassMetadata(), $id);
$this->_class->setFieldValue($entity, $this->_class->versionField, $value);
}
}

View File

@ -63,6 +63,10 @@ class ConvertMappingCommand extends Console\Command\Command
'dest-path', InputArgument::REQUIRED,
'The path to generate your entities classes.'
),
new InputOption(
'force', null, InputOption::VALUE_NONE,
'Force to overwrite existing mapping files.'
),
new InputOption(
'from-database', null, null, 'Whether or not to convert mapping information from existing database.'
),
@ -73,10 +77,24 @@ class ConvertMappingCommand extends Console\Command\Command
new InputOption(
'num-spaces', null, InputOption::VALUE_OPTIONAL,
'Defines the number of indentation spaces', 4
)
),
))
->setHelp(<<<EOT
Convert mapping information between supported formats.
This is an execute <info>one-time</info> command. It should not be necessary for
you to call this method multiple times, escpecially when using the <comment>--from-database</comment>
flag.
Converting an existing databsae schema into mapping files only solves about 70-80%
of the necessary mapping information. Additionally the detection from an existing
database cannot detect inverse associations, inheritance types,
entities with foreign keys as primary keys and many of the
semantical operations on associations such as cascade.
<comment>Hint:</comment> There is no need to convert YAML or XML mapping files to annotations
every time you make changes. All mapping drivers are first class citizens
in Doctrine 2 and can be used as runtime mapping for the ORM.
EOT
);
}
@ -121,6 +139,7 @@ EOT
$cme = new ClassMetadataExporter();
$exporter = $cme->getExporter($toType, $destPath);
$exporter->setOverwriteExistingFiles( ($input->getOption('force') !== false) );
if ($toType == 'annotation') {
$entityGenerator = new EntityGenerator();

View File

@ -85,6 +85,23 @@ class GenerateEntitiesCommand extends Console\Command\Command
))
->setHelp(<<<EOT
Generate entity classes and method stubs from your mapping information.
If you use the <comment>--update-entities</comment> or <comment>--regenerate-entities</comment> flags your exisiting
code gets overwritten. The EntityGenerator will only append new code to your
file and will not delete the old code. However this approach may still be prone
to error and we suggest you use code repositories such as GIT or SVN to make
backups of your code.
It makes sense to generate the entity code if you are using entities as Data
Access Objects only and dont put much additional logic on them. If you are
however putting much more logic on the entities you should refrain from using
the entity-generator and code your entities manually.
<error>Important:</error> Even if you specified Inheritance options in your
XML or YAML Mapping files the generator cannot generate the base and
child classes for you correctly, because it doesn't know which
class is supposed to extend which. You have to adjust the entity
code manually for inheritance to work!
EOT
);
}

View File

@ -23,6 +23,7 @@
namespace Doctrine\ORM\Tools\Export\Driver;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Tools\Export\ExportException;
/**
* Abstract base class which is to be used for the Exporter drivers
@ -39,12 +40,18 @@ abstract class AbstractExporter
protected $_metadata = array();
protected $_outputDir;
protected $_extension;
protected $_overwriteExistingFiles = false;
public function __construct($dir = null)
{
$this->_outputDir = $dir;
}
public function setOverwriteExistingFiles($overwrite)
{
$this->_overwriteExistingFiles = $overwrite;
}
/**
* Converts a single ClassMetadata instance to the exported format
* and returns it
@ -110,6 +117,9 @@ abstract class AbstractExporter
if ( ! is_dir($dir)) {
mkdir($dir, 0777, true);
}
if (file_exists($path) && !$this->_overwriteExistingFiles) {
throw ExportException::attemptOverwriteExistingFile($path);
}
file_put_contents($path, $output);
}
}

View File

@ -15,4 +15,9 @@ class ExportException extends ORMException
{
return new self("The mapping driver '$type' does not exist");
}
public static function attemptOverwriteExistingFile($file)
{
return new self("Attempting to overwrite an existing file '".$file."'.");
}
}

View File

@ -179,7 +179,7 @@ class SchemaTool
$this->_gatherColumn($class, $idMapping, $table);
$columnName = $class->getQuotedColumnName($class->identifier[0], $this->_platform);
// TODO: This seems rather hackish, can we optimize it?
$table->getColumn($class->identifier[0])->setAutoincrement(false);
$table->getColumn($columnName)->setAutoincrement(false);
$pkColumns[] = $columnName;

View File

@ -462,6 +462,7 @@ class UnitOfWork implements PropertyChangedListener
// A PersistentCollection was de-referenced, so delete it.
if ( ! in_array($orgValue, $this->collectionDeletions, true)) {
$this->collectionDeletions[] = $orgValue;
$changeSet[$propName] = $orgValue; // Signal changeset, to-many assocs will be ignored.
}
}
} else if ($isChangeTrackingNotify) {

View File

@ -82,6 +82,27 @@ class LifecycleCallbackTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertTrue($reference->postLoadCallbackInvoked);
}
/**
* @group DDC-958
*/
public function testPostLoadTriggeredOnRefresh()
{
$entity = new LifecycleCallbackTestEntity;
$entity->value = 'hello';
$this->_em->persist($entity);
$this->_em->flush();
$id = $entity->getId();
$this->_em->clear();
$reference = $this->_em->find('Doctrine\Tests\ORM\Functional\LifecycleCallbackTestEntity', $id);
$this->assertTrue($reference->postLoadCallbackInvoked);
$reference->postLoadCallbackInvoked = false;
$this->_em->refresh($reference);
$this->assertTrue($reference->postLoadCallbackInvoked, "postLoad should be invoked when refresh() is called.");
}
/**
* @group DDC-113
*/

View File

@ -3,7 +3,8 @@
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\CMS\CmsUser,
Doctrine\Tests\Models\CMS\CmsGroup;
Doctrine\Tests\Models\CMS\CmsGroup,
Doctrine\Common\Collections\ArrayCollection;
require_once __DIR__ . '/../../TestInit.php';
@ -308,4 +309,37 @@ class ManyToManyBasicAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCa
$this->_em->createQuery("DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) = 10")->execute();
$this->_em->createQuery("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = 'inactive' WHERE SIZE(u.groups) = 10")->execute();
}
/**
* @group DDC-978
*/
public function testClearAndResetCollection()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$group1 = new CmsGroup;
$group1->name = 'Developers_New1';
$group2 = new CmsGroup;
$group2->name = 'Developers_New2';
$this->_em->persist($group1);
$this->_em->persist($group2);
$this->_em->flush();
$this->_em->clear();
$user = $this->_em->find(get_class($user), $user->id);
$coll = new ArrayCollection(array($group1, $group2));
$user->groups = $coll;
$this->_em->flush();
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->groups,
"UnitOfWork should have replaced ArrayCollection with PersistentCollection.");
$this->_em->flush();
$this->_em->clear();
$user = $this->_em->find(get_class($user), $user->id);
$this->assertEquals(2, count($user->groups));
$this->assertEquals('Developers_New1', $user->groups[0]->name);
$this->assertEquals('Developers_New2', $user->groups[1]->name);
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
require_once __DIR__ . '/../../../TestInit.php';
class DDC960Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC960Root'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC960Child')
));
} catch(\Exception $e) {
}
}
/**
* @group DDC-960
*/
public function testUpdateRootVersion()
{
$child = new DDC960Child('Test');
$this->_em->persist($child);
$this->_em->flush();
$child->setName("Test2");
$this->_em->flush();
$this->assertEquals(2, $child->getVersion());
}
}
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorMap({
* "root" = "DDC960Root",
* "child" = "DDC960Child"
* })
*/
class DDC960Root
{
/**
* @Id @GeneratedValue @Column(type="integer")
*/
private $id;
/**
* @Column(type="integer") @Version
*/
private $version;
public function getId()
{
return $this->id;
}
public function getVersion()
{
return $this->version;
}
}
/**
* @Entity
*/
class DDC960Child extends DDC960Root
{
/**
* @column(type="string")
* @var string
*/
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function setName($name)
{
$this->name = $name;
}
}

View File

@ -379,4 +379,15 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
'joinColumns' => array(),
));
}
/**
* @group DDC-996
*/
public function testEmptyFieldNameThrowsException()
{
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
"The field or association mapping misses the 'fieldName' attribute in entity 'Doctrine\Tests\Models\CMS\CmsUser'.");
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
$cm->mapField(array('fieldName' => ''));
}
}

View File

@ -86,4 +86,17 @@ class QueryTest extends \Doctrine\Tests\OrmTestCase
$this->assertSame($q2, $q);
}
/**
* @group DDC-968
*/
public function testHints()
{
$q = $this->_em->createQuery("select a from Doctrine\Tests\Models\CMS\CmsArticle a");
$q->setHint('foo', 'bar')->setHint('bar', 'baz');
$this->assertEquals('bar', $q->getHint('foo'));
$this->assertEquals('baz', $q->getHint('bar'));
$this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $q->getHints());
}
}

View File

@ -69,6 +69,7 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
$converter = new ConvertDoctrine1Schema(__DIR__ . '/doctrine1schema');
$exporter = $cme->getExporter('yml', __DIR__ . '/convert');
$exporter->setOverwriteExistingFiles(true);
$exporter->setMetadata($converter->getMetadata());
$exporter->export();
@ -80,8 +81,8 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em);
$metadata = $cmf->getAllMetadata();
$profileClass = $metadata[0];
$userClass = $metadata[1];
$profileClass = $cmf->getMetadataFor('Profile');
$userClass = $cmf->getMetadataFor('User');
$this->assertEquals(2, count($metadata));
$this->assertEquals('Profile', $profileClass->name);
@ -96,9 +97,12 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('User', $profileClass->associationMappings['User']['targetEntity']);
$this->assertEquals('username', $userClass->table['uniqueConstraints']['username']['columns'][0]);
}
unlink(__DIR__ . '/convert/User.dcm.yml');
unlink(__DIR__ . '/convert/Profile.dcm.yml');
rmdir(__DIR__ . '/convert');
public function tearDown()
{
@unlink(__DIR__ . '/convert/User.dcm.yml');
@unlink(__DIR__ . '/convert/Profile.dcm.yml');
@rmdir(__DIR__ . '/convert');
}
}