Php Doctrine2异常
我在尝试持久化一个引用了许多UserProperties的用户类时遇到了奇怪的问题。请注意,UserProperty将由cascade:persist管理 UserProperties本身具有对属性的引用 当使用新UserProperty创建一个新用户时,它本身引用了一个现有的属性,它会抛出奇怪的错误,如我没想到的错误: InvalidArgumentException:通过关系“UserPropertyproperty”找到了一个新实体,该关系未配置为级联实体的持久化操作 用户: 类用户扩展了可标识对象{ //…其他Var /** *@OneToManytargetEntity=UserProperty,mappedBy=user,cascade={persist,remove},orphandremove=true */ private$userProperties=null; 公共函数构造{ $this->userProperties=newarraycollection; } //…其他方法 公共函数getUserProperties{ 返回$this->userProperties; } 公共函数setUserProperties$userProperties{ $this->userProperties=$userProperties; } 公共函数addUserPropertyUserProperty$userProperty{ $userProperty->setUser$this; $this->userProperties[]=$userProperty; } } 用户属性: 类UserProperty扩展了IdentifiableObject{ /** *@OneToOnetargetEntity=Property *@JoinColumnname=propertyID */ 私人财产; 公共函数getProperty{ 返回$this->property; } 公共函数setProperty$property{ $this->property=$property; } } 属性类没有对这两个类的引用 最后是使用PHPUnit的testClass:Php Doctrine2异常,php,doctrine-orm,phpunit,Php,Doctrine Orm,Phpunit,我在尝试持久化一个引用了许多UserProperties的用户类时遇到了奇怪的问题。请注意,UserProperty将由cascade:persist管理 UserProperties本身具有对属性的引用 当使用新UserProperty创建一个新用户时,它本身引用了一个现有的属性,它会抛出奇怪的错误,如我没想到的错误: InvalidArgumentException:通过关系“UserPropertyproperty”找到了一个新实体,该关系未配置为级联实体的持久化操作 用户: 类用户扩展了
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()));
}
}
奇怪的是,这个属性确实是在数据库中创建的。
如果我使用userDao->persist来保存属性而不是propertyDao,那么测试也可以很好地工作
任何帮助都将不胜感激,提前感谢 问题是我在每个dao中使用了不同的entityManager,因此每个dao都有不同的工作单元。当我将实体设置为单例时,每个DAO都有相同的引用