Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:06:09+00:00 2026-06-09T15:06:09+00:00

I have the Array collection of objects like this Class User { private $tasks;

  • 0

I have the Array collection of objects like this

Class User
{
    private $tasks;
}

How can i empty or clear the collection once user gets loaded from database.

When i query for user then Doctrine will lazy load the tasks in user object but i want to first clear those tasks

something like

$user->getTasks().empty()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:06:11+00:00Added an answer on June 9, 2026 at 3:06 pm

    First of all, I imagine your User entity’s constructor looks something like this:

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

    If that’s not correct so far, then stop reading, and correct me in the comments 🙂

    Note that the ArrayCollection class was created by Doctrine. Symfony and most of its components are pretty good about documenting the classes. When you look up that class, you’ll find:

    https://www.doctrine-project.org/api/collections/latest/Doctrine/Common/Collections/ArrayCollection.html

    (of course, make sure you’re on the same version; otherwise, try to find the documentation for your version)

    The documentation lists all the methods available to the ArrayCollection object. Among them: clear().

    That said, adding a new method to the User class should work:

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

    Then, on the User object, just call:

    $user->clearTasks();
    

    (and don’t forget to persist to the database!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayCollection of objects. I'm passing this array to a horizontallist as
I have a Collection<T> property that wraps a array like T[] array; public Collection<T>
I have a set of classes something like this: abstract class CollectionAbs implements Iterator
I have a class like this: class someClass { public static function getBy($method,$value) {
I have some code like this: public class EffectValues : IEnumerable<object> { public object
I have two Doctrine entities that have a one-to-many relationship, like this: License class
I would like to have an array model objects to be serialized to a
I want to have a collection of objects, which will be of a class
I have a class like this: class Foo { String bar } I am
I have a collection (or list or array list) in which I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.