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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:40:00+00:00 2026-06-13T20:40:00+00:00

I have a Doctrine entity with array type field: /** * @ORM\Table() */ class

  • 0

I have a Doctrine entity with array type field:

/**
 * @ORM\Table()
 */
class MyEntity
{
    (...)

    /**
     * @var array $items
     * 
     * @ORM\Column( type="array" ) 
     */
    private $items;

    /**
     * @param SomeItem $item 
     */
    public function addItem(SomeItem $item)
    {
        $this->items[] = $item;
    }

    (...)
}

If I add element to the array, this code works properly:

$myEntityObject->addItems(new SomeItem()); 
$EntityManager->persist($myEntityObject);
$EntityManager->flush();

$myEntityObject is saved to the database with correct data (array is serialized, and deserialized when querying database).

Unfortunately, when I change one of the object inside array without changing size of that array, Doctrine does nothing if I’m trying to save changes to the database.

$items = $myEntityObject->getItems();
$items[0]->setSomething(123);
$myEntityObject->setItems($items);
$EntityManager->persist($myEntityObject);
$EntityManager->flush();
print_r($myEntityObject);

Although, print_r in the last line of that code displays changed object’s data, Doctrine doesn’t know that something was changed inside the array if array size didn’t changed. Is there any way to force Doctrine to save changes made in that field (or gently inform it about changes in that field that needs to be saved) ?


Just found in documentation a way to solve my issue:

http://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.html

It requires a lot of changes in the code, but it works. Does someone know how to preserve default tracking policy for other fields and use NotifyPropertyChanged just for the field that stores array?

  • 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-13T20:40:01+00:00Added an answer on June 13, 2026 at 8:40 pm

    Doctrine uses identical operator (===) to compare changes between old and new values. The operator used on the same object (or array of objects) with different data always return true. There is the other way to solve this issue, you can clone an object that needs to be changed.

    $items = $myEntityObject->getItems();
    $items[0] = clone $items[0];
    $items[0]->setSomething(123);
    $myEntityObject->setItems($items);
    
    // ...
    

    Or change the setItems() method (We need to clone only one object to persist the whole array)

    public function setItems(array $items) 
    {
        if (!empty($items) && $items === $this->items) {
            reset($items);
            $items[key($items)] = clone current($items);
        }
        $this->items = $items;
    }
    

    Regarding the second question:

    Does someone know how to preserve default tracking policy for other fields and use NotifyPropertyChanged just for the field that stores array?

    You cannot set tracking policy just for a one field.

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

Sidebar

Related Questions

I have Doctrine 2.1 entity: /** * @Entity(repositoryClass=Varlab\Model\Repository\UserRepository) * @Table(name=user) */ class User {
I have the following two entities: <?php namespace Site\AnnonceBundle\Entity; use Doctrine\ORM\Mapping as ORM; use
I have the need of get the array representation of a Doctrine 2 Entity.
I have two classes: User : /** @Entity @Table(name=users) */ class User { /**
Is there a way to have something like this in doctrine: class Entity {
I have a number of simple controller classes that use Doctrine's entity manager to
I have a 'vanilla' install of CodeIgniter 2 + Doctrine 2. My tutorial Entity.User.dcm.yml
I have the following model: WebPromocion: connection: doctrine tableName: WebPromocion columns: id: type: integer(4)
Let's say I have ordinary *Type class: class LocationType extends AbstractType { /** *
I have a Doctrine Entity (News) which I listen for the event prePersist .

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.