<?php
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\ORM\Mapping as A;
/**
* your own doctrine type for multiple files in a directory
*/
class DirType extends Type
{
// the type of your db that should be used (here: VARCHAR)
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
}
// get your value php ready
public function convertToPHPValue($value, AbstractPlatform $platform)
{
// be sure to have a valid path
return new DirectoryIterator($value);
}
// get your value sql ready
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
// be sure you have a DirectoryIterator
return $value->getPath();
}
// name of your new doctrine type
public function getName()
{
return 'dir';
}
}
/**
* Entity that uses the new type
*
* @A\Entity
* @A\Table(name="budgie")
*/
class Budgie
{
/**
* @A\Column(type="integer", name="id")
* @A\Id
* @A\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* the dir type creates a DirectoryIterator from saved directory path
* use a constructor to preset it with a default value
*
* @A\Column(type="dir", length=255, name="budgie_pics")
*/
public $budgiePics;
}
// add your type to doctrine
Type::addType('dir', DirType::CLASS);
$entityManager->getConnection()
->getDatabasePlatform()
->registerDoctrineTypeMapping('DirType', 'dir');
// get sql create query from Budgie entity
$tool = new SchemaTool($entityManager);
$classes = array(
$entityManager->getClassMetadata(Budgie::CLASS),
);
$queries = $tool->getCreateSchemaSql($classes);
echo SqlFormatter::format($queries[0]);
CREATE TABLE budgie ( id INT AUTO_INCREMENT NOT NULL, budgie_pics VARCHAR(255) NOT NULL, PRIMARY KEY(id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB