Note that there are some explanatory texts on larger screens.

plurals
  1. POMapped Super Class Symfony2.2
    primarykey
    data
    text
    <p>I have created a Bundle (called MYBUNDLE) with just 2 entities: Menu and Group. Both were declared as mappedSuperclass because I need this bundle could be use for other projects. As a condition, projects will have to extend from these classes to customize them by setting the tables name or adding some fields. For instance:</p> <p>Classes into MYBUNDLE:</p> <pre><code>class Group{ protected $id; protected $menus; } class Menu{ protected $id; protected $groups; } </code></pre> <p>YML for mapping my entities from MYBUNDLE</p> <pre><code>Project\MYBUNDLE\Entity\Menu: type: mappedSuperclass fields: id: type: integer id: true generator: strategy: AUTO Project\MYBUNDLE\Entity\Group: type: mappedSuperclass fields: id: type: integer id: true generator: strategy: AUTO manyToMany: menus: targetEntity: Menu joinTable: name: sf_group_menu joinColumns: sf_group_id: referencedColumnName: id inverseJoinColumns: sf_menu_id: referencedColumnName: id </code></pre> <p>Classes into my child bundle:</p> <pre><code>use Project\MYBUNDLE\Entity\Group as TGroup; /** * @ORM\Entity * @ORM\Table(name="sf_group") */ class Group extends TGroup { } use Project\MYBUNDLE\Entity\Menu as TMenu; /** * @ORM\Entity * @ORM\Table(name="sf_menu") */ class Menu extends TMenu { } </code></pre> <p>However each one of the two classes have a property to make a manytomany association between them (As mappedSuperclass doesn't allow an inverse side, my association is manytomany unidirectional).</p> <p>I need to make a query inside MYBUNDLE. A query which join both tables with the manytomany association. The reason i want to make this query inside MYBUNDLE, is because this bundle has a service which draws the Menu deppending in the Group or Groups. This method should be facilitate from this bundle, so other bundles could use it and i dont have to implemment in every sub-bundle.</p> <p>My partial solution was to make a config part for my MYBUNDLE, something like:</p> <pre><code>mybundle: menu_entity: name: MyprojectChildBundle\Entity\Menu group_entity: name: MyprojectChildBundle\Entity\Group </code></pre> <p>With this configuration, I can use the repository of the child bundle inside MYBUNDLE, with this:</p> <pre><code>$this-&gt;em-&gt;getRepository("MyprojectChildBundle:Group")-findAll(); </code></pre> <p>Everything works when I make a query without joins. On the other hand, when I do this:</p> <pre><code>$repo = $this-&gt;em-&gt;getRepository("MyprojectChildBundle:Group"); $result = $repo-&gt;createQueryBuilder("c") -&gt;select('c, d') -&gt;join("c.menus", "d")-&gt;getQuery()-&gt;getResult(); return $result </code></pre> <p>Everything fails, because the SQL formed try to look for a table called "Menu" which does not exist because it is called "sf_menu". The table Group is correctly changed to "sf_group" because I am using the Repository of my child. However using this repository just change the name of that class, but not of joined tables.</p> <p>How could I make this kind of query inside MYBUNDLE? Thanks a lot.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload