Zend 1.11.11 Doctrine 2.1.2 initialising of associative proxy entities

大城市里の小女人 提交于 2019-12-22 16:36:43

问题


I am having this strange issue with an associative entity being a proxy class and its methods always returning null. I hope someone can shed a little light on the subject because it's driving me insane.

I am calling this code:

$arrRoleResources = $em->getRepository("AJFIT\Entity\UserRoleResources")->findAll();    

foreach($arrRoleResources as $roleResource) {
       $name = $roleResource->getRoleFk()->getName();   
} 

The $name will always be null even though when I debug the code it initializes the proxy class and the $_identifier is with the correct primary key.

UserRoleResources Entity:

namespace AJFIT\Entity;
/** 
 * UserRoleResources 
 * * @Table(name="user_role_resources") 
 * * @Entity(repositoryClass="AJFIT\Repository\UserRoleResources") 
 */ 
class UserRoleResources {     
   /**    
    * @var UserRoles    
    *    
    * @ManyToOne(targetEntity="UserRoles")    
    * @JoinColumn(name="role_fk", referencedColumnName="pk")    
    *     
    */     

    private $roleFk;
    /**    
     * Get roleFk    
     *    
     * @return UserRoles $roleFk    
     */     

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

UserRole Entity:

namespace AJFIT\Entity;  

/**  
 * UserRoles  
 * * @Table(name="user_roles")  
 * * @Entity(repositoryClass="AJFIT\Repository\UserRoles")  
 */ 
 class UserRoles {     

     /**     
      * @var string $name    
      *
      * @Column(name="name", type="string", length=255)     
      */     

     private $name;      

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

      private $pk;      

      /**     
       * Get name     
       *     
       * @return string $name     
       */     

       public function getName()     {         
           return $this->name;     
       } 
 } 

I have followed the zf-boilerplate precompiled example, and i am able to post my config if required.


回答1:


Some of your annotations look a little wrong. Try fixing these up...

Class annotation

Lose the extra asterisks.

/** 
 * UserRoleResources 
 * 
 * @Table(name="user_role_resources") 
 * @Entity(repositoryClass="AJFIT\Repository\UserRoleResources") 
 */ 
class UserRoleResources {

Property annotation

Place the docblocks directly above the properties (no blank lines)

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


来源:https://stackoverflow.com/questions/7804392/zend-1-11-11-doctrine-2-1-2-initialising-of-associative-proxy-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!