1

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

7
  • Strange. Only missing data for the one object? Absolutely sure that the database table has the correct information? No need to set $this->name in the constructor. Your posted code matches the actual code? Not doing anything fancy with getName()? Commented Mar 10, 2015 at 13:55
  • this is the actual code. Commented Mar 10, 2015 at 15:43
  • please fetch everything and dump it: $query = $repository->createQueryBuilder('r')->where('1 = 1'); \var_dump($query->getArrayResult()); is that data 100% correct? Commented Mar 11, 2015 at 8:59
  • I comment em->clear() line and execute the query with var_dump($query->getArrayResult()); The result is good! all properties are filled but not with getResult() and foreach ... print ...value->getName(), Strange ! Commented Mar 11, 2015 at 10:11
  • Is there any rights problem? clear the cache: app/console cache:clear --env=dev, 2. check the schema app/console doctrine:schema:validate --env=dev and at least clear doctrine cache stackoverflow.com/a/11826487/4469738 I don't think it will work. Are you 100% sure to be in dev mode? Commented Mar 11, 2015 at 10:41

2 Answers 2

1

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 ?

Sign up to request clarification or add additional context in comments.

Comments

0

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 ?

2 Comments

in dev mode there are never caching problems! don't use this
without this em->clear(), the problem occurs also in dev mode !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.