dongying6659 2019-03-25 14:40
浏览 144
已采纳

Symfony - 如何从实体中删除数组中的特定元素

I have a Packages entity (manyToMany with the entityTypeUser that creates me in the DB the package_des_users entity). My Packages entity therefore contains an array of TypeUser (because a package can be assigned to several types of users).

I want to be able to delete one of the elements that are part of the TypeUser array of one of my packages.

My plan of action was:

  1. Retrieve the package to process
  2. Retrieve the name of the TypeUser to remove from the TypeUser array of the package.
  3. Do a check: If the package contains only one typeUser, the package is deleted directly. If it contains more than one, just delete the chosen TypeUser.

For now, I was able to recover the package to be processed as well as the name of the TypeUser to delete, and the number of TypeUser that contained my package. If it contains only one, it suppresses it well. The problem remains for the other case.

Being a beginner in Symfony 3.4, I have a hard time understanding how to do it right.

My controller.php function :

    /**
     * Deletes a paquet entity.
     *
     * @Route("/{id}/{type}/delete", name="paquets_delete")
     */
    public function deleteAction(Request $request, $id, $type)
    {
        $em = $this->getDoctrine()->getManager();
        $unPaquet = $em->getRepository('PagesBundle:Paquet')->find($id);

        $nbTypes = count($unPaquet->getTypeUser());

        if($nbTypes == 1)
        {
            $em->remove($unPaquet);
        }

        else
        {
            $am = $this->getDoctrine()->getManager();
            $leType = $am->getRepository('PagesBundle:TypeUser') 
->findByTypeUtilisateur($type);
            $em->$unPaquet->deleteTypeFromTypesUser($leType);
        }

        $em->flush();

        return $this->redirectToRoute('paquets_index');

My entity (paquet) function :


    /**  
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\ManyToMany(targetEntity="TypeUser")  
     * @ORM\JoinTable(name="Packages_des_TypesUser") 
     * @ORM\JoinColumn(nullable=false)
     */  
    private $typeUser;

    public function __construct()
    {
        $this->typeUser = new ArrayCollection();
    }

    /** 
     * Get TypeUser 
     * 
     * @return Site\PagesBundle\Entity\TypeUser 
     */ 
    public function getTypeUser() 
    { 
        return $this->typeUser; 
    }

    public function deleteTypeFromTypesUser(TypeUser $type)
    {
        $this->typeUser->removeElement($type);
    }

I would therefore like to be able to retrieve the corresponding TypeUser object (the result being unique) from the controller so that I can pass it as a parameter to a function that will take care of removing it from the TypeUser array of my package. Then that translates into the database.

Thanks for your help !

  • 写回答

2条回答 默认 最新

  • doumingo04696 2019-03-25 14:56
    关注

    You have to change it on the class, you are calling the $unPaquet property of the entity manager, and then calling to a function that does not exist. Also you forgot to check if there is more than one or you would be deleting from an empty array.

        /**
         * Deletes a paquet entity.
         *
         * @Route("/{id}/{type}/delete", name="paquets_delete")
         */
        public function deleteAction(Request $request, $id, $type)
        {
            $em = $this->getDoctrine()->getManager();
            $unPaquet = $em->getRepository('PagesBundle:Paquet')->find($id);
    
            $nbTypes = count($unPaquet->getTypeUser());
    
            if($nbTypes == 1)
            {
                $em->remove($unPaquet);
            }
    
            else if ( $nbTypes > 1 )
            {
                $leType = $em->getRepository('PagesBundle:TypeUser') 
    ->findByTypeUtilisateur($type);
                $unPaquet->deleteTypeFromTypesUser($leType);
            }
    
            $em->flush();
    
            return $this->redirectToRoute('paquets_index');
        }
    

    The "Catchable Fatal Error: Argument 1 passed to Site\PagesBundle\Entity\Paquet::deleteTypeFromTypesUser() must be an instance of Site\PagesBundle\Entity\TypeUser, array given " error is due to the find returning an array. You have to change that method. If you need help over there show the code please.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • duai4379 2019-03-25 15:10
    关注

    I've updated my code :

        /**
     * Deletes a paquet entity.
     *
     * @Route("/{id}/{type}/delete", name="paquets_delete")
     */
    public function deleteAction(Request $request, $id, $type)
    {
        $em = $this->getDoctrine()->getManager();
        $unPaquet = $em->getRepository('PagesBundle:Paquet')->find($id);
    
        $nbTypes = count($unPaquet->getTypeUser());
    
        if($nbTypes == 1)
        {
            $em->remove($unPaquet);
        }
    
        else if($nbTypes > 1)
        {
            $am = $this->getDoctrine()->getManager();
            $leType = $am->getRepository('PagesBundle:TypeUser')- 
         >findByTypeUtilisateur($type);
            $unPaquet->deleteTypeFromTypesUser($leType[0]);
        }
    
        $em->flush();
    

    It's okay now ! Thanks

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组件库引入并使用在若依框架未展示
  • ¥149 关于#使用python 的Flash Echarts+ajax+mysql动态数据实现饼图#的问题,请各位专家解答!
  • ¥15 RichTextBox中追加文本时报错
  • ¥15 关于c语言的学习问题
  • ¥15 activity升级到flowable工作流act_ge_bytearray的草稿json数据复制到act_de_model 的model_editor_json的脚本
  • ¥15 cvi使用CreateThread创建线程时,出现存储空间不足无法处理此命令的错误
  • ¥15 求苹果推信imessage批量推信技术
  • ¥15 ubuntu 22.04 系统盘空间不足。隐藏的docker空间占用?(相关搜索:移动硬盘|管理系统)
  • ¥15 c++ word自动化,为什么可用接口是空的?
  • ¥15 Matlab计算100000*100000的矩阵运算问题: