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 7905543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:31:16+00:00 2026-06-03T10:31:16+00:00

Using Doctrine 1.2, I am having some issues deleting items from a Doctrine_Collection. I

  • 0

Using Doctrine 1.2, I am having some issues deleting items from a Doctrine_Collection.

I have a Doctrine collection filled with transient Doctrine_Records. The $record->delete() and $record->unlink() functions don’t seem to work properly here, as they use the record’s IDs. (Which transient records don’t have, since they don’t appear in the database yet.)

Example (in which Company has many Employee)

/* Get a Company that has no Employees yet. */
$company = Doctrine::getTable('Company')->find(1);

/* Add some Employees */
$names = array('Arthur','Bob','Charlie');

foreach ($names as $name) 
{
    $employee = new Employee;
    $employee->name = "Arthur"
    $company->Employee->add($employee);
}

Now, before saving anything to the database, I have one employee in $emp, which I want to remove from the collection.

$emp->delete() /* Does not work when $emp is transient. */

What does work is this, but I really doubt that is the way to go.

foreach($company->Employee as $key => $value)
    if ($emp == $value) 
    {
        $company->Employee->remove($key);
        break;
    }

This doesn’t feel like the easiest way to do this. Is there a more recommended way of doing this?

  • 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-03T10:31:24+00:00Added an answer on June 3, 2026 at 10:31 am

    I ended up creating a subclass for Doctrine_Collection, and implemented this function, which gave me exactly the behaviour I wanted.

       /**
        * Looks up a Doctrine_Record in the collection, and - when found - removes it. It is also compatible with serialised records, as 
        * it tries strict matching (using ===) as well as loose matching (using ==). A strict match precedes a loose match.
        */
       public function removeItem($item)
       {
          if (!($item instanceof Doctrine_Record)) 
          {
             throw new Exception("Error while calling removeItem, trying to remove an item that does not descend from Doctrine_Record");
             return;
          }
    
          foreach ($this as $key=>$value)
          {
             if ($item === $value)
             {
                /* Strict match found, remove it and return. */
                $this->remove($key);
                return;
             }
             if ($looseMatch === null && $item == $value) $looseMatch = $key;
          }
    
          /* No strict match found. If a $matchLoose is on, and aloose match was found, remove it. */
          if ($looseMatch !== null) 
          {
             $this->remove($looseMatch);
          } 
          else throw new Exception('No match found trying to remove an item from a collection.');
    
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some models witch are using Doctrine nestedset feature. I want to add
I'm having a small problem with embedForm using doctrine and SF1.4 I have created
I am using Doctrine 2 entities. We have some entities which have to update
I'm having some trouble pulling up relationed objects from my database using Doctrine2 in
i'm having a little issue with doctrine using symfony 1.4 (I think it's using
I am currently using Doctrine's DBAL library for MySQL interfacing and have currently built
We are using Doctrine 2 for a new project and have run into an
I'm using Doctrine ODM with MongoDB. I have a product model like this: namespace
Using Symfony 1.4.5 with Doctrine I have a model which includes an uploaded image
I am having troubles generating a simple database form model. I am using: Doctrine

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.