2015-06-22 82 views
-1

我有一个下拉式窗体和一组复选框,我使用实体表单字段类型来通过数据库获取值。它与其中一个实体协同工作,但与另一个协同工作。我有这样的代码seperately内AddBadgesType 没有AddBadges实体Symfony 2错误消息

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

与此错误

无法数据库值 “” 转换成学说类型数组

这里财产共享

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

但是,如果我将实体更改为'校园',它工作正常,我认为这可能是与属性实体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

您的PropertyRepository类是否扩展了EntityRepository?你是否在文件的开头使用了正确的EntityRepository的命名空间? – 2015-06-22 08:53:53

+1

请显示文件和命名空间语句的路径和名称,可能它们有问题。 –

+0

@ michal.hubczyk注意命名空间和文件名错误,我检查过,因为它与不同的实体一起工作,所以它不是命名空间问题 –

回答

1

校园实体,以便PHP在相同的目录中的表单类寻找它您还没有包括在你的表单文件的顶部的Symfony EnityRepository类。因此错误信息。将此添加到您的表单类(或者内联EntityRepository):

use Doctrine\ORM\EntityRepository; 
+0

我没有自定义属性库,它确实扩展EntityRepository与正确的命名空间 –

+0

编辑答案 - 这不是扩展,这是问题,它根本没有找到EntityRepository类。 – redbirdo

+0

我也编辑了我的问题,现在:P新错误... –