How to clear or clear a collection of arrays in symphony doctrine

I have an array of arrays of objects like this

Class User
{
private $tasks
}

How can I clear or clear the collection after loading by the user from the database.

When I ask the user, then the doctrine will be too lazy to load tasks into the user object, but I want to clear these tasks first.

sort of

$user->getTasks().empty()

+5
source share
1 answer

First of all, I assume that your user entity constructor looks something like this:

class User
{
    public function __construct()
    {
        ...
        $this->tasks = new \Doctrine\Common\Collections\ArrayCollection();
        ...
    }
}

If this is not the case so far, stop reading and correct me in the comments :)

Note that the ArrayCollection class was created by Doctrine. Symfony and most of its components do well in documenting classes. When you look at this class, you will find:

http://www.doctrine-project.org/api/common/2.2/class-Doctrine.Common.Collections.ArrayCollection.html

(, , , )

, ArrayCollection. : clear().

, User :

class User
{
    public function clearTasks()
    {
        $this->getTasks()->clear();
    }
}

User :

$user->clearTasks();

( !)

+18

All Articles