Symfony 2表单未保存来自select的数据(在联接表上) [英] Symfony 2 Form not saving data from select (on joined table)
问题描述
我创建了一个看起来正确的表单,它具有一些文本字段和一个选择框,其中包含从我所拥有的国家列表中拉出的国家/地区列表。选择框使用正确的值值正确显示,并显示文本。但是,当我提交表单时,我得到一个例外:
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
如果我将数据库表(在PHPMyAdmin中)设置为允许countryid字段为空值
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'));
}
其中的回显返回country对象的__toString函数,这似乎有点奇怪-但这是在表单中选择的国家的完整国家信息。
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.
在Ducks.php类中:
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;
}
这是我的第一个symfony项目,但是我已经在文档时代,以为我已经一切就绪...
This is my first symfony project, but I've been over the docs several times and think I have everything set up ok...
编辑:
我已经建立了一个联接如下所示:
Ducks.php
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;
}
在Country.php上:
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;
}
推荐答案
正在发生的事情是该表单正在向鸭子发送实际的Country对象。您可以使用以下命令进行确认:
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;
}
听起来您只想存储2个字符的国家/地区代码?您不想要实际的关系吗?如果是这样,那么这可能会解决问题:
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.
您似乎同时拥有yml和注释,这有点奇怪。据我了解,您可以在给定的捆绑包中使用其中一个。
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.
这篇关于Symfony 2表单未保存来自select的数据(在联接表上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!