我有一个带有下拉列表和一组复选框的表单,我使用了实体表单字段类型来通过DB获取值。它适用于其中一个实体但不适用于另一个实体。我在 AddBadgesType 中单独使用此代码 没有AddBadges实体
<?php
namespace Test\Bundle\TestBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class AddBadgesType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('property', 'entity', array(
'class' => 'TestBundle:Property',
'property' => 'id',
'query_builder' => function (\Doctrine\ORM\EntityRepository $er) {
return $er->createQueryBuilder('property')
->orderBy('property.id ', 'ASC');
},
))
->add('badges', 'entity', array(
'class' => 'TestBundle:Badge',
'property' => 'name',
'multiple' => true,
'expanded' => true,
))
;
}
/**
* @return string
*/
public function getName()
{
return 'test_bundle_testbundle_AddBadges';
}
}
出现此错误
无法转换数据库值&#34;&#34;到Doctrine Type数组
这是属性的恩赐
<?php
namespace Test\Bundle\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Property
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Test\Bundle\TestBundle\Entity\PropertyRepository")
*/
class Property
{
public function __construct() {
$this->campus = new ArrayCollection();
$this->tag = new ArrayCollection();
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="University", inversedBy="property")
* @ORM\JoinColumn(name="university_id", referencedColumnName="id")
*/
private $university;
/**
* @ORM\ManyToMany(targetEntity="Tag")
*/
private $tag;
/**
* @ORM\Column(name="badges", type="array")
*/
private $badges;
/**
* @ORM\ManyToMany(targetEntity="Campus")
*/
private $campus;
/**
* @var string
* @ORM\Column(name="address", type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(
* min = 10,
* max = 80,
* minMessage = "Address must be more specific",
* maxMessage = "80 characters limit exceeded"
* )
*/
private $address;
/**
* @var integer
* @ORM\Column(name="postalcode", type="integer")
* @Assert\NotBlank()
* @Assert\Length(
* min = 3,
* minMessage = "Password must contain atleast 3 digits"
* )
*/
private $postalcode;
/**
* @var integer
* @Assert\NotBlank()
* @ORM\Column(name="propertytype", type="integer")
*/
private $propertytype;
/**
* @var integer
* @Assert\NotBlank()
* @ORM\Column(name="rent", type="integer")
*/
private $rent;
/**
* @var integer
* @Assert\NotBlank()
* @ORM\Column(name="leaseperiod", type="integer")
*/
private $leaseperiod;
/**
* @var integer
* @Assert\NotBlank()
* @ORM\Column(name="utilities", type="integer")
*/
private $utilities;
/**
* @var integer
* @Assert\NotBlank()
* @ORM\Column(name="bedrooms", type="integer")
*/
private $bedrooms;
/**
* @var integer
* @Assert\NotBlank()
* @ORM\Column(name="bathrooms", type="integer")
*/
private $bathrooms;
/**
* @var string
* @ORM\Column(name="additionaldetails", type="string", length=255)
* @Assert\Length(
* min = 10,
* max = 200,
* minMessage = "Details must be more specific",
* maxMessage = "200 characters limit exceeded"
* )
*/
private $additionaldetails;
/**
* Get id
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set address
*
* @param string $address
* @return Property
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set postalcode
*
* @param string $postalcode
* @return Property
*/
public function setPostalcode($postalcode)
{
$this->postalcode = $postalcode;
return $this;
}
/**
* Get postalcode
*
* @return string
*/
public function getPostalcode()
{
return $this->postalcode;
}
/**
* Set propertytype
*
* @param integer $propertytype
* @return Property
*/
public function setPropertytype($propertytype)
{
$this->propertytype = $propertytype;
return $this;
}
/**
* Get propertytype
*
* @return integer
*/
public function getPropertytype()
{
return $this->propertytype;
}
/**
* Set rent
*
* @param string $rent
* @return Property
*/
public function setRent($rent)
{
$this->rent = $rent;
return $this;
}
/**
* Get rent
*
* @return string
*/
public function getRent()
{
return $this->rent;
}
/**
* Set leaseperiod
*
* @param integer $leaseperiod
* @return Property
*/
public function setLeaseperiod($leaseperiod)
{
$this->leaseperiod = $leaseperiod;
return $this;
}
/**
* Get leaseperiod
*
* @return integer
*/
public function getLeaseperiod()
{
return $this->leaseperiod;
}
/**
* Set utilities
*
* @param integer $utilities
* @return Property
*/
public function setUtilities($utilities)
{
$this->utilities = $utilities;
return $this;
}
/**
* Get utilities
*
* @return integer
*/
public function getUtilities()
{
return $this->utilities;
}
/**
* Set bedrooms
*
* @param integer $bedrooms
* @return Property
*/
public function setBedrooms($bedrooms)
{
$this->bedrooms = $bedrooms;
return $this;
}
/**
* Get bedrooms
*
* @return integer
*/
public function getBedrooms()
{
return $this->bedrooms;
}
/**
* Set bathrooms
*
* @param integer $bathrooms
* @return Property
*/
public function setBathrooms($bathrooms)
{
$this->bathrooms = $bathrooms;
return $this;
}
/**
* Get bathrooms
*
* @return integer
*/
public function getBathrooms()
{
return $this->bathrooms;
}
/**
* Set additionaldetails
*
* @param string $additionaldetails
* @return Property
*/
public function setAdditionaldetails($additionaldetails)
{
$this->additionaldetails = $additionaldetails;
return $this;
}
/**
* Get additionaldetails
*
* @return string
*/
public function getAdditionaldetails()
{
return $this->additionaldetails;
}
/**
* Set university
*
* @param \Test\Bundle\TestBundle\Entity\Univerisity $university
* @return Property
*/
public function setUniversity(\Test\Bundle\TestBundle\Entity\University $university = null)
{
$this->university = $university;
return $this;
}
/**
* Get university
*
* @return \Test\Bundle\TestBundle\Entity\Univerisity
*/
public function getUniversity()
{
return $this->university;
}
/**
* Add campus
*
* @param \Test\Bundle\TestBundle\Entity\Campus $campus
* @return Property
*/
public function addCampus(\Test\Bundle\TestBundle\Entity\Campus $campus)
{
$this->campus[] = $campus;
return $this;
}
/**
* Remove campus
*
* @param \Test\Bundle\TestBundle\Entity\Campus $campus
*/
public function removeCampus(\Test\Bundle\TestBundle\Entity\Campus $campus)
{
$this->campus->removeElement($campus);
}
/**
* Get campus
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCampus()
{
return $this->campus;
}
/**
* Add tag
*
* @param \Test\Bundle\TestBundle\Entity\Tag $tag
* @return Property
*/
public function addTag(\Test\Bundle\TestBundle\Entity\Tag $tag)
{
$this->tag[] = $tag;
return $this;
}
/**
* Remove tag
*
* @param \Test\Bundle\TestBundle\Entity\Tag $tag
*/
public function removeTag(\Test\Bundle\TestBundle\Entity\Tag $tag)
{
$this->tag->removeElement($tag);
}
/**
* Get tag
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTag()
{
return $this->tag;
}
/**
* Set badges
*
* @param array $badges
* @return Property
*/
public function setBadges($badges)
{
$this->badges = $badges;
return $this;
}
/**
* Get badges
*
* @return array
*/
public function getBadges()
{
return $this->badges;
}
}
但是,如果我将实体更改为&#39; Campus&#39;它工作正常,我认为它可能与属性实体中的ManyToMany关系有关?!
校园实体供参考
<?php
namespace Test\Bundle\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Campus
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Test\Bundle\TestBundle\Entity\CampusRepository")
*/
class Campus
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="University", inversedBy="campus")
* @ORM\JoinColumn(name="university_id", referencedColumnName="id")
*/
private $university;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Assert\NotBlank
* @Assert\Length(
* min=3,
* max=35,
* minMessage= "Name Field should contains at least 3 characters",
* maxMessage = "Name Field Cannot contain more than 35 characters"
* )
* @Assert\Regex(pattern="/[^a-z\s-]/i", match=false , message="Name can only contain letters")
*/
private $name;
/**
* @var string
* @ORM\Column(name="address", type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(
* min = 10,
* max = 80,
* minMessage = "Address must be more specific",
* maxMessage = "80 characters limit exceeded"
* )
*/
private $address;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Campus
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* @param string $address
* @return Campus
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set university
*
* @param \Test\Bundle\TestBundle\Entity\University $university
* @return Campus
*/
public function setUniversity(\Test\Bundle\TestBundle\Entity\University $university = null)
{
$this->university = $university;
return $this;
}
/**
* Get university
*
* @return \Test\Bundle\TestBundle\Entity\University
*/
public function getUniversity()
{
return $this->university;
}
}
答案 0 :(得分:1)
您没有在表单文件的顶部包含Symfony EnityRepository类,因此PHP在与表单类相同的目录中查找它。因此错误消息。将其添加到您的表单类(或限定EntityRepository inline):
use Doctrine\ORM\EntityRepository;