Override association mapping of a trait property in Doctrine 2.6
Prerequisites:
I wonder if it's possible to override an inversedBy
attribute of a property association mapping that's taken from a trait.
An interface that I use as a concrete user entity placeholder:
ReusableBundleModelEntrantInterface.php
interface EntrantInterface
{
public function getEmail();
public function getFirstName();
public function getLastName();
}
User
entity that implements EntrantInterface
and all other entities that are derived from these abstract classes in AppBundle
): ReusableBundleEntityEntry.php
/**
* @ORMMappedSuperclass
*/
abstract class Entry
{
/**
* @var EntrantInterface
*
* @ORMManyToOne(targetEntity="ReusableBundleModelEntrantInterface", inversedBy="entries")
* @ORMJoinColumn(name="user_id")
*/
protected $user;
// getters/setters...
}
ReusableBundleEntityTimestamp.php
/**
* @ORMMappedSuperclass
*/
abstract class Timestamp
{
/**
* @var EntrantInterface
*
* @ORMManyToOne(targetEntity="ReusableBundleModelEntrantInterface", inversedBy="timestamps")
* @ORMJoinColumn(name="user_id")
*/
protected $user;
// getters/setters...
}
And couple more entities with similar structure that utilize EntranInterface
UserAwareTrait
to be reusable across several entities: ReusableBundleEntityTraitsUserAwareTrait.php
trait UserAwareTrait
{
/**
* @var EntrantInterface
*
* @ORMManyToOne(targetEntity="ReusableBundleModelEntrantInterface")
* @ORMJoinColumn(name="user_id")
*/
protected $user;
// getter/setter...
}
In Doctrine 2.6 if I would use super class and wanted to override its property I'd do this:
/**
* @ORMMappedSuperclass
* @ORMAssociationOverrides({
* @ORMAssociationOverride({name="property", inversedBy="entities"})
* })
*/
abstract class Entity extends SuperEntity
{
// code...
}
But if I want that Entity to use UserAwareTrait
and override association mapping of a property...
/**
* @ORMMappedSuperclass
* @ORMAssociationOverrides({
* @ORMAssociationOverride({name="user", inversedBy="entries"})
* })
*/
abstract class Entry
{
use UserAwareTrait;
// code...
}
... and run php bin/console doctrine:schema:validate
I see this error in the console:
[DoctrineORMMappingMappingException]
Invalid field override named 'user' for class 'ReusableBundleEntityEntry'.
Is there a workaround that I could follow to achieve the desired result?
Use trait to store shared properties
Override assotiation mapping or (possibly) attributes mapping in the class that uses that trait
TL;DR You should change the access modificator from protected
to private
. Don't forget that you will not be able to directly manipulate the private property in a subclass and will need a getter.
The exception appears due to the bug (I believe, or a quirk of the behavior) in the AnnotationDriver
.
foreach ($class->getProperties() as $property) {
if ($metadata->isMappedSuperclass && ! $property->isPrivate()
||
...) {
continue;
}
It skips all non-private properties for MappedSuperclass
letting them to compose metadata on the subclass parsing. But when it comes to overriding the driver tries to do it at a MappedSuperclass
level, it doesn't remember that the property was skipped, fails to find it in the metadata and raise an exception.
I made a detailed explanation at the issue. You can find there also the link to the unit tests that highlight the case.
You'll have to try this in your own code to see, but it could be possible.
As an experiment, I overridden a trait in a class, then checked for the trait using class_uses()
http://php.net/manual/en/function.class-uses.php
<?php
trait CanWhatever
{
public function doStuff()
{
return 'result!';
}
}
class X
{
use CanWhatever;
public function doStuff()
{
return 'overridden!';
}
}
$x = new X();
echo $x->doStuff();
echo "n$x has ";
echo (class_uses($x, 'CanWhatever')) ? 'the trait' : 'no trait';
This outputs:
overridden!
$x has the trait
Which you can see here https://3v4l.org/Vin2H
However, Doctrine Annotations may still pick up the DocBlock from the trait proper rather than the overridden method, which is why I can't give you a definitive answer. You just need to try it and see!
我有一个类似的问题,并通过覆盖它自己的属性来解决它:
use UserAwareTrait;
/**
* @var EntrantInterface
* @ORMManyToOne(targetEntity="ReusableBundleModelEntrantInterface"inversedBy="entries")
*/
protected $user;
链接地址: http://www.djcxy.com/p/69388.html
上一篇: 调用非成员函数