正しいように見えるフォームを作成しました。いくつかのテキストフィールドと、国のテーブルから取得した国のリストを含む選択ボックスがあります。選択ボックスは、その「値」の正しい値を使用して正しく表示され、テキストが表示されます。ただし、フォームを送信すると、例外が発生します。
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'countryid' cannot be null
(PHPMyAdminで)データベーステーブルを設定して、countryidフィールドにnull値を許可すると、例外なくレコードに入力されますが、countryidのエントリはnullになります。
私のコントローラーには次のコードがあります。
$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'));
}
そこにあるエコーは、国オブジェクトの__toString関数を返しますが、これは少し奇妙に思えますが、フォームで選択された国の完全な国情報です。
Ducks.phpクラス:
/**
* @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プロジェクトですが、私は何度かドキュメントを読んでいて、すべてがうまくセットアップされていると思います...
編集:
私は次のように参加を設定しています: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側で:
/**
* @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;
}