zend 1.11和doctrine2代理没有保湿实体正确地将字段设置为null

时间:2011-11-16 23:33:22

标签: php zend-framework doctrine-orm

我的doctrine 2.0代理有问题,没有正确地保护它的实体,导致实体字段在调用它们时返回null,如下所示: -

来自https://github.com/andyfenna/AJF-IT/blob/master/library/AJFIT/ACL/Factory.php第60行的

摘录

foreach($arrRoleResources as $roleResource) { //$roleResouces is a proxy entity

        $role = $roleResource->getRoleFk(); //

        $roleName = $role->getName(); //roleName becomes null even through the pk is populated

}

我的项目位于git hub https://github.com/andyfenna/AJF-IT

生成时代理保存为库\ AJFIT \ Entity \ Proxy \ AJFITEntityUserRoleResourcesProxy.php

代理是否应该保存为库\ AJFIT \ Entity \ Proxy \ UserRoleResourcesProxy.php?或者我错过了什么?

代理类如下所示,由doctrine自动生成。

UserRoleResources.php实体保存在库\ AJFIT \ Entity \

代理: - AJFITEntityUserRoleResourcesProxy.php

<?php

 namespace AJFIT\Entity\Proxy;

/**
 * THIS CLASS WAS GENERATED BY THE DOCTRINE ORM. DO NOT EDIT THIS FILE.
 */
class AJFITEntityUserResourcesProxy extends \AJFIT\Entity\UserResources implements\Doctrine\ORM\Proxy\Proxy
{
private $_entityPersister;
private $_identifier;
public $__isInitialized__ = false;
public function __construct($entityPersister, $identifier)
{
    $this->_entityPersister = $entityPersister;
    $this->_identifier = $identifier;
}
/** @private */
public function __load()
{
    if (!$this->__isInitialized__ && $this->_entityPersister) {
        $this->__isInitialized__ = true;

        if (method_exists($this, "__wakeup")) {
            // call this after __isInitialized__to avoid infinite recursion
            // but before loading to emulate what ClassMetadata::newInstance()
            // provides.
            $this->__wakeup();
        }

        if ($this->_entityPersister->load($this->_identifier, $this) === null) {
            throw new \Doctrine\ORM\EntityNotFoundException();
        }
        unset($this->_entityPersister, $this->_identifier);
    }
}


public function setModule($module)
{
    $this->__load();
    return parent::setModule($module);
}

public function getModule()
{
    $this->__load();
    return parent::getModule();
}

public function setController($controller)
{
    $this->__load();
    return parent::setController($controller);
}

public function getController()
{
    $this->__load();
    return parent::getController();
}

public function setAction($action)
{
    $this->__load();
    return parent::setAction($action);
}

public function getAction()
{
    $this->__load();
    return parent::getAction();
}

public function setName($name)
{
    $this->__load();
    return parent::setName($name);
}

public function getName()
{
    $this->__load();
    return parent::getName();
}

public function setRoutename($routename)
{
    $this->__load();
    return parent::setRoutename($routename);
}

public function getRoutename()
{
    $this->__load();
    return parent::getRoutename();
}

public function setModifed($modifed)
{
    $this->__load();
    return parent::setModifed($modifed);
}

public function getModifed()
{
    $this->__load();
    return parent::getModifed();
}

public function setCreated($created)
{
    $this->__load();
    return parent::setCreated($created);
}

public function getCreated()
{
    $this->__load();
    return parent::getCreated();
}

public function getPk()
{
    $this->__load();
    return parent::getPk();
}


public function __sleep()
{
    return array('__isInitialized__', 'module', 'controller', 'action', 'name', 'routename', 'modifed', 'created', 'pk');
}

public function __clone()
{
    if (!$this->__isInitialized__ && $this->_entityPersister) {
        $this->__isInitialized__ = true;
        $class = $this->_entityPersister->getClassMetadata();
        $original = $this->_entityPersister->load($this->_identifier);
        if ($original === null) {
            throw new \Doctrine\ORM\EntityNotFoundException();
        }
        foreach ($class->reflFields AS $field => $reflProperty) {
            $reflProperty->setValue($this, $reflProperty->getValue($original));
          }
           unset($this->_entityPersister, $this->_identifier);
       }

   }
}

实体: - UserRoleResources.php

<?php

namespace AJFIT\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* UserRoleResources
 *
* @Table(name="user_role_resources")
* @Entity
*/
class UserRoleResources
{
/**
 * @var datetime $modified
 *
 * @Column(name="modified", type="datetime")
 */
private $modified;

/**
 * @var datetime $created
 *
 * @Column(name="created", type="datetime")
 */
private $created;

/**
 * @var integer $pk
 *
 * @Column(name="pk", type="integer")
 * @Id
 * @GeneratedValue(strategy="IDENTITY")
 */
private $pk;

/**
 * @var serResources
 *
 * @ManyToOne(targetEntity="UserResources")
 * @JoinColumn(name="resources_fk", referencedColumnName="pk")
 */
private $resourcesFk;

/**
 * @var UserRoles
 *
 * @ManyToOne(targetEntity="UserRoles")
 * @JoinColumn(name="role_fk", referencedColumnName="pk")
 */
private $roleFk;


public function __construct() 
{
    $this->roleFk = new ArrayCollection();
}
/**
 * Set modified
 *
 * @param datetime $modified
 */
public function setModified($modified)
{
    $this->modified = $modified;
}

/**
 * Get modified
 *
 * @return datetime 
 */
public function getModified()
{
    return $this->modified;
}

/**
 * Set created
 *
 * @param datetime $created
 */
public function setCreated($created)
{
    $this->created = $created;
}

/**
 * Get created
 *
 * @return datetime 
 */
public function getCreated()
{
    return $this->created;
}

/**
 * Get pk
 *
 * @return integer 
 */
public function getPk()
{
    return $this->pk;
}

/**
 * Set resourcesFk
 *
 * @param UserResources $resourcesFk
 */
public function setResourcesFk(UserResources $resourcesFk)
{
    $this->resourcesFk = $resourcesFk;
}


public function getResourcesFk()
{
    return $this->resourcesFk;
}

/**
 * Set roleFk
 *
 * @param UserRoles $roleFk
 */
public function setRoleFk(UserRoles $roleFk)
{
    $this->roleFk = $roleFk;
}

public function getRoleFk()
{
    return $this->roleFk;
}
}

有没有人有任何想法?任何有关此事的帮助都会被贬低。

感谢您

我已经包含了我正在进行的调试步骤,希望有人知道发生了什么: -

初始断点

Initial break point

逐步代理

Step through to proxy

逐步进行代理加载

Step through to proxy load

实体的根除

Hyradation of entity

返回代理

Return to proxy

进入实体

Step into entity

返回调用者(你可以看到变量$ roleName为null为什么?)

Return to caller

1 个答案:

答案 0 :(得分:0)

我遇到过类似的问题,即我的一个实体属性没有将其值存储在数据库中

根本原因是它的注释不正确(即以某种方式反引用进入注释)

/**` (this backtick is culprit)
 * @var string
 *
 * @ORM\Column(name="condition", type="text", nullable=true)
 */

如果某人面临类似问题,首先需要检查其注释部分

p.s:我知道这个问题已经有一年了但是我回答这个问题因为它可能有助于其他人