Symfony get curent entity from custom DBAL type

267 Views Asked by At

I have class that define custom type and i want to make validation based on class that call that type.

Its in purpose of having 2 tables when one is managed by symfony and other is not for yet.

The table that not managed by symfony ned value of 0, when is null.

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)
    {
        #some logic
        if ($entity instanceof ClassNotManagedBySymfony) {
            return $value === null? 0: (int)$value;
        }
        return $value
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        #some logic
        if ($entity instanceof ClassNotManagedBySymfony) {
            return $value === 0? null: (int)$value;
        }
        return $value;
    }
}

Edit

Question:

Its posible to get Entity instance inside custom type?

class User extends ClassNotManagedBySymfony
{
    /**
     * @var int
     *
     * @ORM\Column(name="entities_id", type="nullidentity")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
}

0

There are 0 best solutions below