logo
down
shadow

How to remove a collection element without EntityManager#remove(...) in Doctrine 2?


How to remove a collection element without EntityManager#remove(...) in Doctrine 2?

Content Index :

How to remove a collection element without EntityManager#remove(...) in Doctrine 2?
Tag : development , By : Goeran
Date : November 26 2020, 03:01 PM

Hope this helps The solution is to remove the reference to EntityA from the EntityB (first). In this case Doctrine will try to persist an EntityB without. But if we combine this with orphanRemoval=true, we'll get the aimed result:
class EntityA
{
    ...
    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="EntityB", mappedBy="entityA", cascade={"persist"}, orphanRemoval=true)
     */
    protected $entityBs;
    ...
    public function removeEntityB(EntityB $entityB)
    {
        $this->entityBs->removeElement($entityB);
        $entityB->setEntityA(null);
        return $this;
    }
    ...
}

class EntityB
{
    ...
    /**
     * @var EntityA
     *
     * @ORM\ManyToOne(targetEntity="EntityA", inversedBy="entityBs")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="entity_a_id", referencedColumnName="id")
     * })
     */
    protected $entityA;
    ...
    /**
     * @param EntityA $entityA
     * @return EntityB
     */
    public function setEntityA(EntityA $entityA = null)
    {
        $this->entityA = $entityA;
        return $this;
    }
    ...
}
public function setEntityBs($entityBs)
{
    $this->removeEntityBs();
    $this->entityBs = new ArrayCollection([]);
    /** @var EntityB $entityB */
    foreach ($entityBs as $entityB) {
        $this->addEntityB($entityB);
    }
    return $this;
}
public function removeEntityBs()
{
    foreach ($this->getEntityBs() as $entityB) {
        $this->removeEntityB($entityB);
    }
    return $this;
}
public function setEntityBs($entityBs)
{
    $this->removeEntityBsNotInList($entityBs);
    $this->entityBs = new ArrayCollection([]);
    /** @var EntityB $entityB */
    foreach ($entityBs as $entityB) {
        $this->addEntityB($entityB);
    }
    return $this;
}
private function removeEntityBsNotInList($entityBs)
{
    foreach ($this->getEntityBs() as $entityB) {
        if ($entityBs->indexOf($entityB) === false) {
            $this->removeEntityB($entityB);
        }
    }
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Does Hibernate remove referenced entities when calling entityManager.remove()?


Tag : hibernate , By : vferman
Date : March 29 2020, 07:55 AM
help you fix your problem Cascading the removal will make Hibernate call entityManager.remove() on the referenced entity (or collection of entities). The referenced entity will thus be removed from the database. But it leaves the entity instances untouched.
Note that having a cascade=REMOVE (or ALL) on a ManyToOne is usually a very bad idea. You generally don't want to remove a user when removing just one of its memberships.

EntityManager.remove() does not generate delete query and does not remove entity


Tag : java , By : jrf
Date : March 29 2020, 07:55 AM
To fix this issue Try to remove the book object without merging it(use em.find), it works for me.
public void deleteBook(Book book) {
        // Book mergedBook = em.merge(book);
        Book b1=em.find(Book.class,book.getId);
        em.remove(b1);
        logger.info("Book with id: " + mergedBook.getId()  + " deleted successfully");
    }

Symfony form collection and doctrine remove all children from a collection (unwanted)


Tag : symfony , By : user142345
Date : March 29 2020, 07:55 AM
I hope this helps . I figured out what caused the problem. I had form fields in the order detail that were disabled. These disabled fields were not sent to the server by my browser. This causes doctrine to remove those children. There has been some discussion on this issue here.
In order to still have field that cannot be changed, but are still visible I removed the disabled attribute and added the attribute readonly => true to those form fields and my problem is resolved.

Java 8 Map of Collections remove element from collection and remove entry if empty


Tag : java , By : Manik
Date : March 29 2020, 07:55 AM
may help you .
Is there a way to do this in a short way using one of the numerous new Map methods of Java 8?

Doctrine, remove collection?


Tag : php , By : harley.holt
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How to remove perforce (p4) on Ubuntu
  • How do they know mean and std, the input value of transforms.Normalize
  • Why this type is not an Interface?
  • SugarCRM Rest API set_relationship between Contacts and Documents
  • Jira dashboard organization
  • Web worker importScripts fails to place script variables in global scope
  • Always errors - The "path" argument must be one of type string, Buffer, or URL. Received type undefined
  • How to create an observable of a stream of infinite items
  • Not efficiently to use multi-Core CPU for training Doc2vec with gensim
  • webGL gl_Position value saving outside shaders
  • Is it okay for a resolver to have side effects besides resolving the type?
  • Move 32bit register into a 8 bit register
  • Is there a way to update, not overwrite, worker_env for a Dask YarnCluster within a script?
  • Lotus Notes Deployment
  • How Do I Add Active Directory To APIM Using Terraform?
  • How to get the old parameter values in Blazor OnParameterSet?
  • How to debug "ERROR: Could not reach the worker node."?
  • How chain indefinite amount of flatMap operators in Reactor?
  • extract dates and times from string in Redshift
  • How do I make a column of 3 cards match in height in bootstrapVue?
  • how to replace missing values from another column in PySpark?
  • only read last line of text file (C++ Builder)
  • Snakemake --forceall --dag results in mysterius Error: <stdin>: syntax error in line 1 near 'File' from Graphvis
  • How Can I Remove Demo Products From APIM Created With Terraform?
  • How to avoid cloning a big integer in rust
  • Break a row of words into word groups in Hive
  • How can I add a path variable to existing files in an Installshield project converted from MSI
  • Certain languages are not available in postman; is there a way to enable it?
  • Concatenation step of U-Net for unequal number of channels
  • HL Fabric - states, transactions but varied keys
  • How to handle "flood wait" errors when using telethon.sync?
  • Any way to make closure which takes a destructured array?
  • What is the Difference between @PeculiarVentures 's `webcrypto` and `node-webcrypto-ossl`?
  • DWG Sheet Combination failing on AutoDesk Forge
  • karate.log(args) on afterScenario hook is not embedded on surefire json file
  • How do I output latest distinct values of specific fields and all other colums?
  • Clarification on lit-element components and where to browse them
  • Will websockets over HTTP2 also be multiplexed in streams?
  • How to apply switch statement for multi columns in datatables
  • frobot framework - Usage outside testing
  • How do I build against the UCRT with mingw-w64?
  • How to use someClass.android.ts and someClass.ios.ts without errors
  • ADB Connection to Samsung smart tv
  • is there a way to 2 create multiple command files in cypress
  • Best way to filter DBpedia results and return a specific results using SPARQL
  • Is it possible to use unicode combining characters to combine arbitrary characters?
  • Antlr4 extremely simple grammar failing
  • Neighbor of 10 wrong answer?
  • PDFlib - setting stroke and fill opacity (transparency)
  • AWS Lambda + Serverless, where/how to deploy js module that couldn't be bundled?
  • how to place mobile call from PWA
  • How to get connected clients and client certificate in node-opcua server
  • Passing dictionary from one template to another in Helm
  • Kivy. Position of GridLayout inside ScrollView
  • How can I try to place a pending order every X minutes till it's successfull?
  • Is there a way to download the SonarLint report generated in Eclipse IDE?
  • How to Open Port in Windows Firewall using C++ Builder?
  • How to put "OR" operator in Karate API assertion statement
  • Get .model.json as String
  • Proof Process busy on combine_split
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com