symfony2 / doctrine实体属性未填充

时间:2015-03-10 08:50:35

标签: php symfony doctrine-orm doctrine

在doctrine createQueryBuilder执行之后,我得到的结果包含未填充的对象属性。

这里是我的代码

在UserSecurityManager(服务)

 ... $repository = $this->em->getRepository('VenusUserBundle:Role');
       $query = $repository->createQueryBuilder('r')
       ->where('r.lft >= :role_lft AND r.rgt <= :role_rgt')
       ->orderBy('r.rgt', 'ASC')
       ->setParameter('role_lft', $result['lft'])
       ->setParameter('role_rgt', $result['rgt'])
       ->getQuery();
       $availableRoles = $query->getResult(); ... 
 //debug 
 foreach ($availableRoles as $key => $value) {  echo '<br>CODE='.$value->getCode().' LFT='.$value->getLft().' NAME=('.$value->getName().') * '; }

...

角色等级

namespace MyApp\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\RoleInterface;

/**
 * @ORM\Entity
 * @ORM\Table(name="Role")
 */
class Role implements RoleInterface, \Serializable
{
  /**
   * @ORM\Id
   * @ORM\Column(type="string", length=100)
   *
   * @var string $code
   */
  protected $code;

  /**
   * @ORM\Column(name="name", type="string", length=100)
   *
   * @var string $name
   */
  private $name; 

  /**
   * @ORM\Column(type="integer")
   *
   * @var integer $lft
   */
  protected $lft; //nested tree

  /**
   * @ORM\Column(type="integer")
   *
   * @var integer $rgt
   */
  protected $rgt; //nested tree


  /**
   * @ORM\OneToMany(targetEntity="Role", mappedBy="parent")
   */
  private $children;

  /**
   * @ORM\ManyToOne(targetEntity="Role", inversedBy="children")
   * @ORM\JoinColumn(name="parent_code", referencedColumnName="code")
   */
  private $parent;

  /**
   * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
   *
   * @var \Doctrine\Common\Collections\ArrayCollection
   */
  protected $users;

    public function __construct()
    {
      $this->name = '';
    $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }

  // @see \Serializable::serialize()
  public function serialize()
  {
    // ! Don't serialize $users field !
    return \serialize(array(
    $this->code,
    $this->name,
    $this->parent,
    $this->children,
    ));
  }

  // @see \Serializable::unserialize()
  public function unserialize($serialized)
  {
    list(
    $this->code,
    $this->name,
    $this->parent,
    $this->children,
    ) = \unserialize($serialized);
  }

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

  /**
   * Sets the role code.
   *
   * @param string $value The code.
   */
  public function setCode($value)
  {
    $this->code = $value;
  }

  /**
   * Gets the code.
   *
   * @return integer The code.
   */
  public function getCode()
  {
      return $this->code;
  }

  /**
   * Gets the role name.
   *
   * @return string The name.
   */
  public function getName()
  {
    return $this->name;
  } 
  /**
   * Sets the role name.
   *
   * @param string $name The name.
   */
  public function setName($name)
  {
    $this->name = $name;
  }

  /**
   * Get parent
   *
   * @return MyApp\UserBundle\Entity\Role 
   */
  public function getParent()
  {
      return $this->parent;
  }
  /**
   * Set parent
   *
   * @param MyApp\UserBundle\Entity\Role $role
   */
  public function setParent(\MyApp\UserBundle\Entity\Role $role)
  {
      $this->parent = $role;
  }

  /**
   * Gets the role left.
   *
   * @return string The left.
   */
  public function getLft()
  {
    return $this->lft;
  } 
  /**
   * Sets the role left.
   *
   * @param string $left Left.
   */
  public function setLft($lft)
  {
    $this->lft = $lft;
  }

  public function getRole()
  {
    return $this->code;
  }


  /**
   * Remove a user
   *
   * @param \MyApp\UserBundle\Entity\User $user
   */
  public function removeUser(\MyApp\UserBundle\Entity\User $user)
  {
    $this->users->removeElement($user);
  }
  /**
   * Add a user
   *
   * @param \MyApp\UserBundle\Entity\User $user
   */
  public function addUser(\MyApp\UserBundle\Entity\User $user)
  {
      $this->users->add($user);
  }
  /**
   * Remove all users
   *
   */
  public function removeUsers()
  {
    $this->users->clear();
  }
  /**
   * Set the collection of related users
   *
   * @param \Doctrine\Common\Collections\ArrayCollection $users
   */
  public function setUsers(\Doctrine\Common\Collections\ArrayCollection $users)
  {
    $this->users = $users;
  }
  /**
   * Get users
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getUsers()
  {
      return $this->users;
  }

}

该行

  foreach ($availableRoles as $key => $value) {  
  echo '<br>CODE='.$value->getCode().' LFT='.$value->getLft().' NAME=('.$value->getName().') * '; }

显示

CODE=client LFT=4 NAME=(client) * 
CODE=studio LFT=6 NAME=(studio) * 
CODE=commercial LFT=8 NAME=(commercial) * 
CODE=user_manager LFT=11 NAME=(user manager) * 
CODE=company_manager LFT=13 NAME=(company manager) * 
CODE=admin LFT=3 NAME=(administrator) * 
CODE=prod LFT=10 NAME=(prod) * 
CODE=superadmin LFT= NAME=() * //<-- THE BUG : name is empty !!! 
CODE=root LFT=1 NAME=(megaroot) * 

数据库中的数据:

code              parent_code     name             lft rgt
admin             superadmin      administrator    3   15
client            admin           client           4   5
commercial        admin           commercial       8   9
company_manager   admin           company manager  13  14
prod              admin           prod             10  15
root              NULL            megaroot         1   18
studio            admin           studio           6   7
superadmin        root            superadmin       2   15
user_manager      admin           user manager     11  12

对于superadmin,属性“名称”未填写,我不明白。

你有什么想法吗?

我做了一些其他测试:

如果角色“administrator”的父级是角色“client”

code               parent_code   name               lft    rgt
admin              client        administrator       3     15
client             admin         client              4      5
commercial         admin         commercial          8      9
company_manager    admin         company manager    13     14
prod               admin         prod               10     15
root               NULL          megaroot            1     18
studio             admin         studio              6      7
superadmin         root          superadmin          2     15
user_manager       admin         user manager       11     12


CODE=client LFT= NAME=() *          <-- BUG HERE !!!
CODE=studio LFT=6 NAME=(studio) * 
CODE=commercial LFT=8 NAME=(commercial) * 
CODE=user_manager LFT=11 NAME=(user manager) * 
CODE=company_manager LFT=13 NAME=(company manager) * 
CODE=admin LFT=3 NAME=(administrator) * 
CODE=prod LFT=10 NAME=(prod) * 
CODE=superadmin LFT=2 NAME=(superadmin) * 
CODE=root LFT=1 NAME=(megaroot) * 

如果角色“administrator”的父级是角色“client”而角色“client”的父级是角色“root”

code               parent_code   name               lft    rgt
admin              client        administrator       3     15
client             admin         client              4      5
commercial         admin         commercial          8      9
company_manager    admin         company manager    13     14
prod               admin         prod               10     15
root               NULL          megaroot            1     18
studio             admin         studio              6      7
superadmin         root          superadmin          2     15
user_manager       admin         user manager       11     12

CODE=client LFT= NAME=() *          <-- BUG HERE !!!
CODE=studio LFT=6 NAME=(studio) * 
CODE=commercial LFT=8 NAME=(commercial) * 
CODE=user_manager LFT=11 NAME=(user manager) * 
CODE=company_manager LFT=13 NAME=(company manager) * 
CODE=admin LFT=3 NAME=(administrator) * 
CODE=prod LFT=10 NAME=(prod) * 
CODE=superadmin LFT=2 NAME=(superadmin) * 
CODE=root LFT= NAME=() *          <-- BUG HERE !!!

谢谢,对不起我的英语。

菲尔

2 个答案:

答案 0 :(得分:1)

如果我执行

$query->setHint(\Doctrine\ORM\Query::HINT_REFRESH, true);

之前

$availableRoles = $query->getResult();
好的,结果很好,但我还不知道原因:s。

问题来自我的代码?!或者它是学说2.4中的一个错误?

答案 1 :(得分:0)

这将是教条缓存的问题。 当我打电话给这个&#34; entitymanager-&gt; clear()&#34;在执行查询之前,一切都很好。

$this->em->clear('MyApp\UserBundle\Entity\Role');
$repository = $this->em->getRepository('MyAppUserBundle:Role');

这个缓存问题的解释是什么?因为我没有在symfony项目中配置缓存。

是&#34; em-&gt; clear()&#34;对其余的申请有危险吗?