我在向用户表单添加ImageType时遇到此错误,我使用FOSUserBundle作为用户实体!任何帮助
这是我的用户实体
<?php
namespace AclBundle\Entity;
use AclBundle\Entity\Image;
use Symfony\Component\Validator\Constraints as Assert;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* User
*
* @ORM\Table(name="ab_user")
* @ORM\Entity(repositoryClass="AclBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="cin", type="string", length=255, unique=true)
*/
private $cin;
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", length=255)
*/
private $firstname;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=255)
*/
private $lastname;
/**
* @var \DateTime
*
* @ORM\Column(name="birthdate", type="date")
*/
private $birthdate;
/**
* @var string
*
* @ORM\Column(name="phone", type="string", length=255)
*/
private $phone;
/**
* @Assert\NotBlank(message="Please, upload the picture as a image file.")
* @Assert\Image(mimeTypes={ "image/jpeg" })
* @ORM\OneToOne(targetEntity="Image", cascade={"remove", "persist"})
*
*/
private $photo;
/**
* @var \DateTime
*
* @ORM\Column(name="dateSubscribe", type="date", length=255)
*/
private $dateInscription;
/**
* @ORM\Column(name="role", type="json_array")
*/
private $role = [];
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set cin
*
* @param string $cin
*
* @return User
*/
public function setCin($cin)
{
$this->cin = $cin;
return $this;
}
/**
* Get cin
*
* @return string
*/
public function getCin()
{
return $this->cin;
}
/**
* Set firstname
*
* @param string $firstname
*
* @return User
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
*
* @return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set birthdate
*
* @param \DateTime $birthdate
*
* @return User
*/
public function setBirthdate($birthdate)
{
$this->birthdate = $birthdate;
return $this;
}
/**
* Get birthdate
*
* @return \DateTime
*/
public function getBirthdate()
{
return $this->birthdate;
}
/**
* Set phone
*
* @param string $phone
*
* @return User
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* @return mixed
*/
public function getRole()
{
return $this->role;
}
/**
* @param mixed $role
* @return string
*/
public function setRole($role)
{
$this->role = $role;
if(in_array('ROLE_SUPER_ADMIN', $this->role)) $role = 'Super Administrator';
else if(in_array('ROLE_ADMIN', $this->role)) $role = 'Administrator';
else if(in_array('ROLE_MANAGER', $this->role)) $role = 'Manager';
else if(in_array('ROLE_ASSOCIATION', $this->role)) $role = 'Association';
else if(in_array('ROLE_USER', $this->role)) $role = 'Member';
return $role;
}
/**
* Get photo
*
* @return \AclBundle\Entity\Image
*/
public function getPhoto()
{
return $this->photo;
}
/**
* @param \AclBundle\Entity\Image $photo
*
* @return User
*
*/
public function setPhoto(\AclBundle\Entity\Image $photo = null)
{
$this->photo = $photo;
}
/**
* @return \DateTime
*/
public function getDateInscription()
{
return $this->dateInscription;
}
/**
* @param \DateTime $dateInscription
*/
public function setDateInscription($dateInscription)
{
$this->dateInscription = $dateInscription;
}
public function __construct()
{
parent::__construct();
$this->dateInscription = new \DateTime('NOW');
}
}
和我的RegistrationType看起来像
<?php
namespace AclBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('cin',TextType::class)
->add('firstname',TextType::class)
->add('lastname',TextType::class)
->add('birthdate',DateType::class,[
'widget' => 'single_text',
'format' => 'dd-MM-yyyy',
'attr' => [
'class' => 'form-control input-inline date',
'data-provide' => 'datepicker',
'data-date-format' => 'dd-mm-yyyy'
],
'html5' => false,
]
)
->add('phone',TextType::class)
->add('photo',ImageType::class, array('required' => false));
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
}
我的ImageType是:
<?php
namespace AclBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ImageType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file',FileType::class);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AclBundle\Entity\Image'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'aclbundle_image';
}
}
我尝试了很多建议的解决方案,但它并不适用于我的案例! ,我认为这是一个简单的错误,但我的思绪被炸毁了