0

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;
}

  • I don't see any question and I don't see any error message, also I apparently struggle to understand the problem due to language barrier. Would you please rephrase, because as far as I can tell, it's hard to help, if the problem is not understandable ;o/ – Jakumi Nov 28 '20 at 11:16
  • @Jakumi its this what you wanted? – Vladyslav Levenets Nov 28 '20 at 12:04
  • so a type essentially is usually just column entry. so in your symfony-managed table that's alright. To turn a singular value into an object, the approach with `#some logic` sounds good in principle, however, it's not *quite* simple to get anything inside a type, since it's already hard to call its constructor. this might help: https://stackoverflow.com/questions/38230885/dependency-injection-with-custom-doctrine-2-data-type once you have an object property with some repository/database connection, the rest should be easy. – Jakumi Nov 28 '20 at 12:28

0 Answers0