我的建设者是 private $ username;
public function __construct( $username = null) {
$this->username = $username;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('titre')
->add('description')
->add('contenu')
->add('categorie')
->add('site', 'choice', array(
'choices' => array('ALM' => 'Aujourdhui le Maroc', 'LVE' => 'Lavieéco', 'FDM' => 'Femmes du Maroc', 'Nissaa' => 'Nissa min almaghrib'),
'required' => false,
))
->add('Journaliste', 'text', array(
'label' => 'Journaliste',
'data' => $this->username))
->add('Webmaster', 'entity', array('class' => 'Work\frontBundle\Entity\Utilisateurs', 'property' => 'id', 'multiple' => false))
->add('image')
->add('Valider')
->add('Envoyer', 'submit')
;
}
和控制器$ a =新文章(); $ USERNAME = $这 - >容器 - >获得(' security.context') - >为gettoken() - >的getUser() - > getUsername(); $ form = $ this-> createForm(new \ Work \ frontBundle \ Form \ ArticleType($ username),$ a); if($ request-> getMethod()==' POST'){
$form->handleRequest($request);
if ($form->isValid()) {
$a->setsTitre($form['titre']->getData());
$a->setDescription($form['description']->getData());
$a->setContenu($form['contenu']->getData());
$a->setCategorie($form['categorie']->getData());
$a->setSite($form['site']->getData());
$a->setImage($form['image']->getData());
$a->setWebmaster($form['Webmaster']->getData());
$a->setValider($form['Valider']->getData());
$a->setJournaliste($form['Journaliste']->getData());
$em = $this->getDoctrine()->getEntityManager();
$em->persist($a);
$em->flush();
}
}
return $this->render('WorkfrontBundle:Default:article.html.twig', array('form' => $form->createView()));
}
设定者 和二传手
/**
* Set journaliste
*
* @param \Work\frontBundle\Entity\Utilisateurs $journaliste
* @return Article
*/
public function setJournaliste(\Work\frontBundle\Entity\Utilisateurs $journaliste = null)
{
$this->journaliste = $journaliste;
return $this;
}
但我遇到了这个问题 可捕获致命错误:传递给Work \ frontBundle \ Entity \ Article :: setJournaliste()的参数1必须是Work \ frontBundle \ Entity \ Utilisateurs的实例,给定字符串,在C:\ wamp \ www \ stage \ vendor \ symfony中调用第442行上的\ symfony \ src \ Symfony \ Component \ PropertyAccess \ PropertyAccessor.php,在C:\ wamp \ www \ stage \ src \ Work \ frontBundle \ Entity \ Article.php第297行中定义