2

I'm trying to join the Entities PhpbbTopics and PhpbbForums on the field forumId. These are PhpBB tables and therefore do not have a foreign key on a database level. However, when joining the two Entities using DQL I get the following exception:

[Semantical Error] line 0, col 78 near 'f': Error: Class 
Project\MyBundle\Entity\PhpbbTopics has no association named forumId

The code that is being executed in my Controller is:

$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
    'SELECT f.forumName
    FROM ProjectMyBundle:PhpbbTopics t JOIN t.forumId f'
);

The two entites are:

**
 * PhpbbTopics
 *
 * @ORM\Table(name="phpbb_topics", indexes={@ORM\Index(name="forum_id", columns={"forum_id"}), @ORM\Index(name="forum_id_type", columns={"forum_id", "topic_type"}), @ORM\Index(name="last_post_time", columns={"topic_last_post_time"}), @ORM\Index(name="topic_approved", columns={"topic_approved"}), @ORM\Index(name="forum_appr_last", columns={"forum_id", "topic_approved", "topic_last_post_id"}), @ORM\Index(name="fid_time_moved", columns={"forum_id", "topic_last_post_time", "topic_moved_id"})})
 * @ORM\Entity
 */
class PhpbbTopics
{
    /**
     * @var \Project\MyBundle\Entity\PhpbbForums
     *
     * @ORM\ManyToOne(targetEntity="Project\MyBundle\Entity\PhpbbForums")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="forum_id", referencedColumnName="forum_id")
     * })
     */
    private $forumId;
}

And:

/**
 * PhpbbForums
 *
 * @ORM\Table(name="phpbb_forums", indexes={@ORM\Index(name="left_right_id", columns={"left_id", "right_id"}), @ORM\Index(name="forum_lastpost_id", columns={"forum_last_post_id"})})
 * @ORM\Entity
 */
class PhpbbForums
{
    /**
     * @var integer
     *
     * @ORM\Column(name="forum_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $forumId;

    /**
     * @var string
     *
     * @ORM\Column(name="forum_name", type="string", length=255, nullable=false)
     */
    private $forumName;
}

Both of these entities have more fields than I have shown. These fields are not used in my query. Every field also has a getter and setter, which are not shown. If you feel the need to see more of the entities, please leave a comment.

I have tried the following solutions but they did not solve my issue:

EDIT:

I found that the file PhpbbTopics.om.xml at src\Project\MyBundle\Resources\config\doctrine did not contain the relationship to PhpbbForums. I have replaced the line:

<field name="forumId" type="integer" column="forum_id" nullable="false"/>

With:

<many-to-one field="forumId" target-entity="PhpbbForums">
   <join-columns>
     <join-column name="forum_id" referenced-column-name="forum_id"/>
   </join-columns>
</many-to-one>

This did not solve or change the issue.

Community
  • 1
  • 1
Simon
  • 1,416
  • 1
  • 15
  • 24
  • Did you add the ManyToOne relation in PHPBBTopics or did it come by default? Can you try adding ```inversedBy="forumId"``` after the targetEntity? – ffflabs Aug 20 '14 at 20:02
  • @amenadiel I hadded the ManyToOne relation myself. Since there is no foreign key between the entities, no relation was created when generating the entities. After adding your suggestion, the error does no change. – Simon Aug 20 '14 at 20:28
  • Are you using some kind of cache? You can also try adding a OneToMany relation ship on PhpbbForums to match the ManyToOne you already defined – ffflabs Aug 20 '14 at 20:32
  • @amenadiel I have not configured any kind of cache manually. Are there any chaches running by default? Adding the lines: * @ORM\OneToMany(targetEntity="Project\MyBundle\Entity\PhpbbTopics", inversedBy="forumId") * @ORM\JoinColumn(name="forum_id", referencedColumnName="forum_id") to PhpbbForums.forumId does not change the error. – Simon Aug 20 '14 at 20:54

1 Answers1

0

I have solved my issue by changing the syntax of the join. The syntax I am using right now explicitly states which fields of which entities should be joined together. The new query is:

$query = $em->createQuery(
    'SELECT f.forumName
    FROM ProjectMyBundle:PhpbbTopics t JOIN ProjectMyBundle:PhpbbForums f WITH f.forumId = t.forumId'
); 

By using this query, I am able to remove the ManyToOne relationship that I have defined in PhpbbTopics.php and PhpbbTopics.om.xml. Without the declared relationship, my entity matches my database table closer as the table phpbb_topics does not have a foreign key to phpbb_forums.

Simon
  • 1,416
  • 1
  • 15
  • 24