简体   繁体   中英

embedded to form symfony2

i work with symfony2 and when i embedded two form (person in group) have under problem:

Neither the property "PersonEntity" nor one of the methods "getPersonEntity()", "isPersonEntity()", "hasPersonEntity()", "_ get()" or " _call()" exist and have public access in class "S118\\ebrahimiBundle\\Entity\\GroupEntity".

my PersonEntity:

<?php
namespace S118\ebrahimiBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;

/** 
 * @ORM\Entity
 */
class PersonEntity
{
    /** 
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** 
     * @ORM\Column(type="string", length=100, nullable=false)
     */
    private $f_name;

    /** 
     * @ORM\Column(type="string", length=100, nullable=false)
     */
    private $l_name;

    /** 
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $tell;

    /** 
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $fileName;

    /** 
     * @ORM\OneToMany(targetEntity="S118\ebrahimiBundle\Entity\BuyEntity", mappedBy="PersonEntity")
     */
    private $BuyEntities;

    /** 
     * @ORM\OneToMany(targetEntity="S118\ebrahimiBundle\Entity\UsageEntity", mappedBy="PersonEntity")
     */
    private $UsageEntities;

    /** 
     * @ORM\ManyToOne(targetEntity="S118\ebrahimiBundle\Entity\GroupEntity", inversedBy="PersonEntities")
     * @ORM\JoinColumn(name="Gid", referencedColumnName="id", nullable=false)
     */
    private $GroupEntity;
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->BuyEntities = new \Doctrine\Common\Collections\ArrayCollection();
        $this->UsageEntities = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set f_name
     *
     * @param string $fName
     * @return PersonEntity
     */
    public function setFName($fName)
    {
        $this->f_name = $fName;

        return $this;
    }

    /**
     * Get f_name
     *
     * @return string 
     */
    public function getFName()
    {
        return $this->f_name;
    }

    /**
     * Set l_name
     *
     * @param string $lName
     * @return PersonEntity
     */
    public function setLName($lName)
    {
        $this->l_name = $lName;

        return $this;
    }

    /**
     * Get l_name
     *
     * @return string 
     */
    public function getLName()
    {
        return $this->l_name;
    }

    /**
     * Set tell
     *
     * @param string $tell
     * @return PersonEntity
     */
    public function setTell($tell)
    {
        $this->tell = $tell;

        return $this;
    }

    /**
     * Get tell
     *
     * @return string 
     */
    public function getTell()
    {
        return $this->tell;
    }

    /**
     * Set fileName
     *
     * @param string $fileName
     * @return PersonEntity
     */
    public function setFileName($fileName)
    {
        $this->fileName = $fileName;

        return $this;
    }

    /**
     * Get fileName
     *
     * @return string 
     */
    public function getFileName()
    {
        return $this->fileName;
    }

    /**
     * Add BuyEntities
     *
     * @param \S118\ebrahimiBundle\Entity\BuyEntity $buyEntities
     * @return PersonEntity
     */
    public function addBuyEntitie(\S118\ebrahimiBundle\Entity\BuyEntity $buyEntities)
    {
        $this->BuyEntities[] = $buyEntities;

        return $this;
    }

    /**
     * Remove BuyEntities
     *
     * @param \S118\ebrahimiBundle\Entity\BuyEntity $buyEntities
     */
    public function removeBuyEntitie(\S118\ebrahimiBundle\Entity\BuyEntity $buyEntities)
    {
        $this->BuyEntities->removeElement($buyEntities);
    }

    /**
     * Get BuyEntities
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getBuyEntities()
    {
        return $this->BuyEntities;
    }

    /**
     * Add UsageEntities
     *
     * @param \S118\ebrahimiBundle\Entity\UsageEntity $usageEntities
     * @return PersonEntity
     */
    public function addUsageEntitie(\S118\ebrahimiBundle\Entity\UsageEntity $usageEntities)
    {
        $this->UsageEntities[] = $usageEntities;

        return $this;
    }

    /**
     * Remove UsageEntities
     *
     * @param \S118\ebrahimiBundle\Entity\UsageEntity $usageEntities
     */
    public function removeUsageEntitie(\S118\ebrahimiBundle\Entity\UsageEntity $usageEntities)
    {
        $this->UsageEntities->removeElement($usageEntities);
    }

    /**
     * Get UsageEntities
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getUsageEntities()
    {
        return $this->UsageEntities;
    }

    /**
     * Set GroupEntity
     *
     * @param \S118\ebrahimiBundle\Entity\GroupEntity $groupEntity
     * @return PersonEntity
     */
    public function setGroupEntity(\S118\ebrahimiBundle\Entity\GroupEntity $groupEntity)
    {
        $this->GroupEntity = $groupEntity;

        return $this;
    }

    /**
     * Get GroupEntity
     *
     * @return \S118\ebrahimiBundle\Entity\GroupEntity 
     */
    public function getGroupEntity()
    {
        return $this->GroupEntity;
    }
    public function  __toString()
    {
        return $this->f_name.' '.$this->l_name;

    }
}

and my group entity:

<?php
namespace S118\ebrahimiBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;

/** 
 * @ORM\Entity
 */
class GroupEntity
{
    /** 
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** 
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $name;

    /** 
     * @ORM\OneToMany(targetEntity="S118\ebrahimiBundle\Entity\BuyEntity", mappedBy="GroupEntity")
     */
    private $BuyEntities;

    /** 
     * @ORM\OneToMany(targetEntity="S118\ebrahimiBundle\Entity\PersonEntity", mappedBy="GroupEntity")
     */
    private $PersonEntities;
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->BuyEntities = new \Doctrine\Common\Collections\ArrayCollection();
        $this->PersonEntities = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return GroupEntity
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Add BuyEntities
     *
     * @param \S118\ebrahimiBundle\Entity\BuyEntity $buyEntities
     * @return GroupEntity
     */
    public function addBuyEntitie(\S118\ebrahimiBundle\Entity\BuyEntity $buyEntities)
    {
        $this->BuyEntities[] = $buyEntities;

        return $this;
    }

    /**
     * Remove BuyEntities
     *
     * @param \S118\ebrahimiBundle\Entity\BuyEntity $buyEntities
     */
    public function removeBuyEntitie(\S118\ebrahimiBundle\Entity\BuyEntity $buyEntities)
    {
        $this->BuyEntities->removeElement($buyEntities);
    }

    /**
     * Get BuyEntities
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getBuyEntities()
    {
        return $this->BuyEntities;
    }

    /**
     * Add PersonEntities
     *
     * @param \S118\ebrahimiBundle\Entity\PersonEntity $personEntities
     * @return GroupEntity
     */
    public function addPersonEntitie(\S118\ebrahimiBundle\Entity\PersonEntity $personEntities)
    {
        $this->PersonEntities[] = $personEntities;

        return $this;
    }

    /**
     * Remove PersonEntities
     *
     * @param \S118\ebrahimiBundle\Entity\PersonEntity $personEntities
     */
    public function removePersonEntitie(\S118\ebrahimiBundle\Entity\PersonEntity $personEntities)
    {
        $this->PersonEntities->removeElement($personEntities);
    }

    /**
     * Get PersonEntities
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getPersonEntities()
    {
        return $this->PersonEntities;
    }
    public function  __toString()
    {
        return $this->name;

    }
}

and my group view :

{% extends '::base.html.twig' %}

{% block body -%}
    <h1>GroupEntity creation</h1>

    {{ form(form) }}

        <ul class="record_actions">
    <li>
        <a href="{{ path('group') }}">
            Back to the list
        </a>
    </li>
</ul>
{% endblock %}

and my person form:

<?php

namespace S118\ebrahimiBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class PersonEntityType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('f_name')
            ->add('l_name')
            ->add('tell')
            ->add('fileName')
            ->add('GroupEntity')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'S118\ebrahimiBundle\Entity\PersonEntity'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 's118_ebrahimibundle_personentity';
    }
}

and my group form:

<?php

namespace S118\ebrahimiBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class GroupEntityType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('PersonEntity','collection', array('type' => new PersonEntityType()))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'S118\ebrahimiBundle\Entity\GroupEntity'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 's118_ebrahimibundle_groupentity';
    }
}

why???

You GroupEntityType form type is looking for a 'PersonEntity' field in your GroupEntity entity. You have a oneToMany PersonEntities instead.

Try to replace PersonEntity to PersonEntities in your GroupEntityType.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM