我的建设者是私人$ 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 = new Article(); $ 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;
}

但我得到了这个问题可捕获的致命错误:参数1传递给Work \ frontBundle \ Entity \ Article :: setJournaliste()必须是Work \ frontBundle \ Entity \ Utilisateurs的实例,给定的字符串,在C:\ wamp \ www \中调用第442行上的stage \ vendor \ symfony \ symfony \ src \ Symfony \ Component \ PropertyAccess \ PropertyAccessor.php并在C:\ wamp \ www \ stage \ src \ Work \ frontBundle \ Entity \ Article.php第297行中定义