dongtang5057 2015-06-22 08:48
浏览 84
已采纳

Symfony 2错误消息

I have a form with a drop down and a set of checkboxes, i've used entity form field type to get the values via DB. it works with one of the entity but not with the other. i have this code seperately inside AddBadgesType there is NO AddBadges entity

<?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';
}
}

with this error

Could not convert database value "" to Doctrine Type array

here is the enity for property

<?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;
}
}

However if i change the entity to 'Campus' it works fine, i think it might be something to do with the ManyToMany relationship in property entity?!

Campus entity for reference

<?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;
}

}

  • 写回答

1条回答 默认 最新

  • douhuang7263 2015-06-22 08:56
    关注

    You have not included the Symfony EnityRepository class at the top of your form file so PHP is looking for it in the same directory as your form class. Hence the error message. Add this to your form class (or qualify EntityRepository inline):

    use Doctrine\ORM\EntityRepository;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)