简体   繁体   中英

symfony2/doctrine entity property not filled

After doctrine createQueryBuilder execute, I get results with a property of objects that is not filled.

here my code

In UserSecurityManager (service)

 ... $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().') * '; }

...

Role Class

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

}

The line

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

Display

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) * 

And the data in database :

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

for superadmin, The property "name" is not filled, I don't understand.

Do you have an idea ?

I made some other tests :

If the parent of role "administrator" is the role "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) * 

If the parent of role "administrator" is the role "client" and the parent of role "client" is the role "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 !!!

Thanks and sorry for my english.

Phil

If I execute

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

before

$availableRoles = $query->getResult();

ok, the result is good but I don't know the cause yet :s.

The problem come from my code ?! or it's a bug in doctrine 2.4 ?

it would be a problem with doctrine cache. When I call this "entitymanager->clear()" before execute query, all is good.

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

What's the explain of this problem of cache ? Because I don't configure caching in my symfony project.

is "em->clear()" dangerous for the rest of application ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM