dongtao9887 2012-04-11 22:12
浏览 38
已采纳

Symfony 2表单不保存select中的数据(在连接表上)

I have created a form that appears to be correct, it has a few text fields and a select box with a list of countries pulled from a table of countries I have. The select box displays correctly using the the correct values for it's 'value' and display text. When I submit the form however I get an exception:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'countryid' cannot be null 

If I set the database table (in PHPMyAdmin) to allow a null value for the countryid field it enters the record with no exception but the entry for the countryid is null.

my controller has the following code:

        $duck = new \Wfuk\DuckBundle\Entity\Ducks();

    $form = $this->createFormBuilder($duck)
       ->add('city', 'text')
       ->add('countryid', 'entity', array('class' => 'WfukDuckBundle:Country', 'property' => 'country'))
       // cut other fields
       ->getForm();

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

    $errors = $this->get('validator')->validate( $form );

    echo $duck->getCountryid();

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($duck);
        $em->flush();
        return $this->redirect($this->generateUrl('upload_duck_success'));
        }

the echo in there returns the __toString function of the country object which seems a bit odd - but it is the full country info for the country chosen in the form.

in the Ducks.php class:

 /**
 * @var string $countryid
 *
 * @ORM\Column(name="countryid", type="string", length=2, nullable=false)
 */
private $countryid;

/**
 * Set countryid
 *
 * @param string $countryId
 */
public function setCountryid($countryid)
{
    $this->countryid = $countryid;
}

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

This is my first symfony project, but I've been over the docs several times and think I have everything set up ok...

edit:

I have a join set up as follows: Ducks.php

/**
 * @ORM\ManyToOne(targetEntity="Country", inversedBy="ducks")
 * @ORM\JoinColumn(name="countryid", referencedColumnName="id")
 */
private $country;

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

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

and on the Country.php side:

/**
 * @ORM\OneToMany(targetEntity="Ducks", mappedBy="country")
 */
protected $ducks;

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

/**
 * Get ducks
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getDucks()
{
    return $this->ducks;
}
  • 写回答

1条回答 默认 最新

  • duanshan7261 2012-04-12 00:56
    关注

    What's happening is that the form is sending an actual Country object to ducks. You can confirm this with:

    public function setCountryid($countryid)
    {
        if (is_object($countryid)) die('Yep, got a country object.');
        $this->countryid = $countryid;
    }
    

    It sounds like you only want to store a 2 char country code? You don't want an actual relation? If so then this might do the trick:

    public function setCountryid($countryid)
    {
        if (is_object($countryid)) $countryid = $countryid->getId();
        $this->countryid = $countryid;
    }
    

    If you want an actual normal Doctrine managed relation between duck and country then something like:

    /**
     * @ORM\ManyToOne(targetEntity="Country")
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
     */
     */
    private $country;
    

    And adjust your getter/setters accordingly.

    It's a bit strange that you seem to have both yml and annotations. From what I understood, you could use one or the other in a given bundle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写