Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7162849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:48:11+00:00 2026-05-28T13:48:11+00:00

I’m trying to do an association of 5 objects with Doctrine2 (PHP). I’m using

  • 0

I’m trying to do an association of 5 objects with Doctrine2 (PHP).
I’m using PostgreSQL.

Here is the Database schema:
Database schema

A company may have many Hubs, each Hub have one Harbor.
A company may have many Line, each Line have a Linelist.
A Linelist have 2 Harbors.
For example, a Linelist is “Los Angeles-Seattle”, and multiple companies may own it thanks to the Line table.

I’m trying to query all the Hub, Harbor, Linelist, and Line for one company.
I have the SQL query:

SELECT *
FROM hub h
JOIN harbor a
ON a.id = h.harbor_id
JOIN linelist l
ON (l.harborstart_id = a.id OR l.harborend_id = a.id)
JOIN line m
ON m.linelist_id = l.id
WHERE h.company_id = 41
AND m.company_id = 41"

I’m trying to do the same using DQL.
I tried this, but it doesn’t worked:

$query = $this->getEntityManager()
->createQuery('SELECT h, a, l, m
               FROM AmGameBundle:Hub h
               JOIN h.harbor a
               JOIN a.linelist l
               JOIN l.line m
               WHERE h.company = :company_id
               AND m.company = :company_id')
->setParameter('company_id', $company_id);

As a result, I only have the LineList and Line objects matching harborstart_id, but I want the one matching either harborstart_id or harborend_id.

Do you think this is possible in DQL?
It might be better to change the relation between Harbor and Linelist for a many to many?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:48:12+00:00Added an answer on May 28, 2026 at 1:48 pm

    I think that’s a matter of defining 2 relations from harbor to linelist in your Entities. I imagine you have something like

    <?php
    /**
     * @Entity
     * @Table(name="LineList")
     */
    class LineList {
        /** 
         * @var object $startHarbor
         * @ManyToOne(targetEntity="Harbor", inversedBy="startHarbors")
         * @JoinColumn(name="harborstart_id", referencedColumnName="id", nullable=FALSE)
         */
        protected $startHarbor;
    }
    
    /**
     * @Entity
     * @Table(name="Harbor")
     */
    class Harbor {
        /** 
         * @var object $startHarbors
         * @OneToMany(targetEntity="LineList", mappedBy="startHarbor")
         */
        protected $startHarbors;
    }
    

    That will let you join Harbors to LineLists via harborstart_id (you named the variable linelist, but I think now it’s better to change the identifiers as there will be 2 referring to the same foreign table), then if you want to harborend_id

    <?php
    /**
     * @Entity
     * @Table(name="LineList")
     */
    class LineList {
        /** 
         * @var object $startHarbor
         * @ManyToOne(targetEntity="Harbor", inversedBy="startHarbors")
         * @JoinColumn(name="harborstart_id", referencedColumnName="id", nullable=FALSE)
         */
        protected $startHarbor;
    
        /** 
         * @var object $endHarbor
         * @ManyToOne(targetEntity="Harbor", inversedBy="endHarbors")
         * @JoinColumn(name="harborend_id", referencedColumnName="id", nullable=FALSE)
         */
        protected $endHarbor;
    }
    
    /**
     * @Entity
     * @Table(name="Harbor")
     */
    class Harbor {
        /** 
         * @var object $startHarbors
         * @OneToMany(targetEntity="LineList", mappedBy="startHarbor")
         */
        protected $startHarbors;
    
        /** 
         * @var object $endHarbors
         * @OneToMany(targetEntity="LineList", mappedBy="endHarbor")
         */
        protected $endHarbors;
    }
    

    Now you can change the DQL to:

    $query = $this->getEntityManager()
    ->createQuery('SELECT h, a, sh, eh, m
                   FROM AmGameBundle:Hub h
                   JOIN h.harbor a
                   JOIN a.startHarbors sh
                   JOIN a.endHarbors eh
                   JOIN l.line m
                   WHERE h.company = :company_id
                   AND m.company = :company_id')
    ->setParameter('company_id', $company_id);
    

    That should get you in the right direction. If it becomes troublesome though, a many-to-many approach as you speculated should be a well documented solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.