Skip to content

Loading…

DDC-231: YAML isn't persisting correcty #3013

Closed
doctrinebot opened this Issue · 5 comments

1 participant

@doctrinebot

Jira issue originally created by user hgirardi:

I'm have some problems when I try to persist..
I've created these classes:

class Customer
{
    public $id;
    public $users;

    public function **construct()
    {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

class User
{
    public $id;
    public $customer;
}

And then I make the mappers with YAML

Ciutat\DomainModel\Customer:
  type: entity
  table: customer
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: auto
      sequenceGenerator:
        sequenceName: customer*id*seq
        allocationSize: 1
        initialValue: 1
  oneToMany:
    users:
      targetEntity: User
      mappedBy: customer
      cascade: cascadePersist

Ciutat\DomainModel\User:
  type: entity
  table: user1
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: auto
      sequenceGenerator:
        sequenceName: user1*id*seq
        allocationSize: 1
        initialValue: 1
  manyToOne:
    customer:
      targetEntity: Customer
      joinColumn:
        name: customerId
        referencedColumnName: id

And when I try to persist some data

$costumer = new DomainModel\Customer();
$user = new DomainModel\User();
$user2 = new DomainModel\User();
$user3 = new DomainModel\User();

$costumer->users->add($user);
$costumer->users->add($user2);
$costumer->users->add($user3);

$em->persist($costumer);
$em->flush();

the result in my database is not what I think it must be expected

select * from customer;
 id 
----
  1

elect * from user1;
 id | customerid 
----+------------
  1 |           
  2 |           
  3 |           

I would like to know if have some mistake with my code or is there a problem with YAML? 'cause I think there should be some data on 'customerId', don't I ?

@doctrinebot

Comment created by romanb:

This is expected behavior. You must maintain the bidirectional association properly. Doctrine only looks at the "owning side" of a bidirectional association. In the case of a bidirectional one-to-many, like in your case, the owning side is always the many-side. Since you only add the elements to the collection, but dont set the customer property pointing back to the customer, the association does not exist for Doctrine.

Change your code to:

$costumer = new DomainModel\Customer();
$user = new DomainModel\User();
$user2 = new DomainModel\User();
$user3 = new DomainModel\User();

$costumer->users->add($user);
$user->customer = $costumer;
$costumer->users->add($user2);
$user2->customer = $costumer;
$costumer->users->add($user3);
$user3->customer = $costumer;

$em->persist($costumer);
$em->flush();

And it should work.

Please read this section carefully: http://www.doctrine-project.org/documentation/manual/2_0/en/association-mapping#owning-side-and-inverse-side

Its a simple, yet essential concept to understand.

Hope that helps.

@doctrinebot

Issue was closed with resolution "Invalid"

@doctrinebot

Comment created by hgirardi:

@doctrinebot

Comment created by romanb:

You dont need prePersist or postPersist. You just need to maintain your bidirectional associations properly. This is nothing doctrine specific, basic OOP.

And it is not automatic because this would break transparency. Its not the task of the ORM to fix your broken associations. Then your object model is suddenly broken without the persistence layer.

If you want a unidirectional one-to-many, you must map it through a join table. Its all in the documentation.

The behavior is very consistent and predictable (compared to what D1 does, for example...)

@doctrinebot

Comment created by hgirardi:

I know I dont need prePersist or postPersist, It was just my choice, I just created them to make it automaticaly..
and I don't want a unidirectional..there's no reason to have another table to join them..
but now I understood why it's not automatic..

thanks man!

@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.