0

正しいように見えるフォームを作成しました。いくつかのテキストフィールドと、国のテーブルから取得した国のリストを含む選択ボックスがあります。選択ボックスは、その「値」の正しい値を使用して正しく表示され、テキストが表示されます。ただし、フォームを送信すると、例外が発生します。

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;
}
4

1 に答える 1

2

何が起こっているのかというと、フォームは実際のCountryオブジェクトをアヒルに送信しているということです。これは次の方法で確認できます。

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

2文字の国コードのみを保存したいようですね。あなたは実際の関係を望んでいませんか?もしそうなら、これはトリックを行う可能性があります:

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

アヒルと国の間の実際の通常のDoctrine管理された関係が必要な場合は、次のようになります。

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

それに応じてゲッター/セッターを調整します。

ymlとアノテーションの両方があるように見えるのは少し奇妙です。私が理解したことから、特定のバンドルでどちらか一方を使用できます。

于 2012-04-12T00:56:21.780 に答える