Symfony2 和 Doctrine2 在持久化带有鉴别器映射列的对象方面存在一个小问题。我已经搜索了几个小时,但找不到答案。
我正在使用的代码如下 - 我正在尝试保留一个新用户,并基于它的“用户类型”(如表格中声明的那样),新的教师/学生/随便什么。
如果有人好心帮助我,你能否解释一下你为什么要这样做?只是这样我才能理解所有这些 ORM 的东西,这对我来说是很新的。
这是我的 SystemUser 类
/**
* SystemUser
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Bundle\MainBundle\Entity\Repository\SystemUserRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="integer")
* @ORM\DiscriminatorMap({"0" = "SystemUser", "1" = "SchoolAdmin", "2" = "Teacher", "3" = "Student", "4" = "Guardian"})
*/
class SystemUser implements AdvancedUserInterface, \Serializable {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(type="string", length=50)
*/
protected $username;
/**
* @var string
*
* @ORM\Column(type="string", length=255)
*/
protected $email;
/**
* @var string
*
* @ORM\Column(type="string", length=32)
*/
protected $salt;
/**
* @var string
*
* @ORM\Column(type="string", length=64)
*/
protected $password;
/**
* @var bool
*
* @ORM\Column(type="boolean", name="is_active")
*/
protected $isActive;
/**
* @var string
* @ORM\Column(name="birth_date", type="date")
*/
protected $birthDate;
/**
* @var string
* @ORM\Column(name="cellphone", type="string", length=10)
*/
protected $cellphone;
/**
* @var ArrayCollection
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*/
protected $roles;
/**
*
*
*
*
* Begin methods
*
*
*
*/
public function __construct() {
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
$this->roles = new ArrayCollection();
}
... 其他方法被忽略了,因为它们似乎都可以正常工作。请注意,鉴别器没有明确的列,尽管它仍然通过注释在模式中生成它。这个对吗?
现在我的表单类型...
class UserFormType extends AbstractType {
private $router;
private $securityContext;
public function __construct(Router $router, SecurityContextInterface $securityContext) {
$this->router = $router;
$this->securityContext = $securityContext;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder->setAction($this->router->generate('register_new_user'))
->add('username', 'text')
->add('email', 'email')
->add('birth_date', 'date', array(
'widget' => 'single_text',
))
->add('password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
))
->add('cellphone', 'text', array(
'max_length' => 10,
'invalid_message' => 'A phone number must be exactly 10 characters long',
));
//Add additional stuff here like access role, status, account active, etc...
$builder->add('roles', null, array(
'required' => true,
'multiple' => true,
'label' => 'Role'
))
->add('is_active', 'checkbox', array(
'required' => true,
'label' => 'Active'
))
//Here is where I get the issue mentioned below
->add('discr', 'choice', array(
'choices' => array(
'1' => 'School Administrator',
'2' => 'Teacher',
'3' => 'Student',
'4' => 'Guardian'
, ),
'multiple' => false,
'label' => 'User Type'
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'SCWORX\MainBundle\Entity\SystemUser',
));
}
public function getName() {
return 'user_form';
}
}
在我添加 discr 选择字段的地方,我收到以下错误:
Neither the property "discr" nor one of the methods "getDiscr()", "isDiscr()", "hasDiscr()", "__get()" or "__call()" exist and have public access in class "Bundle\MainBundle\Entity\SystemUser".
最后,注册表在这里...
class RegistrationFormType extends AbstractType {
private $router;
private $securityContext;
public function __construct(Router $router, SecurityContextInterface $securityContext) {
$this->router = $router;
$this->securityContext = $securityContext;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder->setAction($this->router->generate('create_user'))
->add('user', 'user_form')
->add('submit', 'submit');
}
public function getName() {
return 'user_registration';
}
}
我可能应该注意到这两种形式都已注册为服务。
谢谢你的帮助!