Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Blocker
-
Resolution: Invalid
-
Affects Version/s: 2.0-ALPHA3
-
Fix Version/s: None
-
Component/s: None
-
Security Level: All
-
Labels:None
-
Environment:HideUbuntu 9.04 -
PHP 5.3.1-0.dotdeb.0 with Suhosin-Patch (cli) (built: Nov 28 2009 13:18:25)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans
with Suhosin v0.9.29, Copyright (c) 2007, by SektionEins GmbH
-
PostgreSQL 8.3ShowUbuntu 9.04 - PHP 5.3.1-0.dotdeb.0 with Suhosin-Patch (cli) (built: Nov 28 2009 13:18:25) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans with Suhosin v0.9.29, Copyright (c) 2007, by SektionEins GmbH - PostgreSQL 8.3
Description
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 ?
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:
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.