I’m having strange problems when trying to persist a class of User that has a reference to many UserProperties. Note that a UserProperty will be managed by a cascade:persist.
UserProperties itself has a reference to a Property.
When creating a new User with a new UserProperty (which itself has a reference to an existing Property) it throws strange (strange as in i didn’t expect it) error:
InvalidArgumentException: A new entity was found through the relationship ‘UserProperty#property’ that was not configured to cascade persist operations for entity
User:
class User extends IdentifiableObject {
// … other vars
/**
* @OneToMany(targetEntity="UserProperty", mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $userProperties = null;
public function __construct() {
$this->userProperties = new ArrayCollection();
}
// … other methods
public function getUserProperties() {
return $this->userProperties;
}
public function setUserProperties($userProperties) {
$this->userProperties = $userProperties;
}
public function addUserProperty(UserProperty $userProperty) {
$userProperty->setUser($this);
$this->userProperties[] = $userProperty;
}
}
UserProperty:
class UserProperty extends IdentifiableObject {
/**
* @OneToOne(targetEntity="Property")
* @JoinColumn(name="propertyID")
*/
private $property;
public function getProperty() {
return $this->property;
}
public function setProperty($property) {
$this->property = $property;
}
}
Property class has no references to either class.
And finally my testClass using PHPUnit:
class UserDaoTest extends PHPUnit_Framework_TestCase {
private static $userDao;
private static $propertyDao;
public static function setUpBeforeClass() {
//this will make the EntityManager called inside our DAOImpl point to our test database...
define('__DBNAME__', 'db_test');
createCleanTestDatabase();
self::$userDao = new UserDaoImpl();
self::$propertyDao = new PropertyDaoImpl();
}
public function testEntityClassVariable() {
$this->assertEquals("User", self::$userDao->getEntityClass());
}
public function testPersistUserWithoutProperties() {
$user = new User();
$user->setUserName("tester1");
$user->setUserType(1);
self::$userDao->persist($user);
self::$userDao->flush();
$this->assertEquals(1, count(self::$userDao->findAll()));
}
public function testPersistUserWithProperties() {
$user = new User();
$user->setUserName("tester2");
$user->setUserType(1);
$property = new Property();
$property->setName("propertyName");
$property->setType(1);
self::$propertyDao->persist($property);
self::$propertyDao->flush();
$userProperty = new UserProperty();
$userProperty->setProperty($property);
$userProperty->setValue("test");
$user->addUserProperty($userProperty);
self::$userDao->persist($user);
self::$userDao->flush();
$this->assertEquals(2, count(self::$userDao->findAll()));
$userInDB = self::$userDao->find($user);
$this->assertNotNull($userInDB);
$this->assertEquals(1, count($userInDB->getUserProperties()));
}
}
The strange thing is that the Property is indeed created in the Database.
Also the test works perfectly fine IF i use the userDao->persist to save the Property (instead of the propertyDao…
Any help would be appreciated, thanks in advance!
The problem was that i was using a different entityManager in each dao so effectively having a different UnitOfWork for each DAO. When i made the entity a singleton so that each DAO had the same reference to it.