php - symfony 2 - 一对一映射

标签 php symfony

当我想使用自动生成的表单添加新的用户配置文件时,出现以下错误。错误是: 需要一个对象,但得到一个集合。您是否忘记将“multiple=true”传递给实体字段?

我正在使用Symfony 2.0.12

这是我的 UserProfile 实体的代码。该问题发生在 Controller 中,但我不知道如何解决。请帮忙。

<?php

namespace Infidia\OutsourceBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Infidia\OutsourceBundle\Entity\UserProfile
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Infidia\OutsourceBundle\Entity\UserProfileRepository")
 */
class UserProfile
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 *
 * @ORM\OneToOne(targetEntity="User")
 * @orm:JoinColumn(name="userid", referencedColumnName="id")
 */
private $user;

/**
 * @var  integer $userid
 */
private $userid;

/**
 * @var text $street_address
 *
 * @ORM\Column(name="street_address", type="text")
 */
private $street_address;

/**
 * @var string $city
 *
 * @ORM\Column(name="city", type="string", length=255)
 */
private $city;

/**
 * @var string $country
 *
 * @ORM\Column(name="country", type="string", length=255)
 */
private $country;

/**
 * @var string $zipcode
 *
 * @ORM\Column(name="zipcode", type="string", length=255)
 */
private $zipcode;

/**
 * @var string $timezone
 *
 * @ORM\Column(name="timezone", type="string", length=12)
 */
private $timezone;

/**
 * @var string $contact_no
 *
 * @ORM\Column(name="contact_no", type="string", length=20)
 */
private $contact_no;

/**
 * @var date $birthday
 *
 * @ORM\Column(name="birthday", type="date")
 */
private $birthday;

/**
 * @var string $gender
 *
 * @ORM\Column(name="gender", type="string", length=15)
 */
private $gender;


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}


/**
 * Set street_address
 *
 * @param text $streetAddress
 */
public function setStreetAddress($streetAddress)
{
    $this->street_address = $streetAddress;
}

/**
 * Get street_address
 *
 * @return text 
 */
public function getStreetAddress()
{
    return $this->street_address;
}

/**
 * Set city
 *
 * @param string $city
 */
public function setCity($city)
{
    $this->city = $city;
}

/**
 * Get city
 *
 * @return string 
 */
public function getCity()
{
    return $this->city;
}

/**
 * Set country
 *
 * @param string $country
 */
public function setCountry($country)
{
    $this->country = $country;
}

/**
 * Get country
 *
 * @return string 
 */
public function getCountry()
{
    return $this->country;
}

/**
 * Set zipcode
 *
 * @param string $zipcode
 */
public function setZipcode($zipcode)
{
    $this->zipcode = $zipcode;
}

/**
 * Get zipcode
 *
 * @return string 
 */
public function getZipcode()
{
    return $this->zipcode;
}

/**
 * Set timezone
 *
 * @param string $timezone
 */
public function setTimezone($timezone)
{
    $this->timezone = $timezone;
}

/**
 * Get timezone
 *
 * @return string 
 */
public function getTimezone()
{
    return $this->timezone;
}

/**
 * Set contact_no
 *
 * @param string $contactNo
 */
public function setContactNo($contactNo)
{
    $this->contact_no = $contactNo;
}

/**
 * Get contact_no
 *
 * @return string 
 */
public function getContactNo()
{
    return $this->contact_no;
}

/**
 * Set birthday
 *
 * @param date $birthday
 */
public function setBirthday($birthday)
{
    $this->birthday = $birthday;
}

/**
 * Get birthday
 *
 * @return date 
 */
public function getBirthday()
{
    return $this->birthday;
}

/**
 * Set gender
 *
 * @param string $gender
 */
public function setGender($gender)
{
    $this->gender = $gender;
}

/**
 * Get gender
 *
 * @return string 
 */
public function getGender()
{
    return $this->gender;
}

/**
 * Set experience
 *
 * @param \Infidia\OutsourceBundle\Entity\text $experience
 */
public function setExperience($experience)
{
    $this->experience = $experience;
}

/**
 * Get Experience
 *
 * @return \Infidia\OutsourceBundle\Entity\text
 */
public function getExperience()
{
    return $this->experience;
}

/**
 * Set UserId
 *
 * @param int $userid
 */
public function setUserid($userid)
{
    $this->userid = $userid;
}

/**
 * Get UserId
 * @return int
 */
public function getUserid()
{
    return $this->userid;
}

public function __construct()
{
    $this->user = new ArrayCollection();
}

/**
 * Set user
 *
 * @param Infidia\OutsourceBundle\Entity\User $user
 */
public function setUser(\Infidia\OutsourceBundle\Entity\User $user)
{
    $this->user = $user;
}

/**
 * Get user
 *
 * @return Infidia\OutsourceBundle\Entity\User 
 */
public function getUser()
{
    return $this->user;
}
}

最佳答案

问题出在你的表格上...... 试试这个。

 public function buildForm(FormBuilder $builder, array $options) {


        $builder
->add('employeeType', 'entity', array(
                    'class' => 'MyProject\EntityBundle\Entity\Employee',
                    'property' => 'name',
                      'multiple'=>'true','query_builder' => function ($repository) { return $repository->createQueryBuilder('es')->orderBy('es.name', 'ASC'); },

}

关于php - symfony 2 - 一对一映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10401442/

相关文章:

php - 在 PHP 正则表达式中转义反斜杠 [\] 的正确方法?

javascript - 如何让 select onchange 事件在页面加载时发生?

php - 对数据库中的数据进行排序 (DESC/ASC) 并使用 JSON 发送它们

php - Doctrine :架构:更新不是更新表

symfony - 在 Symfony2 中包含来自包的资源的正确方法

Symfony5 形式 : how to disable "required" for all fields by default?

php - 交响乐2 : Doctrine constraints in forms

php - 如何转义 pdo 中的字符串?

php - 按小数点对齐表格列中的小数数据,HTML5,CSS3

symfony-forms - Symfony 表单生成器默认按 EntityType 选择