why does doctrine think a existed entity as a new entity? - symfony

I have two entities , user and store, they have a many-to-one relationship, before I create a user, I have to make sure a store is existed, it is not allowed to create a store while creating a user ,that means cascade={"persist"} can't be used.
Store class
public function addUser(User $user)
{
if (!$this->users->contains($user))
{
$this->users->add($user);
$user->setStore($this);
}
return $this;
}
before I create a user , I am pretty sure that a store is already existed.these code below is the way I used to create user
$store= $this->get('vmsp.store_provider')->getCurrentStore();
$store->addUser($user);
$userManager->updateUser($user);
code in updateUser method is not special:
$this->entityManager->persist($user);
$this->entityManager->flush();
code in getCurrentStore method:
public function getCurrentStore($throwException=true)
{
if (isset(self::$store)) {
return self::$store;
}
$request = $this->requestStack->getCurrentRequest();
$storeId = $request->attributes->get('storeId', '');
$store = $this->entityRepository->find($storeId);
if ($store === NULL&&$throwException) {
throw new NotFoundHttpException('Store is not found');
}
self::$store = $store;
return $store;
}
this gives me a error:
A new entity was found through the relationship
'VMSP\UserBundle\Entity\User#store' that was not configured to cascade
persist operations for entity: ~ #1. 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"})
thing is getting very interesting, why does a existed store become new entity? why does doctrine think that existed store entity as a new entity?

It seems like your Store-entity is detached from the EntityManager somehow. I can't really see where it happens. Finding that out will probably take a few debugging sessions by you.
A quick fix might be to merge the user's store back into the EntityManager using EntityManager::merge($entity), e.g. in your updateUser-method:
public function updateUser(User $user) {
$store = $user->getStore();
$this->entityManager->merge($store);
$this->entityManager->persist($user);
$this->entityManager->flush();
}
You might also want to play around with Doctrine's UnitOfWork especially with getState($entity, $assumedState) to find out whether your store is still managed or not.

Related

How to override the PUT operation update process in the Api Platform

I'm trying to override the PUT operation to perform my actions under certain conditions. That is, if the sent object is different from the original object (from the database), then I need to create a new object and return it without changing the original object.
Now when I execute the query I get a new object, as expected, but the problem is that the original object also changes
Entity
#[ApiResource(
operations: [
new Get(),
new GetCollection(),
new Post(controller: CreateAction::class),
new Put(processor: EntityStateProcessor::class),
],
paginationEnabled: false
)]
class Entity
EntityStateProcessor
final class PageStateProcessor implements ProcessorInterface
{
private ProcessorInterface $decorated;
private EntityCompare $entityCompare;
public function __construct(ProcessorInterface $decorated, EntityCompare $entityCompare)
{
$this->decorated = $decorated;
$this->entityCompare = $entityCompare;
}
public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
{
if (($this->entityCompare)($data)) { // checking for object changes
$new_entity = clone $data; // (without id)
// do something with new entity
return $this->decorated->process($new_entity, $operation, $uriVariables, $context);
}
return $data;
}
}
I don't understand why this happens, so I return a clone of the original object to the process. It would be great if someone could tell me what my mistake is.
I also tried the following before returning the process
$this->entityManager->refresh($data); - Here I assumed that the original instance of the object will be updated with data from the database and the object will not be updated with data from the query
$this->entityManager->getUnitOfWork()->detach($data); - Here I assumed that the object would cease to be manageable and would not be updated
But in both cases the state of the original $data changes.
I'm using ApiPlatform 3.0.2
The error is that the main entity is related to an additional entity, so it's not enough to detach the main entity from UnitOfWork. So use the Doctrine\ORM\UnitOfWork->clear(YourEntity::class) method to detach all instances of the entity, and you do the same for relationships.
Once the entity is detach, cloning the entity becomes pointless because the previous entity instance isn't managed by the Doctrine ORM, so my code rearranges itself like this:
public function process($data, Operation $operation, array $uriVariables = [], array $context = [])
{
if (($this->entityCompare)($data)) { // checking for object changes
$this->getEntityManager()->getUnitOfWork()->clear(Entity::class);
$this->getEntityManager()->getUnitOfWork()->clear(EelatedEntity::class);
// do something with new entity
return $this->decorated->process($data, $operation, $uriVariables, $context);
}
return $data;
}

Symfony 4 - Compare two objects before persisting?

I've a Symfony 4 project.
In my controller, I've an action to edit an object in my database :
/**
* Editer un groupe
*
* #Route("/admin/validation/{id}", name="admin_validation_edit")
*
* #param GroupeValidateurs $groupeValidateurs
* #return void
*/
public function edit(GroupeValidateurs $groupeValidateurs, Request $request)
{
$form = $this->createForm(GroupeValidateursType::class, $groupeValidateurs);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//persist and flush in database
}
But when my form is submitted, I would like to compare my old and my new object, to make some actions.
For that, I used a session vairable :
$session = $this->get('session');
if (!$session->has('groupe')) {
$session->set('groupe', $groupeValidateurs);
}
And I remove it when the form is submitted and valid.
It works, but it's not correct, because, if I go on an edit page with some groupeValidateur, and just after, I go on another edit page with another groupeValidateur, My session variable will contains my previous groupeValidateur.
Which solution can I use please ?
before $form->handleRequest($request) your object $groupeValidateurs is still the original.
If you want to keep some information there are several options, amongst those the very easy and straightforward:
handle outside object and outside the form component:
if I understood you correctly, you only want to prevent certain users to be added/removed. Since I don't know your entity, I will assume, that your object has a method getUsers(), that returns the current users.
$oldUsers = $groupeValidateurs->getUsers(); // I assume $oldUsers is an ARRAY***
$form = $this->createForm(...)
//...
if($form->isSubmitted() && $form->isValid()) {
$newUsers = $groupeValidateurs->getUsers();
// do whatever ...
}
***) if this is a OneToMany or ManyToMany relation, make sure to return the array instead of the collection:
public function getUsers() {
return $this->users instanceof \Doctrine\Common\Collections\Collection
? $this->users->toArray()
: $this->users;
}
if you manage to keep $this->users as a Collection always, you can just return $this->users->toArray();
other options:
add event listeners to the form, that capture the data before edits come in, add this to a constraint that gets an additional list of users to prevent from being added/removed
IF there is a property on the user which makes it clear, the user shall not be removed ever, you can bake this into your add/removeUser function:
function removeUser($user) {
if($user->isAdmin()) {
return;
}
$this->users->removeElement($user); // I assume users to be a doctrine Collection
}
function setUsers($users) {
foreach($this->users as $user) {
if($user->isAdmin() && !in_array($users)) {
$users[] = $user; // add user;
}
}
$this->users = $users;
}
note: depending on your form, you might have to set by_reference to false. which imho is not a real problem if a) your getUsers() returns the array instead of the collection (how it should be) or b) if you implement addUser/removeUser.
also, this approach has the obvious caveat, that nobody can remove that user without removing the admin privilege, so maybe this is overkill ;o)
setup a doctrine event listener for updates on your entity type that checks for removed users and re-add them accordingly. for this to work, you either have to check the changesets somehow (this is quite the overkill probably)
upon changing the users of an object, store the old version (if add/remove implementation, take care not to overwrite the backup) of user list
implement clone on your entity properly and actually produce a copy of your object before getting it changed (by handleRequest). compare at will.
get the original entity as Andrea Manzi described and compare with that.
In edit "action" try using:
if ($form->isSubmitted() && $form->isValid()) {
$currentdata = $form->getData();
/**/
}
to get current data submitted
Or write an "update" action like this:
public function update(Request $request, $id)
{
/* #var $em EntityManager */
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository(Entity::class)->find($id); //your GroupeValidateurs entity class
$form = $this->createForm(GroupeValidateursType::class, $groupeValidateurs);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//persist and flush in database
$currentdata = $form->getData();
$beforedata = $em->getUnitOfWork()->getOriginalEntityData($entity);
/*....*/
}
Clone object to $cloneGroupeValidateurs. Set entity to form. Next submitting your form and compare with previously cloned variable.

Best practice Symfony2 entity management

There is a lots of ways to manage entities in Symfony2, but I don't know which is the better
Solution 1: In the controller
public function myAction()
{
$myEntity = new MyEntity();
$form = $this->createForm($myEntityType, $myEntity);
...
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager()
$em->persist($myEntity);
$em->flush()
}
...
}
Solution 2: Using custom entityManager
public function myAction()
{
$myEntityManager = $this->get('manager.my_entity');
$myEntity = $myEntityManager->create();
$form = $this->createForm($myEntityType, $myEntity);
...
if ($form->isValid()) {
$myEntityManager->update($myEntity);
}
...
}
Solution 3: Using factory
public function myAction()
{
$myEntityFactory = $this->get('factory.my_entity');
$myEntity = $myEntityFactory->create();
$form = $this->createForm($myEntityType, $myEntity);
...
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager()
$em->persist($myEntity);
$em->flush()
}
...
}
I prefer the solution 2, but peoples told me this is not a single responsibility pattern because you have a factory and a method to update into it. The solution is maybe to use the factory in the manager, but it brings a lits of complexity.
Controller
public function myAction()
{
$myEntity = $this->get('entity_repository')->getBySomething();
$form = $this->createForm($myEntityType, $myEntity);
...
if ($form->isValid()) {
$this->get('entity_persister')->updateEntity($myEntity);
}
...
}
Your repository has the only responsability to give you model objects.
The domain service 'entity_persister' has the responsability to persist the given data to the model
Note that this solution is not the cleanest way because your form is directly mapped to an object representing the database table. I would recommend if you want to have a cleaner architecture to keep the model object and the object mapped by the form different.
Depends solely on a use case, IMO. There's no single "best way".
The question is: what do you actually do with this entity in your app? And how many times you need to access the same methods?
If only in one place, then creating a service or anything outside a controller could be totally uncessary.
Factory design pattern has its own usages, so it's a separate matter to be analysed (regardless the entity).
I'd go with 1st option for most of use cases, though, when you need to just create an Entity:
$myEntity = new MyEntity();
Why? Because code doesn't hide anything. Come on, MyEntity is just a Plain Object, it doesn't need any service or manager or (in most cases) factory. The other two seem to be a bad practice then, as they hide what is really going on there (unless Factory is needed).

Symfony2: creating a new SecurityIdentity

I'm using ACL in Symfony 2.1, and I need to create a new SecurityIdentity, so that my ACL can be set in function of some sort of groups.
Picture the following situation: there are groups with users (with different roles) that each have user information. In group 1, users with the ROLE_ADMIN can't edit other users from the same group's information, but in group 2, users with ROLE_ADMIN can edit others information.
So basically my ACL will vary in function of what group the user is in.
I thought I'd start solving this problem with the creation of a new "GroupSecurityIdentity". However the class itself doesn't suffice, as I get this exception when I use it:
$sid must either be an instance of UserSecurityIdentity, or RoleSecurityIdentity.
My question is: how do I "register" my new SecurityIdentity so I can use it as RoleSecurityIdentity and UserSecurityIdentity?
What better ways are there to implement a system similar to this I want to do?
2 years ago I went down that path, it turned out to be a bad decision. Modifying the ACL system is difficult and might cause problems when updating Symfony. There are at least 2 better solutions. I'll list them all so you can decide which best suits your needs.
New security identity
I'm using the GroupInterface from FOSUserBundle, but I guess you could use your own too. The following files need to be added:
AclProvider.php
The method to change is private - the whole file has to be copied, but the only change has to be made to hydrateObjectIdentities
GroupSecurityIdentity.php
MutableAclProvider.php
We have to duplicate the whole file as it must extend AclProvider, but we're using a custom one and can't therefore extend the stock MutableAclProvider. The methods changed are getInsertSecurityIdentitySql and getSelectSecurityIdentityIdSql.
SecurityIdentityRetrievalStrategy.php
Next up: rewire the dependency injection container by providing the following parameters:
<parameter key="security.acl.dbal.provider.class">
Acme\Bundle\DemoBundle\Security\Acl\Dbal\MutableAclProvider
</parameter>
<parameter key="security.acl.security_identity_retrieval_strategy.class">
Acme\Bundle\DemoBundle\Security\Acl\Domain\SecurityIdentityRetrievalStrategy
</parameter>
Time to cross fingers and see whether it works. Since this is old code I might have forgotten something.
Use roles for groups
The idea is to have group names correspond to roles.
A simple way is to have your User entity re-implement UserInterface::getRoles:
public function getRoles()
{
$roles = parent::getRoles();
// This can be cached should there be any performance issues
// which I highly doubt there would be.
foreach ($this->getGroups() as $group) {
// GroupInterface::getRole() would probably have to use its
// canonical name to get something like `ROLE_GROUP_NAME_OF_GROUP`
$roles[] = $group->getRole();
}
return $roles;
}
A possible implementation of GroupInterface::getRole():
public function getRole()
{
$name = $this->getNameCanonical();
return 'ROLE_GROUP_'.mb_convert_case($name, MB_CASE_UPPER, 'UTF-8');
}
It's now just a matter of creating the required ACE-s as written in the cookbook article.
Create a voter
Finally, you could use custom voters that check for the presence of specific groups and whether the user has access to said object. A possible implementation:
<?php
namespace Acme\Bundle\DemoBundle\Authorization\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class MySecureObjectVoter implements VoterInterface
{
/**
* {#inheritDoc}
*/
public function supportsAttribute($attribute)
{
$supported = array('VIEW');
return in_array($attribute, $supported);
}
/**
* {#inheritDoc}
*/
public function supportsClass($class)
{
return $class instanceof GroupableInterface;
}
/**
* {#inheritDoc}
*/
public function vote(TokenInterface $token, $object, array $attributes)
{
$result = VoterInterface::ACCESS_ABSTAIN;
if (!$object instanceof MySecureObject) {
return VoterInterface::ACCESS_ABSTAIN;
}
foreach ($attributes as $attribute) {
if (!$this->supportsAttribute($attribute)) {
continue;
}
// Access is granted, if the user and object have at least 1
// group in common.
if ('VIEW' === $attribute) {
$objGroups = $object->getGroups();
$userGroups = $token->getUser()->getGroups();
foreach ($userGroups as $userGroup) {
foreach ($objGroups as $objGroup) {
if ($userGroup->equals($objGroup)) {
return VoterInterface::ACCESS_GRANTED;
}
}
}
return voterInterface::ACCESS_DENIED;
}
}
}
}
For more details on voters please refer to the cookbook example.
I would avoid creating a custom security identity. Use the two other methods provided. The second solution works best, if you will be having lots of records and each of them must have different access settings. Voters could be used for setting up simple access granting logic (which most smaller systems seem to fall under) or when flexibility is necessary.
I write my answer here to keep a track of this error message.
I implemented group support with ACL and i had to hack a bit the symfony core "MutableAclProvider.php"
protected function getSelectSecurityIdentityIdSql(SecurityIdentityInterface $sid)
{
if ($sid instanceof UserSecurityIdentity) {
$identifier = $sid->getClass().'-'.$sid->getUsername();
$username = true;
} elseif ($sid instanceof RoleSecurityIdentity) {
$identifier = $sid->getRole();
$username = false;
}else {
//throw new \InvalidArgumentException('$sid must either be an instance of UserSecurityIdentity, or RoleSecurityIdentity.');
$identifier = $sid->getClass().'-'.$sid->getGroupname();
$username = true;
}
return sprintf(
'SELECT id FROM %s WHERE identifier = %s AND username = %s',
$this->options['sid_table_name'],
$this->connection->quote($identifier),
$this->connection->getDatabasePlatform()->convertBooleans($username)
);
}
Even if the provided object is not an instance of UserSecurityIdentity or RoleSecurityIdentity it return a value. So now i can use a custom "GroupSecurityIdentity"
It's not easy to put in place but was much adapted to my system.

Infinite Loop when persisting entity with Event Listeners

I am using Symfony 2 with Doctrine 2. I have a UserListener (symfony docs page) that listens to the PrePersist & PreRemove events for User objects. When Persisting a User, I want to create a UserInventory instance for the User. UserInventory is the owning side of the (uni-directional) association.
However with this setup, I encounter an infinite loop:
class UserListener {
/**
* Initializes UserInventory for user with initial number of nets
*/
public function prePersist(LifecycleEventArgs $args) {
$em = $args->getEntityManager();
$user = $args->getEntity();
$inventory = new UserInventory();
$inventory->setUser($user);
$inventory->setNumNets($this->initialNets);
$em->persist($inventory); // if I comment out this line, it works but the inventory is not persisted
$em->flush();
}
}
It might be that UserInventory is the owning side of the association thus, it tries to persist the user again resulting in this function called again? How can I fix this?
I want my UserInventory to own the association here because its in the "correct" bundle. I have a UserBundle but I dont think the Inventory class should be there.
UPDATE: Error/Log
You added a listener for all entites in your application. Of course, when you persist any object, e.g. UserInventory, prePersist will be called again and again.
As the symfony documentation says you can simply do a check:
if ($user instanceof User) {
$inventory = new UserInventory();
$inventory->setUser($user);
$inventory->setNumNets($this->initialNets);
$em->persist($inventory);
}
Also, I recommend to read about events in doctrine2.

Resources