0

Symfony2 のフォームにデフォルト データを設定する方法に関する多くのトピックを見てきましたが、クエリ ビルダーを使用してエンティティ ウィジェットにデフォルト データを設定する方法については何も見つかりませんでした。私は私の問題を説明します:

関係 ManyToOne を持つ、通信とステータスの 2 つのエンティティがあります。ここに私のコミュニケーションクラスがあります:

class Communication{

/**
 * @ORM\Id
 * @ORM\Column(name="Comm_CommunicationId")
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

    /**
 * @ORM\ManyToOne(targetEntity="Test\DatabaseBundle\Entity\Statut", inversedBy="communication")
 * @ORM\JoinColumn(name="Comm_Status", referencedColumnName="Capt_Code")
 */
private $statut;}

そして、ここに私のステータスクラスがあります:

class Statut{

/**
 * @ORM\Id
 * @ORM\Column(name="Capt_CaptionId")
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
protected $id;
/**
 * @ORM\Column(name="Capt_Code", type="string")
 */
private $code;

/**
 * @ORM\Column(name="Capt_FR", type="string")
 */
private $codefr;}

CommunicationType フォームを作成して、通信エンティティを変更できるようにしました。

 public function buildForm(FormBuilder $builder, array $options)
{
    $builder      -> add('caseid','text')
                  -> add('statut','entity', array('class' => 'Test\DatabaseBundle\Entity\Statut',
                                                  'query_builder' => function(\Test\DatabaseBundle\Entity\StatutRepository $sr){
                                                        $res = $sr->getCodeOnly();                                                          
                                                        return $res; },
                                                  'property' => 'CodeFr',
                                                  'preferred_choices' => array(1),
                                                  ));}

これが私のコントローラーです:

public function ModifierAction($commid){
    $comm = $this -> getDoctrine()
                  -> getEntityManager()
                  -> getRepository('Test\DatabaseBundle\Entity\Communication')
                  -> find($commid);
    $form = $this -> createForm(new CommunicationType($em), $comm);
    ....
}

私はあなたに私のエンティティのセッターとゲッターを与えます: 通信 :

/**
 * Set id
 *
 * @param integer $Id
 */
public function setId($Id)
{
    $this -> id = $Id;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
} 

ステータスエンティティ:

/**
 * Set id
 *
 * @param integer $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}       /**
 * Set code
 *
 * @param string $code
 */
public function setCode($code)
{
    $this->code = $code;
}

/**
 * Get code
 *
 * @return string
 */
public function getCode()
{
    return $this->code;
}       /**
 * Add communication
 *
 * @param Acme\StoreBundle\Entity\Communication $communication
 */
public function addCommunication(\Test\DatabaseBundle\Entity\Communication $communication)
{
    $this->communication[] = $communication;
}

/**
 * Get communication
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getCommunication()
{
    return $this->ccommunication;
}       public function __construct()
{
    $this->communication = new ArrayCollection();
}

あなたは教義が自動的にオブジェクトをチェックすると言いました。通常、ドクトリンはクエリを実行し、データベースでオブジェクトの現在の値を検索し、それをデフォルトの選択肢として渡しますか?

これでエンティティを変更できますが、現在のエンティティのステータス値をウィジェット「エンティティ」のデフォルト値として設定する方法がわかりません。メソッド「getCodeOnly」は、ステータスの属性コードの値 (Complete、Cancelled、InProgress、Deleted) をデータベースで検索し、既定値として常に Complete を渡します。たとえば、エンティティの値が Cancelled の場合、このフォームを使用してエンティティを変更するときに、Canceled をデフォルト値として設定します。

Preferred_choice はエンティティではなくパラメーターとして配列を必要とするため、getData と preferred_choice を使用してステータス値にアクセスすることはできません。

また、ステータスのさまざまな値で配列を作成してフォームに渡しましたが、データベースにいくつかの問題があり、失敗しました。

誰かがこの問題を解決するための情報を持っているなら、私はそれを見てうれしいです.

4

0 に答える 0