나는 심포니 2 시작 했어 그리고 난 데이터베이스에서 데이터로 "선택"유형을 표시 싶지만 문제가 :선택 양식이
addAction :
public function addAction()
{
$categories = new CategoriesAnnonce();
$form = $this->get('form.factory')->create(new AddFormType($categories),$categories);
return $this->render('AnnoncesBundle::add.html.twig', array(
'form' => $form->createView(),
));
}
AddFormType. PHP :
<?php
namespace AnnoncesBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;
class AddFormType extends AbstractType
{
private $cat;
public function __construct(CategoriesAnnonce $categories)
{
$this->cat = $categories->getNomSousCategorie();
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titre', 'text')
->add('categories', 'choice', array(
'choices' => $this->cat,
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AnnoncesBundle\Form\Model\Add',
));
}
public function getName()
{
return 'Add';
}
}
?>
오류 :
Catchable Fatal Error: Argument 1 passed to AnnoncesBundle\Form\Type\AddFormType::__construct() must be an instance of AnnoncesBundle\Form\Type\CategoriesAnnonce, instance of AnnoncesBundle\Entity\CategoriesAnnonce given, called in /Users/jordan/Desktop/www/Lesbonnesaffaires/src/AnnoncesBundle/Controller/DefaultController.php on line 69 and defined
고맙지 만 지금 다른 문제가 있습니다. 양식의보기 데이터는 AnnoncesBundle \ Form \ Model \ Add 클래스의 인스턴스가 될 것으로 예상되지만 AnnoncesBundle \ Entity \ CategoriesAnnonce 클래스의 인스턴스입니다. "data_class"옵션을 null로 설정하거나 AnnoncesBundle \ Entity \ CategoriesAnnonce 클래스의 인스턴스를 AnnoncesBundle \ Form \ Model \ Add의 인스턴스로 변환하는 뷰 변환기를 추가하여이 오류를 피할 수 있습니다. –
@JordanLechevin 내 대답이 약간 업데이트되었지만 실제로 도움이 될만한지 확실하지 않습니다. 귀하의 질문은 당신이 얻고있는 특정 오류에 대해 더 적고 sf2에서 양식 구성 요소가 어떻게 작동하고 Doctrine과 상호 작용하는지에 대해 혼란 스러울 것입니다. – prodigitalson