CategoryType フォームから「作成者」フィールドの値を設定しようとしています。FOSバンドルでログインしている現在のユーザーのユーザーIDにしたい。
私のCategoryTypeフォーム:
namespace My\CategoryBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class CategoryType extends AbstractType
{
private $userId;
public function __construct(array $userId)
{
$this->userId = $userId;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('author')
->add('content')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\CategoryBundle\Entity\Category',
'auteur' => $this->userId
));
}
/**
* @return string
*/
public function getName()
{
return 'my_categorybundle_category';
}
}
そして私のコントローラのアクション:
public function addAction()
{
$category = new Category;
$user = $this->get('security.context')->getToken()->getUser();
$userId = $user->getId();
$form = $this->get('form.factory')->create(new CategoryType(), array( 'author' => $userId));
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->flush();
return $this->redirect($this->generateUrl('mycategory_voir',
array('id' => $category->getId())));
}
}
return $this->render('MyCategoryBundle:Category:add.html.twig',
array(
'form' => $form->createView(),
));
}
アクションの実行中にこのエラーをキャッチします。
キャッチ可能な致命的なエラー: My\CategoryBundle\Form\CategoryType::__construct() に渡される引数 1 は、指定されていない配列である必要があり、/My/CategoryBundle/Controller/CategoryController.php の 55 行目で呼び出され、/My/CategoryBundle で定義されている必要があります/Form/CategoryType.php 13行目
フォームに渡しているのはすでに配列ではありませんか?