Symfony ManyToOne relation change type

924 Views Asked by At

I have 2 entities when 1 is managed by Symfony and other is not

Entity not managed by Symfony

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="p_user")
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User
{
    /**
     * @var int
     *
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var Permission
     *
     * @ORM\ManyToOne(targetEntity="Permission", inversedBy="user", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="permission_id", referencedColumnName="permission_id", onDelete="CASCADE")
     */
    private $permission;
}

Entity managed by Symfony

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Permission
 *
 * @ORM\Table(name="p_permission")
 * @ORM\Entity(repositoryClass="App\Repository\PermissionRepository")
 */
class Permission
{
    /**
     * @var int
     *
     * @ORM\Column(name="permission_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="User", mappedBy="permission", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    private $user;
}

I need to save User entity but permission can't be null, when no relation, value is set to 0.

After my research i find that User::permission is receiving type of Permission::id, but i want that User::permission type to be nullidentity

PS: Null\IdentityType

namespace App\DBAL\Types\Null;

use Doctrine\DBAL\Types\IntegerType as DeafaultType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class IdentityType extends DeafaultType
{
    const NULLIDENTITY = 'nullidentity';

    public function getName()
    {
        return self::NULLIDENTITY;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        // dd($this);
        return $value === null? 0: (int)$value;
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value === 0? null: (int)$value;
    }
}
1

There are 1 best solutions below

7
On

In symfony the best practice to validate on a entity is the Doctrine Event Subscribers.

You can make one subscriber and check, change, delete, insert or run the code you like. You have events than triggers on persist,update or on flush.

For your problem just validate the entities and change as you like.

Edit

Add one example as requested.

First you have to register it in the service.yaml

App\EventSubscribers\EntitySubscriber:
   tags:
     - { name: doctrine.event_subscriber, connection: default}

And after create a class like this

class EntitySubscriber implements EventSubscriber{

public function getSubscribedEvents()
{
    return [
        Events::prePersist
    ];
}

public function prePersist(LifecycleEventArgs $args)
{
  /*Continue for other entities or add the user entity in the if statement*/
    if (!$args->getObject() instanceof Permission) {
        return;
    }
     
      /*Do what you need here, use the $args for more options*/

} 
}

You can find all event here