私は単純なクラスを持っています:
class Type
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=15)
*/
private $name;
...
}
データベースにいくつかの「タイプ」オブジェクトがあります。したがって、そのうちの 1 つを変更したい場合は、新しいコントローラー ルール (/types/edit/{id} など) と新しいアクションを作成します。
public function typesEditViewAction($id)
{
...
$editedType = new Type();
$form = $this->createFormBuilder($editedType)
->add('name', 'text')
->add('id', 'hidden', array('data' => $id))
->getForm();
// send form to twig template
...
}
その後、別のコントローラー ルール (/types/do_edit など) とアクションを作成します。
public function typesEditAction(Request $request)
{
...
$editedType = new Type();
$form = $this->createFormBuilder($editedType)
->add('name', 'text')
->add('id', 'hidden')
->getForm();
$form->bind($request); // <--- ERROR THERE !!!
// change 'type' object in db
...
}
そして、そこで小さな問題を見つけました。クラス 'Type' には自動生成されたセッター setId() がなく、バインド時にエラーが発生しました。
Neither the property "id" nor one of the methods "setId()", "__set()" or "__call()" exist and have public access in class "Lan\CsmBundle\Entity\Type".
ここで、symfony2 フォーム オブジェクト ($form) から 'id' フィールドを削除し、手動でテンプレートに送信します。2 番目のコントローラーのアクションでは、$form オブジェクトと「id」フィールドが離れています。それを行うための「適切な」方法がわかりません(「タイプ」クラスの更新)。助けてください。