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.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题