这是我最初的问题的后续问题: 将 mysql 枚举迁移到使用 symfony2 的理论实体的最佳策略?
我已经按照此处的说明成功添加了枚举数据类型:http: //docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/cookbook/mysql-enums.html 更具体地说,我使用了使用后半部分的解决方案 2 我创建了一个基本 EnumType 类,然后将其扩展为:
<?php
namespace CP\AdminBundle\DataTypes;
class EnumContactBasicGender extends EnumType
{
protected $name = 'EnumContactBasicGender';
protected $values = array('m','f');
}
我的 EnumType 类看起来像:
<?php
namespace CP\AdminBundle\DataTypes;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
abstract class EnumType extends Type
{
protected $name;
protected $values = array();
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$values = array_map(function($val) { return "'".$val."'"; }, $this->values);
return "ENUM(".implode(", ", $values).") COMMENT '(DC2Type:".$this->name.")'";
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value;
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (!in_array($value, $this->values)) {
throw new \InvalidArgumentException("Invalid enum value given: '".$this->value."' for enum: '".$this->name."'");
}
return $value;
}
public function getName()
{
return $this->name;
}
}
然后我像这样注册了我的枚举:
<?php
namespace CP\AdminBundle;
use Doctrine\DBAL\Types\Type;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class CPAdminBundle extends Bundle
{
public function boot()
{
$em = $this->container->get('doctrine.orm.entity_manager');
Type::addType('EnumContactBasicGender', 'CP\AdminBundle\DataTypes\EnumContactBasicGender');
Type::addType('EnumContactBasicType', 'CP\AdminBundle\DataTypes\EnumContactBasicType');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('EnumContactBasicGender','EnumContactBasicGender');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('EnumContactBasicType','EnumContactBasicType');
}
}
当我运行命令./app/console dictionary:generate:entities CP时,它起作用了!它使用新的枚举数据类型创建我的实体,它毫无问题地创建了 setter 和 getter,我的一个实体如下所示:
<?php
namespace CP\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* CP\AdminBundle\Entity\ContactBasic
*
* @ORM\Table(name="contacts_basics")
* @ORM\Entity(repositoryClass="CP\AdminBundle\Entity\ContactBasicRepository")
*/
class ContactBasic
{
/** Field #1 (contacts_id) **/
/**
* @var integer $contacts_id
* @ORM\Column(type="integer",nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $contacts_id;
/** enum ('m','f') **/
/**
* @ORM\Column(type="EnumContactBasicGender",nullable=false)
*/
private $gender;
/** enum ('non-customer','customer','blacklisted') **/
/**
* @ORM\Column(type="EnumContactBasicType",nullable=false)
*/
private $type;
/** more omitted properties here **/
/**
* Set gender
*
* @param EnumContactBasicGender $gender
*/
public function setGender(\EnumContactBasicGender $gender)
{
$this->gender = $gender;
}
/**
* Get gender
*
* @return EnumContactBasicGender
*/
public function getGender()
{
return $this->gender;
}
/**
* Set type
*
* @param EnumContactBasicType $type
*/
public function setType(\EnumContactBasicType $type)
{
$this->type = $type;
}
/**
* Get type
*
* @return EnumContactBasicType
*/
public function getType()
{
return $this->type;
}
/** more omitted setters and getters here **/
}
当我创建一个夹具加载器时,我的问题出现了,我不知道如何设置任何枚举值以将它们保存到数据库中,到目前为止,我的夹具加载器类如下所示:
<?php
namespace CP\AdminBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use CP\AdminBundle\Entity\ContactBasic;
use CP\AdminBundle\DataTypes\EnumContactBasicGender;
use CP\AdminBundle\DataTypes\EnumContactBasicType;
class ContactBasicLoader implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$contact = new ContactBasic();
// $contact->setGender(new EnumContactBasicGender());
$contact->setGender('m');
// $contact->setType(new EnumContactBasicType());
$contact->setType('customer');
$manager->persist($contact);
$manager->flush();
}
}
当我运行./app/console dictionary:fixtures:load时,我收到以下错误:
Catchable Fatal Error: Argument 1 passed to CP\AdminBundle\Entity\ContactBasic::setGender() must be an instance of EnumContactBasicGender, string given
但是如果我像上面的注释行一样通过类,我也会得到一个错误。
有人可以解释一下我需要如何设置所需的值吗?