簡體   English   中英

Model 與多對多關系未與 symfony 中的 Doctrine 保持更新時

[英]Model with ManyToMany relationship not persisted with Doctrine in symfony when updated

我嘗試更新一個 model,但是當從 entityManager 調用 flush() 時,最后一個不會持久化。

代碼如下

 /**
     * @ORM\ManyToMany(targetEntity=GroupBase::class, inversedBy="baseUsers", cascade={"persist"})
     */
    private $GroupBase;
// this is the variable used in the entity GroupBase

/**
 * @ORM\ManyToMany(targetEntity=BaseUser::class, mappedBy="GroupBase", cascade={"persist"})
 */
    private $baseUsers;
// this is the Entity GroupBase, which should contain a collection of User 

public function addBaseUser(BaseUser $baseUser): self
    {
        if (!$this->baseUsers->contains($baseUser)) {
            $this->baseUsers[] = $baseUser;
            $baseUser->addGroupBase($this);
     
        }

        return $this;
    }
// this methods is the one that insert Users in the GroupBase Collection 

 public function insertUsersGroup(LdapUser $ldapUser, GroupBase $groupBase){
        /**
         * this methods aim to insert all Users that belong to group Members into GroupBase  
         */
        
        $dn = $ldapUser->getDistinguishedName();
        $members = $groupBase->getMembers();
        
        if(in_array($dn, $members)){
            $groupBase->addBaseUser($ldapUser);
            $ldapUser->addGroupBase($groupBase);
        }
        return $groupBase;
   }

這是在組中插入用戶的 controller

/**
     * @Route("group/insertUsersGroup")
     */
    public function insertUsersInGroup(){
        $entityManager = $this->getDoctrine()->getManager();
        $ldapUsers = $entityManager->getRepository(LdapUser::class)->findAll();
        $groupBase = $entityManager->getRepository(GroupBase::class)->findAll();
        foreach($groupBase as $group){
            foreach($ldapUsers as $user){
                $this->service->insertUsersGroup($user, $group);
                try{
                    $entityManager->persist($group);
                    $entityManager->flush();
                    $response = "model saved";
                }
                catch(Exception $e){
                    throw $e;
                }
            }
        }
        return new Response(
            $response
        );

    } 

如果我在持久化數據之前調用方法 $groupBase->getBaseUsers() 它會告訴我我的用戶在這里,並且不會拋出任何錯誤。 但似乎數據在$entitymanager->flush()之后沒有持久化

查看您提供的要點 -https://gist.github.com/lobamo/209d67b71fad18dc61e20d7a411e2780

我看到 Entity/User.php 具有以下內容:

abstract class BaseUser
{

這是故意的嗎? 我認為實體不應該是抽象的 class。

根據 PHP 的定義,在https://www.php.net/manual/en/language.oop5.abstract.php

定義為抽象的類不能被實例化,任何包含至少一個抽象方法的 class 也必須是抽象的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM