2

I have two entities in a OneToMany relationship, Perfil and IPerfil. Perfil can have several IPerfil's associated:

>

    class Perfil  
    {  
    /**  
     * @var integer $id  
     *  
     * @ORM\Column(name="id_perfiles", type="integer")  
     * @ORM\Id  
     * @ORM\GeneratedValue(strategy="SEQUENCE")  
     * @ORM\SequenceGenerator(sequenceName="P360.perfiles_seq")  
     * @ORM\OneToMany(targetEntity="IPerfil", mappedBy="id")  
*/  
    private $id;  

And the other one:

>

class IPerfil  
{  
    /**  
     * @var integer $id
     * @ORM\ManyToOne(targetEntity="Perfil")  
     * @ORM\JoinColumn(name="id_perfiles", referencedColumnName="id_perfiles", onDelete="CASCADE")  
*/  
    protected $id;

I'm able to lazyload from IPerfil to Perfil, but not in the other direction (from Perfil to IPerfil), but if I can't do any DQL over IPerfil or createQueryBuilder()->setPameter... Symfony will always return an error like this:

[Semantical Error] line 0, col 9 near 'id_perfiles FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

If I define the field Id with an

@ORM\Column(name="id_perfiles", type="integer") Then I'm able to build DQL's with the entity, but the relationship won't work anymore.

I'm lost around here... It seems I'm missing something in the relationship definition, but have no clue.

Thanks in advance for any tip.

EDIT

If I use:

$entities=$em->getRepository('usuariosBundle:IPerfil')->findAll(array());

It doen'st gives an error, but I need to filter and order the results.


$entities=$em->getRepository('usuariosBundle:IPerfil')->findBy(array(),array('id' => 'asc', 'descripcion' => 'asc'));

Unrecognized field: id


$query=$em->getRepository('usuariosBundle:IPerfil')->createQueryBuilder('p')
        ->orderBy('id', 'ASC')
        ->getQuery();       
        $entities = $query->getResult();

*[Semantical Error] line 0, col 60 near 'id ASC': Error: 'id' is not defined. *


$entities = $this->get('doctrine')->getEntityManager()->createQuery('SELECT p.id FROM usuariosBundle:IPerfil p ')->getResult(); 

[Semantical Error] line 0, col 9 near 'id FROM usuariosBundle:IPerfil': Error: Invalid PathExpression. Must be a StateFieldPathExpression.


Joseph at SwiftOtter
  • 4,276
  • 5
  • 37
  • 55
g0ldan
  • 21
  • 4

2 Answers2

1

Use attribute name of class, no column name on dql => 'id'

->orderBy('p.id', 'ASC') and ->createQuery('SELECT p FROM usuariosBundle:IPerfil p ')->getResult(), the name of bundle is usuarios os Usuarios ?

Ciro Vargas
  • 422
  • 1
  • 5
  • 16
  • Of course I did, I use DQL's in other Controllers and it goes fine. Just won't work here. I'm on Oracle 11, forgot to mention, not sure if does matter. – g0ldan Dec 21 '12 at 13:11
  • Just Edited the Question with info about the errors I'm getting. – g0ldan Dec 21 '12 at 13:22
  • ->orderBy('p.id', 'ASC') and ->createQuery('SELECT p FROM usuariosBundle:IPerfil p ')->getResult(), the name of bundle is usuarios os Usuarios ? – Ciro Vargas Dec 21 '12 at 15:02
  • Hello @Ciro Vargas, maybe I'm missing something, but I think you didn't see that I updated the question. I already tried both ways you propose. The bundle's name is 'usuarios'. I'm not really seeing the point in this error. Btw, why isn't necessary @ORM\Column when you spicify a Relationship? – g0ldan Dec 24 '12 at 08:12
  • Ah sorry, I've seen the difference that you use in the ->OrderBy() 'p.id' instead of 'p', and that works! Thanks for the help. But I'm still unable to filter or groupby, it is still giving the same errors as I described in the question. However I'm not seeing a real difference with the methods I tried, don't really know why they won't work. I'm starting to be really lost around here. Thanks again. – g0ldan Dec 24 '12 at 08:41
  • If it is useful to anyone, I finally created a non-persistent attribute in the Perfil entity to attach a collection of IPerfil's there: `code` /** * @ORM\OneToMany(targetEntity="IPerfil", mappedBy="id") */ private $descripcion; `code` – g0ldan Feb 06 '13 at 15:59
  • If it is useful to anyone, I finally created a non-persistent attribute in the Perfil entity to attach a collection of IPerfil's there: `/**` `* @ORM\OneToMany(targetEntity="IPerfil", mappedBy="id")` `*/` `private $descripcion;` And now I'm able to lazy load in the right direction and do any necessary operation. – g0ldan Feb 06 '13 at 16:00
0

If it is useful to anyone, I finally created a non-persistent attribute in the Perfil entity to attach the the relationship and a collection of IPerfil's there:

/** 
* @ORM\OneToMany(targetEntity="IPerfil", mappedBy="id") 
*/ 
private $desc; 

Need to declare it as a ArrayCollection:

public function __construct()
{
    $this->desc = new \Doctrine\Common\Collections\ArrayCollection();
}

And now I'm able to lazy load in the right direction and do any necessary operation. I suppouse Doctrine couldnt load the collection into id, as it wasn't declared as a collection and it's the ID field.

g0ldan
  • 21
  • 4