I've got a Doctrine2 Entity called "Order", which has several status properties. The allowed status' are stored in a different Entity, so there is a ManyToOne relation defined for those entities.
/**
* #ORM\Entity()
*/
class Order extends AbstractEntity {
// ...
/**
* #ORM\ManyToOne(targetEntity="Status")
* #ORM\JoinColumn(onDelete="NO ACTION", nullable=false)
*/
protected $status;
/** #ORM\Column(nullable=true) */
protected $stringProperty = "default value";
}
I need to set this status property to a default value when creating a new instance of the order object.
For a "non-relation" property I can simply set it like the $stringProperty above. But how to do it for relations?
I cannot set the value to the id of the related record, as Doctrine2 will complain.
It's fine if the configured default is a "Reference" to the status entity. The available status' are fixed and won't change (often).
How do I configure the entity to have a proper default relation configured.
Preferably not in a listener when persisting, as the status may be requested before that.
There are several approaches but I would suggest using the OrderRepository as a factory for creating new orders.
class OrderRepository
{
public function create()
{
$order = new Order();
$status = $this->_em->find('Status','default'); // or getReference
$order->setStatus($status);
return $order;
}
}
// In a controller
$orderRepository = $this->container->get('order_repository');
$order = $orderRepository->create();
By going with a repository you can initialize complex entity graphs that will be ready for persisting.
==========================================================================
Plan B would be to do this sort of thing within the order object and then use listeners to "fix things up" before persisting or updating.
class Order
{
public function __construct()
{
$this->status = new Status('Default');
}
}
The problem of course is that a default status object already exists in the database so when you flush you will get a error. So you need to hang an onFlush(http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush) listener on the entity manager, check to see if the status object is being managed by the entity manager and, if not, replace it with a managed object fetched via the entity manager.
This approach lets you deal with more "pure" domain models without worrying as much about the persistence layer. On the other hand, dealing with the flush can be tricky. On the gripping hand, once you get it working then it does open up some major possibilities.
========================================================
There is also the question of what exactly the status entity does. If all it contains is some sort of status state ('entered',processed') etc. Then you might consider just having it be a string. Sort of like the ROLE_USER objects.
Related
We are using Symfony for our projects and there's something about Doctrine that I can't get on with.
Doctrine's entity manager (lets call it 'em' in the following) is a shared service, so when I inject em into multiple services, they share exactly the same instance of em. It is simpler If I introduce an example right away to explain what I want to ask: Consider the following example:
$service1 = $this->get('vendor_test.service_one'); // $service1 has a private entity manager property
$service2 = $this->get('vendor_test.service_two'); // $service2 as well has a private entity manager property
$entity1 = $service1->getEntityById(1); // getEntityById() queries for an entity with the given id and returns it. So it is in the managed list of service1's entity manager
$entity2 = $service2->getEntityById(2); // entity1 and entity2 not necessarily of the same class
$entity1
->setProperty1('aaaa')
->setProperty2($service2->updateDateTime($entity2)) // updateDateTime() let's say updates a datetime field of the passed entity (in this case entity2) and calls $this->entityManager->flush(); and returns the datetime.
->setProperty3('bbbb')
$service1->save(); // calls $this->entityManager->flush() so it should update the managed entities (in this case entity1)
So the question is: If the entityManager object of service1 and service2 are the same instance of entityManager so they are identical, they share the same internal managed list, then when calling $service2->updateDateTime($entity2) does an entityManager->flush(), does it flushes $entity1 as well? Does $entity1 with Property1 set to 'aaaa' being flushed midway and updated in the database, and being flushed in a second step when $service1->save(); is called?
Hope I managed to draw up what I mean and what I want to ask.
As I tested out and asked someone more competent, the answer is yes, since everywhere I use entity manager they all share the same managed list. To overcome the problem mentioned in the question, is to pass the entity to be flushed to the entity manager and all the others will be intact.
I have a cached entity Product. I retrieve the entity and update some properties including adding price into it.
Relationship is set up like this
class Product
{
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="Price", mappedBy="product", fetch="EXTRA_LAZY")
*/
private $prices;
}
class Price
{
/**
* #var Product
*
* #ORM\ManyToOne(targetEntity="Product", inversedBy="prices")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
}
I am trying to save properties like this.
// Here $em is Entity Manager
$cacheDriver = $em->getConfiguration()->getResultCacheImpl();
$product = $cacheDriver->fetch($key)
$product->setUpdatedAt(new \DateTime());
$price = new Price;
$label = $em->getReference(PriceLabelEntity::class, $labelId);
$price->setLabel($label)
->setPriceLabelId($labelId)
->setProduct($productEntity)
->setProductId($product->getId());
$em->persist($price);
$product->addPrice($price);
$em->flush();
But whenever I do that I get the exception saying.
A new entity was found through the relationship 'Price#product' that was not
configured to cascade persist operations for entity:
Product#0000000043c0cdc400007fec6b41ca76. To solve this issue: Either
explicitly call EntityManager#persist() on this unknown entity or configure
cascade persist this association in the mapping for example
#ManyToOne(..,cascade={"persist"}). If you cannot find out which entity
causes the problem implement 'Product#__toString()' to get a clue.
Even more strange issue if I do just this.
$product->setUpdatedAt($timeStamp);
$em->flush();
It does not throw any error but no data is saved in DB. Not sure whether this issues are related or not either.
I tried to put cascade too but it gives different error. Is there any way to resolve this issue.
It is my understanding that Doctrine entity managers are fairly adept (at least, one would hope) at managing their result cache, so fiddling with it directly may be the issue.
Instead of hitting the result cache directly, try using:
$product = $entityManager->find('Product',$key);
Then, you also seem to have a typo on the last line - missing an "e" in "entityManager" - but I am sure that would throw an error, so it's probably a copy/paste issue when creating this question.
EDIT: You are also using both $product and $productEntity. If these are meant to be the same variable, you should pick one name and stick with it.
#Rushing thank you very much for your reply. You are right there are some typos because I had to do some changes in code to paste in StackOverflow but the actual code is correct. I am sorry I was bit late to reply on this topic. The solution you provided was good but even better solution was to use merge like this.
$productEntity = $em->merge($productEntity);
....rest of the logic
In this way we did not have to query the database again and entity was fresh.
I'm trying to use in memory test with NHibernate, and i succeeded to do that in this little project :
https://github.com/demojag/NHibernateInMemoryTest
As you can see from the map of the object i had to comment this line :
//SchemaAction.None(); Test will fail. this option hide the schema exportation.
this comment is just i guess I've made because so far i haven't found serious documentation about Schema Actions.
I'm doing those tests because i have an existing situation i would like to test in memory but all the entity maps have the option SchemaActions.None(), and when i try to execute the in memory test i get a lot of "no such tables".
I would like to know if exist a way to keep the Schema action option set to none and export the schema anyway ? (i know that can be an encapsulation violation so it would not really have a lot of sense).
I would like to leave this option set to none because is a "DatabaseFirst" application , and i can't take the risk to drop the database and re create it every time the configuration is build, but i guess, if in the configuration i don't specify the instruction "exposeConfiguration" and SchemaExport, i can be pretty safe.
Thank you in advice
Giuseppe.
You should be able to override any and all settings in the HBM or Fluent NHibernate mappings via the NHibernate.Cfg.Configuration.BeforeBindMapping event which gives you programmatic runtime access to NHibernate's internal model for a mapping. See the example below which sets up BeforeBindMapping event handler which overrides the SchemaAction specified in the mapping to whatever you want.
public NHibernate.Cfg.Configuration BuildConfiguration()
{
var configuration = new NHibernate.Cfg.Configuration();
configuration.BeforeBindMapping += OnBeforeBindMapping;
// A bunch of other stuff...
return configuration;
}
private void OnBeforeBindMapping( object sender, NHibernate.Cfg.BindMappingEventArgs bindMappingEventArgs )
{
// Set all mappings to use the fully qualified namespace to avoid class name collision
bindMappingEventArgs.Mapping.autoimport = false;
// Override the schema action to all
foreach ( var item in bindMappingEventArgs.Mapping.Items )
{
var hbmClass = item as NHibernate.Cfg.MappingSchema.HbmClass;
if ( hbmClass != null )
{
hbmClass.schemaaction = "all";
}
}
}
I am importing data into a new Symfony2 project using Doctrine2 ORM.
All new records should have an auto-generated primary key. However, for my import, I would like to preserve the existing primary keys.
I am using this as my Entity configuration:
type: entity
id:
id:
type: integer
generator: { strategy: AUTO }
I have also created a setter for the id field in my entity class.
However, when I persist and flush this entity to the database, the key I manually set is not preserved.
What is the best workaround or solution for this?
The following answer is not mine but OP's, which was posted in the question. I've moved it into this community wiki answer.
I stored a reference to the Connection object and used that to manually insert rows and update relations. This avoids the persister and identity generators altogether. It is also possible to use the Connection to wrap all of this work in a transaction.
Once you have executed the insert statements, you may then update the relations.
This is a good solution because it avoids any potential problems you may experience when swapping out your configuration on a live server.
In your init function:
// Get the Connection
$this->connection = $this->getContainer()->get('doctrine')->getEntityManager()->getConnection();
In your main body:
// Loop over my array of old data adding records
$this->connection->beginTransaction();
foreach(array_slice($records, 1) as $record)
{
$this->addRecord($records[0], $record);
}
try
{
$this->connection->commit();
}
catch(Exception $e)
{
$output->writeln($e->getMessage());
$this->connection->rollBack();
exit(1);
}
Create this function:
// Add a record to the database using Connection
protected function addRecord($columns, $oldRecord)
{
// Insert data into Record table
$record = array();
foreach($columns as $key => $column)
{
$record[$column] = $oldRecord[$key];
}
$record['id'] = $record['rkey'];
// Insert the data
$this->connection->insert('Record', $record);
}
You've likely already considered this, but my approach would be to set the generator strategy to 'none' for the import so you can manually import the existing id's in your client code. Then once the import is complete, change the generator strategy back to 'auto' to let the RDBMS take over from there. A conditional can determine whether the id setter is invoked. Good luck - let us know what you end up deciding to use.
Using doctrine 2.1 (and zend framework 1.11, not that it matters for this matter), how can I do post persist and post update actions, that involves re-saving to the db?
For example, creating a unique token based on the just generated primary key' id, or generating a thumbnail for an uploaded image (which actually doesn't require re-saving to the db, but still) ?
EDIT - let's explain, shall we ?
The above is actually a question regarding two scenarios. Both scenarios relate to the following state:
Let's say I have a User entity. When the object is flushed after it has been marked to be persisted, it'll have the normal auto-generated id of mysql - meaning running numbers normally beginning at 1, 2, 3, etc..
Each user can upload an image - which he will be able to use in the application - which will have a record in the db as well. So I have another entity called Image. Each Image entity also has an auto-generated id - same methodology as the user id.
Now - here is the scenarios:
When a user uploads an image, I want to generate a thumbnail for that image right after it is saved to the db. This should happen for every new or updated image.
Since we're trying to stay smart, I don't want the code to generate the thumbnail to be written like this:
$image = new Image();
...
$entityManager->persist($image);
$entityManager->flush();
callToFunctionThatGeneratesThumbnailOnImage($image);
but rather I want it to occur automatically on the persisting of the object (well, flush of the persisted object), like the prePersist or preUpdate methods.
Since the user uploaded an image, he get's a link to it. It will probably look something like: http://www.mysite.com/showImage?id=[IMAGEID].
This allows anyone to just change the imageid in this link, and see other user's images.
So in order to prevent such a thing, I want to generate a unique token for every image. Since it doesn't really need to be sophisticated, I thought about using the md5 value of the image id, with some salt.
But for that, I need to have the id of that image - which I'll only have after flushing the persisted object - then generate the md5, and then saving it again to the db.
Understand that the links for the images are supposed to be publicly accessible so I can't just allow an authenticated user to view them by some kind of permission rules.
You probably know already about Doctrine events. What you could do:
Use the postPersist event handler. That one occurs after the DB insert, so the auto generated ids are available.
The EventManager class can help you with this:
class MyEventListener
{
public function postPersist(LifecycleEventArgs $eventArgs)
{
// in a listener you have the entity instance and the
// EntityManager available via the event arguments
$entity = $eventArgs->getEntity();
$em = $eventArgs->getEntityManager();
if ($entity instanceof User) {
// do some stuff
}
}
}
$eventManager = $em->getEventManager():
$eventManager->addEventListener(Events::postPersist, new MyEventListener());
Be sure to check e. g. if the User already has an Image, otherwise if you call flush in the event listener, you might be caught in an endless loop.
Of course you could also make your User class aware of that image creation operation with an inline postPersist eventHandler and add #HasLifecycleCallbacks in your mapping and then always flush at the end of the request e. g. in a shutdown function, but in my opinion this kind of stuff belongs in a separate listener. YMMV.
If you need the entity id before flushing, just after creating the object, another approach is to generate the ids for the entities within your application, e. g. using uuids.
Now you can do something like:
class Entity {
public function __construct()
{
$this->id = uuid_create();
}
}
Now you have an id already set when you just do:
$e = new Entity();
And you only need to call EntityManager::flush at the end of the request
In the end, I listened to #Arms who commented on the question.
I started using a service layer for doing such things.
So now, I have a method in the service layer which creates the Image entity. After it calls the persist and flush, it calls the method that generates the thumbnail.
The Service Layer pattern is a good solution for such things.