doubinduo3364 2012-05-01 17:13
浏览 48
已采纳

symfony 2 - 一对一映射

I am getting the following error, when I want to add new UserProfile using autogenerated form.The error is: Expected an object, but got a collection. Did you forget to pass "multiple=true" to an entity field?

I am using Symfony 2.0.12

This is my code for the UserProfile entity. The issue occurred in Controller, but I am not sure how to solve it. Please help.

<?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;
}
}
  • 写回答

2条回答 默认 最新

  • dongtangu8403 2012-05-02 04:19
    关注

    The problem is in your form... Try this.

     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'); },
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3