I have a User Entity in a small Framework made by me. Now i want to use this User Entity in several projects.
But in some projects I want to add a few fields to the User Entity without modifying the file.
What I tried so far:
I created a new DefaultUser Entity in a DefaultUser Bundle and made the User Entity a mappedsuperclass. But now I can't make a association in other entities like
/*
* #ORM\ManyToOne(targetEntity="User", inversedBy="jobs")
* #ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $user;
Because Doctrine can't find the id column in the user entity. This only works if I specify the DefaultUser Entity. According to the doctrine documentation this only works on many to many associations if only one leaf exists.
Then I tried Single Table Inheritance. This works fine but I have to modify the DiscriminatorMap if I want to extend my user entity which is shared acros multiple projects...
So whats the best way to extend the UserEntity?
I have precisely the same problem - I have just switched from RedBean to Doctrine (for a project using the Zend Framework), and the structure of my classes did not take into account this issue. The core problem is that maps in Doctrine have a one to one relationship with classes, as far as I can work out. What we are looking for is a way to have one concrete class (the UserEntity) that uses a map from an abstract class (the DefaultUser). My solution, which may be something of a hack (I've only been using Doctrine for a couple of days), works for YAML at least:
Create a new mapping driver extending the YAML driver, and override the _loadMappingFile method with something like this:
class MyLibrary_Doctrine_Mapping_Driver_YamlExtended extends MyLibrary_Doctrine_Mapping_Driver_YamlExtended
{
protected $_basicEntityFolder;
protected function _loadMappingFile($file)
{
$entMaps = parent::_loadMappingFile($file);
//merge this with any extensions if defined
foreach($entMaps as $ent => $map)
{ //load the relevant map
if (!isset($map['extendEntity'])) {
continue;
}
$fileName = $this->_basicEntityFolder . DIRECTORY_SEPARATOR . str_replace('\\', '.', $map['extendEntity']) . $this->_fileExtension;
$extendedMaps = $this->_loadMappingFile($fileName);
if (!is_array($extendedMaps[$map['extendEntity']])) {
throw new MyProject_Doctrine_Exception("Entity to extend from could not be found.");
}
//merge so that the file lower in the class hierachy always overrides the higher
$map = array_merge($extendedMaps[$map['extendEntity']], $map);
//clear the extendEntity value
unset($map['extendEntity']);
$entMaps[$ent] = $map;
}
return $entMaps;
}
public function setExtendedEntitiesFolder($path)
{
$this->_basicEntityFolder = $path;
}
}
I then have two yaml files, in different folders, like this:
#MyApplication/Entities/Maps/Entities.User.dcm.yml
Entities\User:
extendEntity: LibraryEntities\User
That is the file in the application. Then in the library I have
#Library/Entities/Maps/ExtendedEntities/LibraryEntities.User.dcm.yml
LibraryEntities\User:
type: entity
table: user
fields:
username:
type: text
nullable: true
password:
type: text
nullable: true
defaultProfile:
type: text
nullable: true
column: default_profile
The reason it is in an ExtendedEntities folder is so I can define mappedSuperclasses in the library using a normal namespace, and Doctrine will load those automatically when a class extends them, but these extendedentities are outside of the path for Doctrine's usual class inheritance loading (if they were all in the normal folder structure then for eg "class ApplicationUser extends LibraryUser" Doctrine would try to load the config for LibraryUser because it would find it, and then cause the same error you have already encountered).
Then when I set up my $em I provide it with my driver:
$driverImpl = new MyLibrary_Doctrine_Mapping_Driver_YamlExtended(array(APPLICATION_PATH . '/entities/maps',
LIBRARY_PATH . '/Entities/Maps'));
$driverImpl->setExtendedEntitiesFolder(LIBRARY_PATH . '/Entities/Maps/ExtendedEntities');
Notice that this solution allows inheritance chains defined by 'extendEntity' (because the _loadMappingFile method is recursive). Also that any configuration file lower down the chain can overwrite any property already defined, so even if in your library yaml you had:
username:
type: text
Say you had a project in which usernames where integers you can simply override it in your application config with
username:
type: int
or whatever.
And therefore this solves the problem of defining Doctrine style inheritance on the base class. In every project you can define the DiscriminatorMap however you like.
In principle the same solution could be applied to annotations, though extending the annotation driver is a little more complicated, because it doesn't simply read metadata by reading one file in one go and converting it to an array, but makes numerous requests to the annotation reader, which means implementing this structure would be trickier.
I'd be very interested to know how other people have solved this problem.
Related
I am working an Symfony 2.8 based web app project which currently uses Doctrine 2. The project is basically a simple ToDo list application which can be synced with a mobile app (iOS/Android).
While reading the Update notes of Doctrine 3 I discovered, that EntityManager::merge will no longer be supported.
An alternative to EntityManager#merge() is not provided by ORM 3.0,
since the merging semantics should be part of the business domain
rather than the persistence domain of an application. If your
application relies heavily on CRUD-alike interactions and/or PATCH
restful operations, you should look at alternatives such as
JMSSerializer.
I am not sure what is the best/correct way to replace EntityManager::merge?
Where do I use merge:
During the sync of the mobile apps with the web app the data is transferred as serialized JSON which is than de-serialized by JMSSerializer to an entity object. When the web app receives a ToDoEntry object this way, it can be a new ToDo-Entry (not known in the web app yet) or an updated existing entry. Either way, the received object is not managed by the EntityManager. Thus $em->persist($receivedObject) will always try to insert a new object. This will fail (due to the unique constraint of the id) if the ToDo-Entry already exists in the web app and needs to be updated.
Instead $em->merge($receivedObject) is used which automatically checks wether an insert or update is required.
Hot wo solve this?
Of course I could check for every received objects if an entity with the same ID already exists. In this case could load the existing object and update its properties manually. However this would be very cumbersome. The real project of course uses many different entities and each entity type/class would need its own handling to check which properties needs to be updated. Isn't there a better solution?
You can try to use registerManaged() method of Doctrine\ORM\UnitOfWork.
// $this->em <--- Doctrine Entity Manager
// $entity <--- detached Entity (and we know that this entity already exists in DB for example)
$id = [$entity->getId()]; //array
$data = $entity->toArray(); //array
$this->em->getUnitOfWork()->registerManaged($entity, $id, $data);
Of course, You can check the state of Your Entity using getEntityState() of Doctrine\ORM\UnitOfWork before/after perfoming needed actions.
$this->eM->getUnitOfWork()->getEntityState($entity, $assert = 3)
$assert <-- This parameter can be set to improve performance of entity state detection by potentially avoiding a database lookup if the distinction between NEW and DETACHED is either known or does not matter for the caller of the method.
While I have posted this question quite a while ago, it is still quite active. Until now my solution was to stick with Doctrine 2.9 and keep using the merge function. Now I am working on new project which should be Doctrine 3 ready and should thus not use the merge anymore.
My solution is of course specific for my special use case. However, maybe it is also useful for other:
My Solution:
As described in the question I use the merge method to sync deserialized, external entities into the web database where a version of this entity might already exist (UPDATE required) or not (INSERT required).
#Merge Annotation
In my case entities have different properties where some might be relevant for syncing and must be merged while others are only used for (web) internal housekeeping and must not be merged. To tell these properties appart, I have created a custom #Merge annotation:
use Doctrine\Common\Annotations\Annotation;
/**
* #Annotation
* #Target("PROPERTY")
*/
final class SyncMerge { }
This annotation is then be used to mark the entities properties which should be merged:
class ToDoEntry {
/*
* #Merge
*/
protected $date;
/*
* #Merge
*/
protected $title;
// only used internally, no need to merge
protected $someInternalValue;
...
}
Sync + Merge
During the sync process the annotation is used to merge the marked properties into existing entities:
public function mergeDeserialisedEntites(array $deserializedEntities, string $entityClass): void {
foreach ($deserializedEntities as $deserializedEntity) {
$classMergingInfos = $this->getMergingInfos($class);
$existingEntity = $this->entityManager->find($class, $deserializedEntity->getId());
if (null !== $existingEntity) {
// UPDATE existing entity
// ==> Apply all properties marked by the Merge annotation
foreach ($classMergingInfos as $propertyName => $reflectionProperty) {
$deserializedValue = $reflectionProperty->getValue($deserializedEntity);
$reflectionProperty->setValue($existingEntity, $deserializedEntity);
}
// Continue with existing entity to trigger update instead of insert on persist
$deserializedEntity = $existingEntity;
}
// If $existingEntity was used an UPDATE will be triggerd
// or an INSERT instead
$this->entityManager->persist($deserializedEntity);
}
$this->entityManager->flush();
}
private $mergingInfos = [];
private function getMergingInfos($class) {
if (!isset($this->mergingInfos[$class])) {
$reflectionClass = new \ReflectionClass($class);
$classProperties = $reflectionClass->getProperties();
$propertyInfos = [];
// Check which properties are marked by #Merge annotation and save information
foreach ($classProperties as $reflectionProperty) {
$annotation = $this->annotationReader->getPropertyAnnotation($reflectionProperty, Merge::class);
if ($annotation instanceof Merge) {
$reflectionProperty->setAccessible(true);
$propertyInfos[$reflectionProperty->getName()] = $reflectionProperty;
}
}
$this->mergingInfos[$class] = $propertyInfos;
}
return $this->mergingInfos[$class];
}
That's it. If new properties are added to an entity I have only to decide whether it should be merged or not and add the annotation if needed. No need to update the sync code.
Actually the code to handle this can be just a few lines. In background Doctrine will issue a query to search for your entity if not already in memory, so you can do the same by doing the query yourself with result cache enabled, and then just use PropertyAccessor to map the data.
https://symfony.com/doc/current/components/property_access.html
See this gist for a POC https://gist.github.com/stevro/99060106bbe54d64d3fbcf9a61e6a273
Using Symfony 2.7 and Doctrine 2.5, I have
an Interface Alsciende\MyBundle\Model\CycleInterface
an abstract class Alsciende\MyBundle\Entity\Cycle that implements the interface
a final class AppBundle\Entity\Cycle that extends the abstract class and implements the interface
a doctrine orm configuration with resolve_target_entities that maps the interface to the final class
This system works well and I was able to create the database and implements some CRUD in AppBundle, manipulating the target entity directly.
However, I now want to manipulate the target entity in MyBundle, through the Interface. I need to get its repository:
$this->getDoctrine()->getRepository('Alsciende\MyBundle\Model\CycleInterface');
But I get the exception
class 'Alsciende\MyBundle\Model\CycleInterface' does not exist
How can I get the repository of the target entity? That is, how can I call ResolveTargetEntityListener directly to get the name of the entity implementing the interface?
edit:
Why do I need that? Very simply, for example, I need a controller that displays a list of all Cycles. The interface defines that each Cycle has an id and a name. I want to display every Cycle with its name and id. In order to do that, I need to access the repository of the actual Cycle entities.
Alsciende/MyBundle/Model/CycleInterface.php
<?php
namespace Alsciende\MyBundle\Model;
interface CycleInterface
{
public function getId();
public function getName();
}
Alsciende/MyBundle/Controller/CycleController.php
<?php
namespace Alsciende\MyBundle\Controller;
class CycleController extends Controller
{
public function indexAction()
{
$cycles = $this
->getDoctrine()
->getRepository('Alsciende\MyBundle\Model\CycleInterface')
->findAll();
// return template with list $cycles
// using only id and name properties
}
}
It's the same way that FosUserBundle is able to manage the User entities, even though the User class defined in FosUserBundle is an abstract class.
How can I get the repository of the target entity?
In app/config/config.yml put:
doctrine:
orm:
resolve_target_entities:
Namespace\InterfaceInterface: Namespace\Entity\TargetEntityImplementing
BUT
Why do I need that? Very simply, for example, I need a controller that displays a list of all Cycles. The interface defines that each Cycle has an id and a name. I want to display every Cycle with its name and id. In order to do that, I need to access the repository of the actual Cycle entities.
It's not a solution in this case, IMO. I would rather used entity with #DiscriminatorColumn configured:
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance
Parent class will be some kind of interface you're looking for.
I recommend you to "merge" above: create a parent class which will implement such an interface, then map this interface to this class.
When implementing a __clone() method in media entity, I need to get the absolute path of a file to be able to make a copy of the file itself. I've been searching for a long time and I have not found any documentation to get this. Any ideas?
Here it is the __clone() method where I need the file path:
// Norwalk\StoreBundle\Entity\ProductHasMedia
public function __clone() {
if ($this->id) {
$this->media = clone $this->media;
$this->media->setProviderReference('clone_'.$this->media->getProviderReference());
$this->media->setName('clone_'.$this->media->getName());
$providerMeta = array('filename' => $this->media->getName());
$this->media->setProviderMetadata($providerMeta);
// Clone the physical image file too
$fs = new Filesystem();
$fs->copy( "original_image_path/".$this->media->getName(), "original_image_path/"."clone_".$this->media->getName());
}
}
I don't think it is possible without giving the Entity class to much responsibilities since you can't tell the absolute path name without having some services ('sonata.media.manager.media', 'sonata.media.provider.image' or 'sonata.media.twig.extension')..
The proper way to go is to build a clone function into an controller. The controller can have all the magic (services and entitymanagers) to do the cloning for you.
See this stackoverflow question for an example.
I have an existing SQL Database in which all tables have an ID column as the primary key. Can I generate an Entity Framework Model from this database AND make sure all the generated types inherit from an interface that defines the ID property?
Basically, I want everything that I return from the database to implement this:
public interface IDatabaseTable
{
public int ID { get; set; }
}
I hope you using EntityFramework 4 if so you need to use T4 templates to generate your entity and data context, you may download that from here
http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313
Than when you get it setup you would need to modify T4 template to generate inheritance.
So you would get two tamplates one template which is generating Data Context and other one which is generating Entities.
You need to modify the second one(entity generator template), go to line 41 of your entity .tt file and add your inheritance there like that:
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#> : IDatabaseTable
Some details on T4 POCO tamplates you may find here
http://sharedtolearn.blogspot.com/2010/06/entity-framework-40-and-t4-templates-to.html
http://msdn.microsoft.com/en-us/data/gg558520
See this MSDN page about partial classes.
In short, you can create a partial class for your EF entities and implement whichever interfaces you'd like.
Here's the documentation from the MSDN page:
The following are merged from all the partial-type definitions:
XML comments
interfaces
generic-type parameter attributes
class attributes
members
For example, consider the following declarations:
partial class Earth : Planet, IRotate { }
partial class Earth : IRevolve { }
They are equivalent to the following declarations:
class Earth : Planet, IRotate, IRevolve { }
Does anyone have a good way to unit test an entity's validation constraints in Symfony2?
Ideally I want to have access to the Dependency Injection Container within the unit test which would then give me access to the validator service. Once I have the validator service I can run it manually:
$errors = $validator->validate($entity);
I could extend WebTestCase and then create a client to get to the container as per the docs however it doesn't feel right. The WebTestCase and client read in the docs as more of a facility to test actions as a whole and therefore it feels broken to use it to unit test an entity.
So, does anyone know how to either a) get the container or b) create the validator inside a unit test?
Ok since this got two votes I guess other people are interested.
I decided to get my shovel out and was pleasantly surprised (so far anyway) that this wasn't at all difficult to pull off.
I remembered that each Symfony2 component can be used in a stand alone mode and therefore that I could create the validator myself.
Looking at the docs at: https://github.com/symfony/Validator/blob/master/ValidatorFactory.php
I realised that since there was a ValidatorFactory it was trivial to create a validator (especially for validation done by annotations which I am, although if you look at the docblock on the page I linked above you'll also find ways to validate xml and yml).
First:
# Symfony >=2.1
use Symfony\Component\Validator\Validation;
# Symfony <2.1
use Symfony\Component\Validator\ValidatorFactory;
and then:
# Symfony >=2.1
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
# Symfony <2.1
$validator = ValidatorFactory::buildDefault()->getValidator();
$errors = $validator->validate($entity);
$this->assertEquals(0, count($errors));
I hope this helps anyone else whose conscience wouldn't allow them to just use WebTestCase ;).
We end up rolling your own base test case to access the dependency container from within a test case. Here the class in question:
<?php
namespace Application\AcmeBundle\Tests;
// This assumes that this class file is located at:
// src/Application/AcmeBundle/Tests/ContainerAwareUnitTestCase.php
// with Symfony 2.0 Standard Edition layout. You may need to change it
// to fit your own file system mapping.
require_once __DIR__.'/../../../../app/AppKernel.php';
class ContainerAwareUnitTestCase extends \PHPUnit_Framework_TestCase
{
protected static $kernel;
protected static $container;
public static function setUpBeforeClass()
{
self::$kernel = new \AppKernel('dev', true);
self::$kernel->boot();
self::$container = self::$kernel->getContainer();
}
public function get($serviceId)
{
return self::$kernel->getContainer()->get($serviceId);
}
}
With this base class, you can now do this in your test methods to access the validator service:
$validator = $this->get('validator');
We decided to go with a static function instead of the class constructor but you could easily change the behavior to instantiate the kernel into the constructor directly instead of relying on the static method setUpBeforeClass provided by PHPUnit.
Also, keep in mind that each single test method in you test case won't be isolated fro, each others because the container is shared for the whole test case. Making modification to the container may have impact on you other test method but this should not be the case if you access only the validator service. However, this way, the test cases will run faster because you will not need to instantiate and boot a new kernel for each test methods.
For the sake of reference, we find inspiration for this class from this blog post. It is written in French but I prefer to give credit to whom it belongs :)
Regards,
Matt
I liked Kasheens answer, but it doesn't work for Symfony 2.3 anymore.
There are little changes:
use Symfony\Component\Validator\Validation;
and
$validator = Validation::createValidatorBuilder()->getValidator();
If you want to validate Annotations for instance, use enableAnnotationMapping() like below:
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
the rest stays the same:
$errors = $validator->validate($entity);
$this->assertEquals(0, count($errors));
With Symfony 2.8, it seems that you can now use the AbstractConstraintValidatorTest class this way :
<?php
namespace AppBundle\Tests\Constraints;
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
use AppBundle\Constraints\MyConstraint;
use AppBundle\Constraints\MyConstraintValidator;
use AppBundle\Entity\MyEntity;
use Symfony\Component\Validator\Validation;
class MyConstraintValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}
protected function createValidator()
{
return new MyConstraintValidator();
}
public function testIsValid()
{
$this->validator->validate(null, new MyEntity());
$this->assertNoViolation();
}
public function testNotValid()
{
$this->assertViolationRaised(new MyEntity(), MyConstraint::SOME_ERROR_NAME);
}
}
You have got a good sample with the IpValidatorTest class
The answer in https://stackoverflow.com/a/41884661/4560833 has to be changed a little for Symfony 4:
Use ConstraintValidatorTestCase instead of AbstractConstraintValidatorTest.
Answer (b): Create the Validator inside the Unit Test (Symfony 2.0)
If you built a Constraint and a ConstraintValidator you don't need any DI container at all.
Say for example you want to test the Type constraint from Symfony and it's TypeValidator. You can simply do the following:
use Symfony\Component\Validator\Constraints\TypeValidator;
use Symfony\Component\Validator\Constraints\Type;
class TypeValidatorTest extends \PHPUnit_Framework_TestCase
{
function testIsValid()
{
// The Validator class.
$v = new TypeValidator();
// Call the isValid() method directly and pass a
// configured Type Constraint object (options
// are passed in an associative array).
$this->assertTrue($v->isValid(5, new Type(array('type' => 'integer'))));
$this->assertFalse($v->isValid(5, new Type(array('type' => 'string'))));
}
}
With this you can check every validator you like with any constraint configuration. You neither need the ValidatorFactory nor the Symfony kernel.
Update: As #psylosss pointed out, this doesn't work in Symfony 2.5. Nor does it work in Symfony >= 2.1. The interface from ConstraintValidator got changed: isValid was renamed to validate and doesn't return a boolean anymore. Now you need an ExecutionContextInterface to initialize a ConstraintValidator which itself needs at least a GlobalExecutionContextInterface and a TranslatorInterface... So basically it's not possible anymore without way too much work.
I don't see a problem with the WebTestCase. If you don't want a client, don't create one ;) But using a possibly different service than your actual application will use, that's a potential pit fall. So personally, I've done like this:
class ProductServiceTest extends Symfony\Bundle\FrameworkBundle\Test\WebTestCase
{
/**
* Setup the kernel.
*
* #return null
*/
public function setUp()
{
$kernel = self::getKernelClass();
self::$kernel = new $kernel('dev', true);
self::$kernel->boot();
}
public function testFoo(){
$em = self::$kernel->getContainer()->get('doctrine.orm.entity_manager');
$v = self::$kernel->getContainer()->get('validator');
// ...
}
}
It's less DRY than Matt answer -- as you'll repeat the code (for each test class) and boot the kernel often (for each test method), but it's self-contained and require no extra dependencies, so it depends on your needs. Plus I got rid of the static require.
Also, you're sure to have the same services that your application is using -- not default or mock, as you boot the kernel in the environnement that you wish to test.
If people still read this one in 2023, prefer to inject the ValidatorInterface for Symfony > 3 / 4.
use Symfony\Component\Validator\Validator\ValidatorInterface;
// ...
$this->validator->validate($myEntity);