Symfony3,在控制器中获取表单值子项“annonce.title”不存在

时间:2017-10-27 09:44:37

标签: php forms symfony

我的imput annonce title的源代码

 {{ form_widget(form.annonce.title) }}

我收到此错误:

Child "annonce.title" does not exist.

我的实体: 用户实体

  class User extends BaseUser
{
/**
 * @var guid
 *
 * @ORM\Column(name="id", type="guid")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="UUID")
 *
 * @Serializer\Expose()
 */
protected $id;



/**
 * @ORM\Column(type="phone_number")
 */
private $phoneNumber;



/**
 * @ORM\Column(name="adresse", type="string", length=250)
 */
private $adresse;



    /**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 *
 * @Serializer\Expose()
 */
private $createdAt;



public function __construct()
{
    parent::__construct();
    $this->createdAt = new \DateTime();
    $this->isActive = false;
    $this->salt = md5(uniqid(null, true));
    // your own logic
}


/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return User
 */
public function setCreatedAt($createdAt) {
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime
 */
public function getCreatedAt() {
    return $this->createdAt;
}

function getPhoneNumber() {
    return $this->phoneNumber;
}

function setPhoneNumber($phoneNumber) {
    $this->phoneNumber = $phoneNumber;
}

function getId(): guid {
    return $this->id;
}

function getAdresse() {
    return $this->adresse;
}

function setId(guid $id) {
    $this->id = $id;
}

function setAdresse($adresse) {
    $this->adresse = $adresse;
}

Annonce Entity

  class Annonce {

/**
 * @var guid
 *
 * @ORM\Column(name="id", type="guid")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="UUID")
 *
 * @Serializer\Expose()
 */
private $id;

    /**
 * @ORM\OneToMany(targetEntity="userAnnonces", mappedBy="annonce", cascade={"persist"}, orphanRemoval=true)
 *
 * @var ArrayCollection|userAnnonces[]
 */
private $userAnnonces;

private $annoncesAsArray;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 *
 * @Serializer\Expose()
 */
private $createdAt;


    /**
 * @ORM\Column(type="string")
 *
 * @Assert\NotBlank(message="Veuillez ajouter une photo pour votre annonce.")
 * @Assert\File(mimeTypes={ "application/image" })
 */
private $imageFile;

        /**
 * 
 * @ORM\OneToOne(targetEntity="Ville", inversedBy="annonce")
 * @ORM\JoinColumn(name="ville_id", referencedColumnName="id")
 */
private $ville;

            /**
 * 
 * @ORM\OneToOne(targetEntity="metier", inversedBy="annonce")
 * @ORM\JoinColumn(name="metier_id", referencedColumnName="id")
 */
private $metier;

/**
 * @ORM\Column(name="titre", type="string", length=250)
 */
private $titre;

/**
 * @ORM\Column(name="description", type="string", length=800)
 */
private $description;

public function __construct() {
  //  parent::__construct();
    // your own logic
    $this->createdAt = new \DateTime();
    $this->userAnnonces = new ArrayCollection();
}

/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return User
 */
public function setCreatedAt($createdAt) {
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime
 */
public function getCreatedAt() {
    return $this->createdAt;
}



/**
 * @ORM\Column(type="datetime")
 *
 * @var \DateTime
 */
private $updatedAt;



function getId(){
    return $this->id;
}



function getTitre() {
    return $this->titre;
}

function getDescription() {
    return $this->description;
}

function getUpdatedAt(): \DateTime {
    return $this->updatedAt;
}

function setId(guid $id) {
    $this->id = $id;
}



function setTitre($titre) {
    $this->titre = $titre;
}

function setDescription($description) {
    $this->description = $description;
}

function setUpdatedAt(\DateTime $updatedAt) {
    $this->updatedAt = $updatedAt;
}


public function addAnnonce(User $user)
{
    $userAnnonces = new userAnnonces();
    $userAnnonces->setAnnonce($this);
    $userAnnonces->setUser($user);
    $this->userAnnonces->add($userAnnonces);
}

public function removeAnnonce(User $user)
{
    //TODO: implement
}

public function getAnnonces()
{
    $annonces = array();
    foreach ($this->userAnnonces as $userAnnonce) {
        $annonces[] = $userAnnonce->getPhoto();
    }

    return $annonces;
}

public function setAnnoncesAsArray(array $annonces)
{
    $this->annoncesAsArray = $annonces;
}

/**
 * @Serializer\VirtualProperty()
 * @Serializer\SerializedName("annonces")
 *
 * @return array
 */
public function getAnnoncesAsArray()
{
    if (null !== $this->annoncesAsArray) {
        return $this->annoncesAsArray;
    }

    $this->annoncesAsArray = array();
    foreach ($this->userAnnonces as $userAnnonce) {
        $this->annoncesAsArray[] = $userAnnonce->getAnnonce()->getTitre();
    }

    return $this->annoncesAsArray;
}


function getVille() {
    return $this->ville;
}

function getMetier() {
    return $this->metier;
}

function setVille($ville) {
    $this->ville = $ville;
}

function setMetier($metier) {
    $this->metier = $metier;
}


function getImageFile() {
    return $this->imageFile;
}

function setImageFile($imageFile) {
    $this->imageFile = $imageFile;
}

userAnnonces实体:

   class userAnnonces
   {
/**
 * @var guid
 *
 * @ORM\Column(name="id", type="guid")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="UUID")
 *
 * @Serializer\Expose()
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="Herfaty\AppBundle\Entity\User")
 * @ORM\JoinColumn(name="user_id", nullable=false, onDelete="CASCADE")
 *
 * @var User
 */
private $user;

/**
 * @ORM\ManyToOne(targetEntity="annonce" , inversedBy="userAnnonces")
 * @ORM\JoinColumn(name="annonce_id", nullable=false, onDelete="CASCADE")
 *
 * @var annonce
 */
private $annonce;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime")
 */
private $createdAt;

public function __construct()
{
    $this->createdAt = new \DateTime();
}


public function getId()
{
    return $this->id;
}




/**
 * Set createdAt
 *
 * @param \DateTime $createdAt
 * @return UserAnnonces
 */
public function setCreatedAt(\DateTime $createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * Get createdAt
 *
 * @return \DateTime
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

function getUser() {
    return $this->user;
}

function getAnnonce() {
    return $this->annonce;
}

function setUser(User $user) {
    $this->user = $user;
}

function setAnnonce(Annonce $annonce) {
    $this->annonce = $annonce;
}

我为每个实体创建了一个表单类型,userAnnones的表单类型:

    class userAnnoncesType extends AbstractType
    {
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('annonce', annonceType::class, array('data_class' => 'Herfaty\AppBundle\Entity\Annonce'))
            ->add('user', RegistrationFormType::class, array('data_class' => 'Herfaty\AppBundle\Entity\User'))
            ->add('submit', SubmitType::class )

    ;
}

     /**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        array(
            'data_class' => 'Herfaty\AppBundle\Entity\userAnnonces'
        )
    );

}

public function getName() {
    return "Herfaty_user_annonce";
}

当我想在我的控制器中使用一个表单同时插入用户和annonce时,它会在标题中显示错误

     public function CreateAnnonceAction() {
    $request = $this->container->get('request_stack')->getCurrentRequest();
    $local = $request->getLocale();
    $em = $this->getDoctrine()->getManager();
    $doctrine = $this->getDoctrine();


    $userannonce = new userAnnonces();

    //$form = $this->get('fos_user.registration.form.factory');

    $form = $this->createForm(userAnnoncesType::class, $userannonce);

    if ($request->getMethod() == 'POST') {


        $form->handleRequest($request);

        if ($form->get('submit')->isClicked()) {
            $user = new User();
            $annonce = new Annonce();

            $title = $form->get('annonce.title')->getData();

            $ville = $form->get('annonce.ville.nom')->getData();
            $ville = $em->getRepository('HerfatyAppBundle:Ville')->findOrCreateByName($ville);
            $adresse = $form->get('user.adresse')->getData();
            $username = $form->get('user.username')->getData();
            $email = $form->get('user.email')->getData();
            $metier = $form->get('annonce.metier.nom')->getData();
            $metier = $em->getRepository('HerfatyAppBundle:Metier')->findOrCreateByName($metier);
            $description = $form->get('annonce.description')->getData();
            $phoneNumber = $form->get('user.phoneNumber')->getData();

            $file = $form->get('annonce.imageFile')->getData();

            // Generate a unique name for the file before saving it
            $fileName = md5(uniqid()) . '.' . $file->guessExtension();

            // Move the file to the directory where brochures are stored
            $file->move(
                    $this->getParameter('annonces_directory'), $fileName
            );


            $user->setAdresse($adresse);
            $user->setEmail($email);
            $user->setUsername($username);
            $user->setPhoneNumber($phoneNumber);


            $annonce->setDescription($description);
            $annonce->setTitre($titre);
            $annonce->setVille($ville);
            $annonce->setMetier($metier);
            $annonce->setImageFile($fileName);

            $userannonce->setAnnonce($annonce);
            $userannonce->setUser($user);

            $em->persist($annonce);
            $annonce->addAnnonce($user);

            $em->flush();
        }

我的annonceType

    class annonceType extends AbstractType
   {
   public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('titre', TextType::class)
        ->add('description', TextareaType::class)
        ->add('ville',  villeType::class, array('data_class' => 'Herfaty\AppBundle\Entity\Ville'))


        ->add('metier',  metierType::class, array('data_class' => 'Herfaty\AppBundle\Entity\Metier'))
        ->add('imageFile', FileType::class )
            //->add('submit', SubmitType::class )

    ;
}

     /**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        array(
            'data_class' => 'Herfaty\AppBundle\Entity\Annonce'
        )
    );

}

public function getName() {
    return "Herfaty_annonce";
}

1 个答案:

答案 0 :(得分:0)

在annonce实体中,我没有看到max = 50 xgboost.plot_importance(dict(sorted(bst.get_fscore().items(), reverse = True, key=lambda x:x[1])[:max]), ax = ax, height = 0.8) 我看到getTitle(), 我想你应该试一试;

getTitre()

或者将{{ form_widget(form.annonce.titre) }} 更改为getTitre(),因此请更改getTitle()每个文件,类和方法。