【问题标题】:Unable to add 2 fields in my table无法在我的表中添加 2 个字段
【发布时间】:2017-10-06 13:13:18
【问题描述】:

我正在尝试在我的新表中插入数据,这是实体代码

 <?php

    namespace IT\ITBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * Famille
     * @ORM\Table(name="famille")
     * @ORM\Entity(repositoryClass="IT\ITBundle\Repository\FamilleRepository")

     */

    class Famille
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;

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

        /**
         * @ORM\ManyToOne(targetEntity="IT\ITBundle\Entity\Personne", inversedBy="familles")
         * @ORM\JoinColumn(nullable=false)

         */

        private $personne;
        /**


         * @ORM\OneToMany(targetEntity="IT\ITBundle\Entity\Famille", mappedBy="personne",cascade={"persist"})
         * @ORM\JoinColumn(name="enfant_id", referencedColumnName="id")

         */
        private $enfant;

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

        /**
         * Set lien
         *
         * @param string $lien
         * @return Famille
         */
        public function setLien($lien)
        {
            $this->lien = $lien;

            return $this;
        }

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

        /**
         * Set personne
         *
         * @param \IT\ITBundle\Entity\Personne $personne
         *
         * @return Famille
         */
        public function setPersonne(\IT\MITBundle\Entity\PersonnePhysique $personne = null)
        {
            $this->personne = $personne;

            return $this;
        }

        /**
         * Get personne
         *
         * @return \Doctrine\Common\Collections\ArrayCollection
         */
        public function getPersonne()
        {
            return $this->personne;
        }
        /**
         * Set enfant
         *
         * @param PersonnePhysique $personne
         *
         * @return Famille
         */
        public function setEnfant($enfant)
        {
            $this->enfant = $enfant;

            return $this;
        }

        /**
         * Get enfant
         *
         * @return Personne
         */
        public function getEnfant()
        {
            return $this->enfant;
        }
        public function __construct()
        {

            $this->familles = new \Doctrine\Common\Collections\ArrayCollection();

        }
    }

这是我的第二个实体

/** * @var 集合。 * @ORM\OneToMany(targetEntity="IT\ITBundle\Entity\Famille",cascade={"persist","re​​move"}, mappedBy="personne")

 */
private $familles;

我的控制器是

public function newAction(Request $request)
    {
        $controle = $this->get('Controles');
        if(($controle->is_Granted('Ajout personne Morale','Client/Societaire',$this->getUser()))==false){throw new AccessDeniedException();}
        $personne = new PersonneMorale();
        $form = $this->createForm(PersonneMoraleType::class, $personne);
        $form->handleRequest($request);

        $mails=$personne->getEmails();
        if(!empty($mails)){
            foreach($mails as $mail){
                $mail->setPersonne($personne);
            }
        }
        $adress=$personne->getAdresses();
        if(!empty($adress)){
            foreach($adress as $adre){
                $adre->setPersonne($personne);
            }
        }
        $tels=$personne->getTelecoms();
        if(!empty($tels)){
            foreach($tels as $tel){
                $tel->setPersonne($personne);
            }
        }
        $compteBancaires=$personne->getCompteBancaires();
        if(!empty($compteBancaires)){
            foreach($compteBancaires as $cmp){
                $cmp->setPersonne($personne);
            }
        }
        $contacts=$personne->getContacts();
        if(!empty($contacts)){
            foreach($contacts as $contact){
                $contact->setPersonne($personne);
            }
        }
        $familles=$personne->getfamilles();
        if(!empty($familles)){
            foreach($familles as $fam){
                $fam->setFamille($personne);
            }
        }
        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $maxNum = $em->getRepository('ITBundle:Personne')->getMaxNumPersonne();
            if($maxNum == null){
                $personne->setNumpersonne(1000000);
            }
            else{
                $personne->setNumpersonne($maxNum+1);
            }
            $em->persist($personne);
            $em->flush();
           // return $this->redirectToRoute('Personne_morale_index', array());
            return $this->render('ClientsBundle:Morale:success.html.twig', array(
                'personne' => $personne
            ));
        }
        return $this->render('ClientsBundle:Morale:new.html.twig', array(
            'post' => $personne,
            'form' => $form->createView()
        ));
    }

如您所见,我只能保存“lien”,但我想保存 personne_id 和 enfant_id enter image description here

【问题讨论】:

  • 有什么错误吗?你有什么问题?
  • 如您所见,我只能保存“lien”,但我想保存 personne_id 和 enfant_id 在此处输入图像描述
  • 请帮忙
  • 你更新你的数据库架构了吗???
  • 不,我手动完成所有代码。我应该更新数据库 shema 以及我如何做到这一点

标签: javascript php sql symfony


【解决方案1】:

如果 personne_id 列不存在于表结构中, 跑 bin/console doctrine:schema:update --dump-sql 用于查看查询和 bin/console doctrine:schema:update --force 运行它们。

您还需要持久化 Famille,因为 Personne_id 的数据归 Famille 所有,或者也将级联持久化添加到 personne。

由于 Famille 与 Enfant 的关系是 oneToMany,因此一旦持久化 Enfant,famille_id 将存储在 Enfant 表中。

【讨论】:

  • 如果 enfant 不是一个持久对象,那么保存与 Famille 相关的 enfant_id 是没有意义的,如果那是错误的,请纠正我。请更新如果通过持久化 Famille 对象,您能够保存 Personne_id
  • 为你的答案想 asha 我只想解释一下 enfant 不是一张桌子。我在 famille 桌子上有 enfant_id 。personne 有很多 famille 并且 famille 有很多人,但我不会使用 ManyToMany 。我需要保存'Lien' 和 enfant_id 将是一个人。我应该将'enfant_id'作为自动增量吗?等待你的答案或建议。再想想
  • 能否请您在问题中也添加班级人员代码。
  • 如果你能看到我就这么做了
【解决方案2】:

类人

 /**
     * @var Collection.
     * @ORM\OneToMany(targetEntity="IT\ITBundle\Entity\Famille",cascade={"persist","remove"}, mappedBy="personne")

     */
    private $familles;
    /**
     * Add famille
     *
     * @param \IT\ITBundle\Entity\Famille $famille
     *
     * @return Personne
     */
    public function addFamille(\IT\ITBundle\Entity\Famille $famille)
    {
        $this->familles[] = $famille;

        return $this;
    }

    /**
     * Remove famille
     *
     * @param \IT\ITBundle\Entity\Famille $famille
     */
    public function removeFamille(\IT\ITBundle\Entity\Famille $famille)
    {
        $this->familles->removeElement($famille);
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    相关资源
    最近更新 更多