Doctrine2奇怪的持续异常

时间:2012-01-20 23:46:35

标签: php doctrine-orm phpunit

在尝试持久化一个具有许多UserProperties引用的User类时,我遇到了一些奇怪的问题。请注意,UserProperty将由级联管理:persist。

UserProperties本身具有对Property的引用。

当使用新的UserProperty创建一个新用户时(它本身引用了现有的属性),它会抛出奇怪的(奇怪的,就像我没想到的那样)错误: InvalidArgumentException:通过关系'UserProperty#property'找到了一个新实体,该关系未配置为级联实体的持久化操作

用户:

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;
}
}

属性类没有对任何一个类的引用。

最后我的testClass使用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()));
}
}

奇怪的是,该属性确实是在数据库中创建的。 此外,测试工作完全正常如果我使用userDao-> persist来保存属性(而不是propertyDao ...

任何帮助将不胜感激,提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题在于我在每个dao中使用了不同的entityManager,因此每个DAO都有一个不同的UnitOfWork 。当我使实体成为单例时,每个DAO都有相同的引用。

相关问题