-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Composite primary key & association entities as keys improvements #1113
Conversation
Hello, thank you for creating this pull request. I have automatically opened an issue http://www.doctrine-project.org/jira/browse/DDC-3258 We use Jira to track the state of pull requests and the versions they got |
@goetas can you add a functional test that shows how this API is supposed to work at repository or collection level? |
* | ||
* @return ORMException | ||
*/ | ||
public static function cantUseInOperatorOnComposteKeys() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo. Missing i
@stof Thanks for reviewing my code |
It would be nice to have: //Admin1 is an entity with a composite key (country+code)
//Admin1AlternativeNames is an entity with a simple key (id) + Admin1 as association
$compositeEntity = $em->getRepository('Admin1')
->findOneBy(array('country' => 'IT', 'id' => 1));
$dql = $em->getRepository('Admin1AlternativeNames')
->createQueryBuilder('n')
->andWhere("n.admin1 = :admin1")
->setParameter("admin1", $compositeEntity); but at the moment seems to be complicated for me... :-( |
I think that postgres failure is not related to my changes... |
@goetas no I think this is related to the recent merge of doctrine/dbal#444 |
@goetas there's no BC break, since we do not allow custom persisters (yet) |
@guilhermeblanco good to know! |
@@ -413,6 +422,7 @@ public function loadAll(array $criteria = array(), array $orderBy = null, $limit | |||
$rsm = $this->getResultSetMapping(); | |||
$querykey = new QueryCacheKey($hash, 0, Cache::MODE_NORMAL); | |||
$queryCache = $this->cache->getQueryCache($this->regionName); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
white line.. i will fix it :-(
Hi everybody! Now it should be complete! |
@@ -232,6 +233,14 @@ public function storeEntityCache($entity, EntityCacheKey $key) | |||
$class = $this->metadataFactory->getMetadataFor($className); | |||
} | |||
|
|||
if ($class->containsForeignIdentifier) { | |||
foreach ($class->associationMappings as $name => $assoc) { | |||
if (isset($assoc['id']) && $assoc['id'] && !isset($assoc['cache'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use ! empty($assoc['id'])
instead of isset($assoc['id']) && $assoc['id']
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to know! I will do that!
* | ||
* @param string $field | ||
* @param mixed $value | ||
* | ||
* @return integer | ||
* @return array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would document it as integer[]
which explains what type is inside the array too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getTypes
will return strings and integers... integers will be only when $value
is an array
News on this? |
*/ | ||
public static function cantUseInOperatorOnCompositeKeys() | ||
{ | ||
return new self("Can't use IN operator on entities that have composte keys."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo on "composite" word
@Ocramius I can try to adapt it, but the current implementation of
|
This is something that I discussed in the PR that introduced it ( #1143 ): needs improvement
Making it recursive seems acceptable to me here: what's the problem with that? We could also make the entity identifiers accessible to it.
Should be improved also in the
It was just introduced, so it is obviously not yet used across the system :-)
Can you clarify on this? What would be a failure case? |
@@ -2639,10 +2625,18 @@ public function createEntity($className, array $data, &$hints = array()) | |||
|
|||
if ($joinColumnValue !== null) { | |||
if ($targetClass->containsForeignIdentifier) { | |||
if ($joinColumnValue instanceof AssociationCacheEntry) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really not proud of this. Ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@FabioBatSilva maybe got ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ocramius It was my mistake... fixed with goetas@268426a
/cc @FabioBatSilva
Ok, now looks better |
@@ -335,7 +335,11 @@ protected function fetchVersionValue($versionedClass, $id) | |||
. ' FROM ' . $tableName | |||
. ' WHERE ' . implode(' = ? AND ', $identifier) . ' = ?'; | |||
|
|||
$flatId = $this->identifierFlattener->flattenIdentifier($versionedClass, (array) $id); | |||
if (!is_array($id)) { | |||
$id = array($this->class->identifier[0] => $id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@goetas this is very naive, and assumes a single column identifier.
What if I try to fetch the version of an entity with a composite identity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may also avoid calling this method with a mixed
$id
, and change its signature to enforce array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the assignDefaultVersionValue
signature, simplified a lot! Good advice! :-D
Now I'm more satisfied with the pull request status! |
Manually merged after a rebase at f908974 |
Thanks @goetas, as usual nice work :-) |
Hi!
I tried to implement the matching of entities that uses composite primary keys...
Any suggestion?
BC changes here https://github.com/doctrine/doctrine2/pull/1113/files#diff-c18001a2ca9928743b1e99854ffad33bL1611 (protected method makred as private and changed return type..)