We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    I’m trying to do a query that joins on 2 other tables, but I’ve noticed that the query generated is not valid:

    SELECT Jobs.*, EducationLevels.description as education_levels_description, JobsCategories.* 
    FROM `jobs` AS `Jobs` 
    LEFT JOIN `education_levels` `EducationLevels` ON `Jobs`.`education_level_id` = `EducationLevels`.`education_level_id` 
    LEFT JOIN `jobs_categories` `JobsCategories` ON 
    WHERE ( `JobsCategories`.`category_id` = '3' AND `Jobs`.`is_published` = '1' )
    


    Do you see the 2nd LEFT JOIN has an unqualified "ON" statement? What’s up with that? Did I define my relationships incorrectly?

    My code is something like this:
    $query = $my_xpdo->newQuery('Jobs');
    $query->leftJoin('EducationLevels');
    $query->leftJoin('JobsCategories');
    $query->select( 'Jobs.*, EducationLevels.description as education_levels_description, JobsCategories.*' );
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Ooops... figured it out. It had to do with my XML schema. I need to document this formally, but for now, remember that if you have a 3 table many-to-many type of relationship, e.g. a job can be grouped into zero or many categories:


      • jobs
      • categories
      • jobs_categories -- contains a job_id and a category_id

      Then your schema for Jobs must include an aggregate relationship to the jobs_categories, e.g.

      		<composite alias="Jobs" class="Jobs" local="job_id" foreign="job_id" cardinality="many" owner="local" />
      		<aggregate alias="JobsCategories" class="JobsCategories" local="job_id" foreign="job_id" cardinality="one" owner="local" />