How to run a lookup query inside Doctrine2 Entity with Symfony3 - symfony

I have a basic Doctrine2 entity, but one of the fields needs some formatting applied to it to turn it from a database primary key, into a user-visible "friendly ID".
I want to put the formatting logic in only one place, so that if it ever changes, it only has to be updated once.
Part of the formatting involves looking up a string from the database and using that as a prefix, as this value will be different for different installations. I am a bit stuck, because within the entity I can't (and probably shouldn't) look up the database to retrieve this prefix.
However I am not sure how else to go about this.
Here is some pseudocode illustrating what I am trying to do:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
// This is also an entity, annotations/getters/setters omitted for brevity.
class Lookup {
protected $key;
protected $value;
}
class Person {
/**
* Database primary key
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* Get the person's display ID.
*
* #Serializer\VirtualProperty
* #Serializer\SerializedName("friendlyId")
*/
protected function getFriendlyId()
{
if ($this->person === null) return null;
//$prefix = 'ABC';
// The prefix should be loaded from the DB, somehow
$lookup = $this->getDoctrine()->getRepository('AppBundle:Lookup')->find('USER_PREFIX');
$prefix = $lookup->getValue();
return $prefix . $this->person->getId();
}
}

You could use event listeners using symfony and doctrine and listen to postLoad event by registering the service
services:
person.postload.listener:
class: AppBundle\EventListener\PersonPostLoadListener
tags:
- { name: doctrine.event_listener, event: postLoad }
Now in your listener you will have an access to entity manager
namespace AppBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use AppBundle\Entity\Person;
class PersonPostLoadListener
{
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if (!$entity instanceof Person) {
return;
}
$entityManager = $args->getEntityManager();
$lookup =$entityManager->getRepository('AppBundle\Entity\Lookup')->findOneBy(array(
'key'=> 'USER_PREFIX'
));
$entity->setFriendlyId($entity->getId().$lookup->getValue());
//echo "<pre>";dump($entity);echo "</pre>";die('Call')
}
}
And in your person entity you need to define an un mapped property for your id and its getter and setter method like
class Person
{
private $friendlyId;
public function getFriendlyId()
{
return $this->friendlyId;
}
public function setFriendlyId($friendlyId)
{
return $this->friendlyId = $friendlyId;
}
}

Related

Doctrine error with LifecycleEventArgs arguments

I want to use Listener in my project with postLoad method but I got an error
[TypeError] App\Company\Infrastructure\Persistence\ORM\EventListener\LoadLicensesListener::postLoad(): Argument #1 ($args) must be of type Do
ctrine\ORM\Event\LifecycleEventArgs, App\Company\Domain\Entity\Company given, called in D:\OpenServer\domains\project\vendor\doctrine\orm\lib\Doc
trine\ORM\Event\ListenersInvoker.php on line 108
My Listener
use Doctrine\ORM\Event\LifecycleEventArgs;
final class LoadLicensesListener
{
/**
* #param LifecycleEventArgs $args
*/
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if (!$entity instanceof Copmany) {
// Something to do
$licenses = $entity->relatedLicenses;
$entity->initializeObject($licenses);
}
}
}
And I registered it in Company.orm.xml
<entity-listeners>
<entity-listener class="App\Company\Infrastructure\Persistence\ORM\EventListener\LoadLicensesListener">
<lifecycle-callback type="postLoad" method="postLoad"/>
</entity-listener>
</entity-listeners>
services.yml
App\Company\Infrastructure\Persistence\ORM\EventListener\LoadLicensesListener:
tags:
- { name: doctrine.event_listener, event: postLoad, connection: default }
Where did I go wrong? Maybe I misunderstood the documentation - Symfony Events or Doctrine Events
Or I should do something in services.yml because I've changed a folder with EventListeners?
"doctrine/orm": "2.8.4"
Doctrine provide different type of listeners, "Default" event listener and Entity Listener, here your registered an entity listener in your file Company.orm.xml and also for the same class a "default" event listener.
Choose which type of listener you want and register it according to the documentation.
If you choose a Entity Listener then the first argument will be the Entity itself, that's why you get this error.
I would say it looks like you've configured it wrong.
try to implement postLoad method inside your Campany.php (Note! Without any params) and see what it outputs.
class Company {
// ...
public function postLoad() {
dump(__METHOD__);
}
}
also take a look at this https://symfony.com/doc/4.1/doctrine/event_listeners_subscribers.html and this one https://symfony.com/doc/current/bundles/DoctrineBundle/entity-listeners.html
I am unfortunately not familiar with xml-configs, so I can't spot anything suspicious.
As always, there are several ways to get it done:
simple EntityLifeCycles (docs) - useful for basic stuff and if you don't rely on additional services for this particular task. Logic applies only for that specific Entity.
an Doctrine\Common\EventSubscriber with getSubscribedEvents - more advanced and flexible. One logic could be applied for several entities
an EventListener.
So here are examples for symfony 4.4 and doctrine 2.7:
Entity LifeCylcles:
/**
* #ORM\Entity()
* #ORM\Table(name="company")
* #ORM\HasLifecycleCallbacks
*/
class Company {
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
// ... props and methods
/**
* #ORM\PostLoad()
*/
public function doStuffAfterLoading(): void
{
// yor logic
// you can work with $this as usual
// no-return values!
// dump(__METHOD__);
}
}
with these annotations no extra entries in services.yml|xml necessary
Subscriber - to apply same logic for one or several Entities
use App\Entity\Company;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
final class PostLoadSubscriber implements EventSubscriber {
public functuin __construct()
{
// you can inject some additional services if you need to
// e.g. EntityManager
}
public function getSubscribedEvents()
{
return [
Events::postLoad,
];
}
public function postLoad(LifecycleEventArgs $args)
{
// check if it's right entity and do your stuff
$entity = $args->getObject();
if ($entity instanceof Company) {
// magic...
}
}
}
You need to register this PostLoadSubscriber as a service in services.yaml|xml

get autogenerated id for additional field, using post-persist

i have a product with an autogenarete id and also have a productcode field, which grabs values based on user choices combined with the autogenated key to make the productcode. However i cannot grab the autogenate id when inserting a new product.
I used first prepersist & preupdate but that doesn't grab the id when inserting a new product. only when updating it grabs the id
/**
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public function setProductcode()
{
$option1 = $this->option1;
$option2 = $this->option2;
$id = $this->id;
$whole = $option1.''.$option2.''.$id;
$this->productcode = $whole;
}
i try to use postpersist, and changed my field to be nullablae true but it saves the productcode as null.
/**
* #var string
*
* #ORM\Column(type="string", length=191, unique=true, nullable=true)
*/
private $productcode;
I used postload and postpersist together and it does show the productcode as output.. but it isn't save it the db.
* #ORM\PostLoad
* #ORM\PostPersist
How can i grab the id in the entity to put it in additional field? Thanks in advance!
edit
I made an easyadminsubcriber and it works when i use the pre_persist return.
However the code below is updated to post_persist. but i have trouble implementing the flush function together with lifecycleeventargs.
i got the following error back
Argument 2 passed to App\EventSubscriber\EasyAdminSubscriber::setProductcode() must be an instance of Doctrine\Common\Persistence\Event\LifecycleEventArgs, string given, called in
below is my post_persist code
<?php
# src/EventSubscriber/EasyAdminSubscriber.php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use App\Entity\Product;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
class EasyAdminSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
'easy_admin.post_persist' => array('setProductcode'),
);
}
/**
* #param LifecycleEventArgs $args
*/
public function setProductcode(GenericEvent $event, LifecycleEventArgs $args)
{
$entityManager = $args->getEntityManager();
$entity = $event->getSubject();
if (!($entity instanceof Product)) {
return;
}
$whole = 'yooo';
$entityManager->flush();
$entity->setProductcode($whole);
$event['entity'] = $entity;
}
}
by default, the id is only set, when the entity is flushed to the database. this means, you have to generate your product code after you have flushed the entity and then flush again. doctrine can't use some fancy magic to determine the id before it actually hears back from the database, so there's not really another way. (if you want to do all of this in-entity, I can't imagine another practical and clean way to do this)
update
you should use PostPersist (while keeping PreUpdate).
The postPersist event occurs for an entity after the entity has been made persistent. It will be invoked after the database insert operations. Generated primary key values are available in the postPersist event. (source)
so, the generated primary key is available there. However, this is only after you flushed the entity. So, you'd have to flush again to write the productcode to the database as well.
create proper event handlers (because "setProductcode" is a setter, not an event handler, at least name-wise)
/**
* PostPersist triggers after the _creation_ of entities in db
* #ORM\PostPersist
*/
public function postPersist(LifecycleEventArgs $args) {
$this->setProductcode();
// need to flush, so that changes are written to database
$args->getObjectManager()->flush();
}
/**
* PreUpdate triggers before changes are written to db
* #ORM\PreUpdate
*/
public function preUpdate() {
$this->setProductcode();
// don't need to flush, this happens before the database calls
}
(see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#lifecycle-callbacks-event-argument for further information)
(disclaimer: this answer was heavily edited since it first was created, leaving the connected comments partly without relevant references)
Do you really need to persist the productcode if it is just a concatenation of other columns? What about just using an efficient getter?
public function getProductcode()
{
if(!empty($this->productcode)){
return $this->productcode;
}
if(empty($this->id)){
return "to be determined";
}
$this->productcode = $this->option1 . $this->option2 . $this->id;
return $this->productcode;
}
Alright so i have now 2 solutions to set the autogenerate id in another field (by not using the controller). First one is directly in entity file itself as shown in #jakumi answer.
public function setProductcode()
{
$part = $this->producttype->gettypenumber();
$id1 = $this->id;
$part = sprintf("%03d", $id1);
$whole = $part1.''.$part2;
return $this->productcode= $whole;
}
/**
* PostPersist triggers after the _creation_ of entities in db
* #ORM\PostPersist
*/
public function postPersist(LifecycleEventArgs $args) {
$this->setPoductcode();
// need to flush, so that changes are written to database
$args->getObjectManager()->flush();
}
/**
* PreUpdate triggers before changes are written to db
* #ORM\PreUpdate
*/
public function preUpdate() {
$this->setProductcode();
// don't need to flush, this happens before the database calls
}
Another solutions is to use the eventsubscriber.
<?php
# src/EventSubscriber/EasyAdminSubscriber.php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Product;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
class EasyAdminSubscriber implements EventSubscriberInterface
{
public function __construct(EntityManagerInterface $em) {
$this->em = $em;
}
public static function getSubscribedEvents()
{
return array(
'easy_admin.post_persist' => array('setProductcode'),
);
}
public function setProductcode(GenericEvent $event)
{
$entity = $event->getSubject();
if (!($entity instanceof Product)) {
return;
}
$this->em->flush();
$entity->setProductcode();
$this->em->flush();
}
}
and my entity code with postpersist & preupdate
/**
* #ORM\PostPersist
* #ORM\PreUpdate
*/
public function setProductcode()
{
$part1 = $entity->getProducttype()->getTypenumber();
$id1 = $entity->getId();
$part2 = sprintf("%03d", $id1);
$whole = $part1.$part2;
$this->productcode = $whole;
}
Thanks #Jakumi for explanation & guidelines for both solutions.

EasyAdmin - How to show custom Entity property properly which use EntityRepository

I would like to show on EasyAdmin a custom property, here is an example :
class Book
{
/**
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
public $id;
/**
* #ORM\Column(type="string")
*/
public $name;
/**
* #ORM\Column(type="float")
*/
public $price;
public function getBenefit(): float
{
// Here the method to retrieve the benefits
}
}
In this example, the custom parameter is benefit it's not a parameter of our Entity and if we configure EasyAdmin like that, it works !
easy_admin:
entities:
Book:
class: App\Entity\Book
list:
fields:
- { property: 'title', label: 'Title' }
- { property: 'benefit', label: 'Benefits' }
The problem is if the function is a bit complexe and need for example an EntityRepository, it becomes impossible to respect Controller > Repository > Entities.
Does anyone have a workaround, maybe by using the AdminController to show custom properties properly in EasyAdmin ?
You shouldn't put the logic to retrieve the benefits inside the Book entity, especially if it involves external dependencies like entityManager.
You could probably use the Doctrine events to achieve that. Retrieve the benefits after a Book entity has been loaded from the DB. Save the benefits before or after saving the Book entity in the DB.
You can find out more about it here https://symfony.com/doc/current/doctrine/event_listeners_subscribers.html
class Book
{
...
public $benefits;
}
// src/EventListener/RetrieveBenefitListener.php
namespace App\EventListener;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Entity\Book;
class RetrieveBenefitListener
{
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getObject();
// only act on some "Book" entity
if (!$entity instanceof Book) {
return;
}
// Your logic to retrieve the benefits
$entity->benefits = methodToGetTheBenefits();
}
}
// src/EventListener/SaveBenefitListener.php
namespace App\EventListener;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Entity\Book;
class SaveBenefitListener
{
public function postUpdate(LifecycleEventArgs $args)
{
$entity = $args->getObject();
// only act on some "Book" entity
if (!$entity instanceof Book) {
return;
}
// Your logic to save the benefits
methodToSaveTheBenefits($entity->benefits);
}
}
// services.yml
services:
App\EventListener\RetrieveBenefitListener:
tags:
- { name: doctrine.event_listener, event: postLoad }
App\EventListener\SaveBenefitListener:
tags:
- { name: doctrine.event_listener, event: postUpdate }
This is just an example, I haven't tested the code. You will probably have to add the logic for the postPersist event if you create new Book objects.
Depending on the logic to retrieve the benefits (another DB call? loading from an external API?), you might want to to approach the problem differently (caching, loading them in your DB via a cron job, ...)

Disable Doctrine Timestampable auto-updating the `updatedAt` field on certain update

In a Symfony2 project, I have a Doctrine entity that has a datetime field, called lastAccessed. Also, the entity uses Timestampable on updatedAt field.
namespace AppBundle\Entity;
use
Doctrine\ORM\Mapping as ORM,
Gedmo\Mapping\Annotation as Gedmo
;
class MyEntity {
/**
* #ORM\Column(type="datetime")
*/
private $lastAccessed;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="update")
*/
private $updatedAt;
}
I need to update the field lastAccessed without also updating the updatedAt field. How can I do that?
I just stumbled upon this as well and came up with this solution:
public function disableTimestampable()
{
$eventManager = $this->getEntityManager()->getEventManager();
foreach ($eventManager->getListeners('onFlush') as $listener) {
if ($listener instanceof \Gedmo\Timestampable\TimestampableListener) {
$eventManager->removeEventSubscriber($listener);
break;
}
}
}
Something very similar can be used to disable the blamable behavior as well of course.
There is also an easy way (or more proper way) how to do it, just override listener.
first create interface which will be implemented by entity
interface TimestampableCancelInterface
{
public function isTimestampableCanceled(): bool;
}
than extend Timestampable listener and override updateField.
this way we can disable all all events or with cancelTimestampable define custom rules for cancellation based on entity state.
class TimestampableListener extends \Gedmo\Timestampable\TimestampableListener
{
protected function updateField($object, $eventAdapter, $meta, $field)
{
/** #var \Doctrine\Orm\Mapping\ClassMetadata $meta */
$property = $meta->getReflectionProperty($field);
$newValue = $this->getFieldValue($meta, $field, $eventAdapter);
if (!$this->isTimestampableCanceled($object)) {
$property->setValue($object, $newValue);
}
}
private function isTimestampableCanceled($object): bool
{
if(!$object instanceof TimestampableCancelInterface){
return false;
}
return $object->isTimestampableCanceled();
}
}
implement interface. Most simple way is to just set property for this
private $isTimestampableCanceled = false;
public function cancelTimestampable(bool $cancel = true): void
{
$this->isTimestampableCanceled = $cancel;
}
public function isTimestampableCanceled():bool {
return $this->isTimestampableCanceled;
}
or define rules like you want
last thing is to not set default listener but ours.
I'm using symfony so:
stof_doctrine_extensions:
orm:
default:
timestampable: true
class:
timestampable: <Namespace>\TimestampableListener
Than you can just do
$entity = new Entity;
$entity->cancelTimestampable(true)
$em->persist($entity);
$em->flush(); // and you will get constraint violation since createdAt is not null :D
This way can be timestamps disabled per single entity not for whole onFlush. Also custom behavior is easy to apply based on entity state.
Timestampable is just doctrine behavior so is executed every time when you use an ORM.
In my opinion the simplest way is just using DBAL layer and raw sql query.
For example:
$sql = "UPDATE my_table set last_accessed = :lastAccess where id = :id";
//set parameters
$params['lastAccess'] = new \DateTime();
$params['id'] = $some_id;
$stmt = $this->entityManager->getConnection()->prepare($sql);
$stmt->execute($params);
You can of course put it into proper repository class

Symfony2 - Set default value in entity constructor

I can set a simple default value such as a string or boolean, but I can't find how to set the defualt for an entity.
In my User.php Entity:
/**
* #ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Foo")
*/
protected $foo;
In the constructor I need to set a default for $foo:
public function __construct()
{
parent::__construct();
$this->foo = 1; // set id to 1
}
A Foo object is expected and this passes an integer.
What is the proper way to set a default entity id?
I think you're better to set it inside a PrePersist event.
In User.php:
use Doctrine\ORM\Mapping as ORM;
/**
* ..
* #ORM\HasLifecycleCallbacks
*/
class User
{
/**
* #ORM\PrePersist()
*/
public function setInitialFoo()
{
//Setting initial $foo value
}
}
But setting a relation value is not carried out by setting an integer id, rather it's carried out by adding an instance of Foo. And this can be done inside an event listener better than the entity's LifecycleCallback events (Because you'll have to call Foo entity's repository).
First, Register the event in your bundle services.yml file:
services:
user.listener:
class: Tsk\TestBundle\EventListener\FooSetter
tags:
- { name: doctrine.event_listener, event: prePersist }
And the FooSetter class:
namespace Tsk\TestBundle\EventListener\FooSetter;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Tsk\TestBundle\Entity\User;
class FooSetter
{
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();
if ($entity instanceof User) {
$foo = $entityManager->getRepository("TskTestBundle:Foo")->find(1);
$entity->addFoo($foo);
}
}
}
I would stay well away from listeners in this simple example, and also passing the EntityManager into an entity.
A much cleaner approach is to pass the entity you require into the new entity:
class User
{
public function __construct(YourEntity $entity)
{
parent::__construct();
$this->setFoo($entity);
}
Then elsewhere when you create a new entity, you will need to find and pass the correct entity in:
$foo = [find from entity manager]
new User($foo);
--Extra--
If you wanted to go further then the creation of the entity could be in a service:
$user = $this->get('UserCreation')->newUser();
which could be:
function newUser()
{
$foo = [find from entity manager]
new User($foo);
}
This would be my preferred way
You can't just pass the id of the relationship with 'Foo'. You need to retrieve the Foo entity first, and then set the foo property. For this to work, you will need an instance of the Doctrine Entity Manager. But then, you make your entity rely on the EntityManager, this is something you don't want.
Example:
// .../User.php
public function __construct(EntityManager $em) {
$this->em = $em;
$this->foo = $this->em->getRepository('Foo')->find(1);
}

Resources