簡體   English   中英

Symfony 2 錯誤信息

[英]Symfony 2 error message

我有一個帶有下拉菜單和一組復選框的表單,我使用實體表單字段類型通過 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';
}
}

有這個錯誤

無法將數據庫值 "" 轉換為 Doctrine 類型數組

這是財產的實體

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

但是,如果我將實體更改為 'Campus' 它工作正常,我認為這可能與屬性實體中的多對多關系有關?!

校園實體供參考

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

}

您沒有在表單文件的頂部包含 Symfony EnityRepository 類,因此 PHP 正在與您的表單類相同的目錄中查找它。 因此出現錯誤消息。 將此添加到您的表單類(或限定 EntityRepository 內聯):

use Doctrine\ORM\EntityRepository;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM