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 7074565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:04:23+00:00 2026-05-28T06:04:23+00:00

I have 4 entities : Country, Region, Province, Town. <?php namespace Entities; use Doctrine\Common\Collections\ArrayCollection;

  • 0

I have 4 entities : Country, Region, Province, Town.

<?php

namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity (repositoryClass="Repositories\Region") 
 * @Table(name="regions") 
 * @HasLifecycleCallbacks
 */
class Region {

    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @Column(type="string", length=30,unique=TRUE) */
    private $regionname;

    /** @Column(type="boolean") */
    private $active;

    /**
     * @ManyToOne(targetEntity="Country", inversedBy="regions")
     * @JoinColumn(name="countries_id", referencedColumnName="id",nullable=FALSE)
     */
    private $countries_id;

    /**
     * @OneToMany(targetEntity="Province", mappedBy="provinces")
     */
    private $provinces;

    public function __construct() {
        $this->provinces = new ArrayCollection();
        $this->active = true;
    }

<?php

namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity (repositoryClass="Repositories\Province") 
 * @Table(name="provinces") 
 * @HasLifecycleCallbacks
 */
class Province {

    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @Column(type="string", length=30,unique=TRUE) */
    private $provincename;

    /** @Column(type="boolean") */
    private $active;

    /**
     * @ManyToOne(targetEntity="Region", inversedBy="provinces")
     * @JoinColumn(name="regions_id", referencedColumnName="id",nullable=FALSE)
     */
    private $regions_id;

    /**
     * @OneToMany(targetEntity="Town", mappedBy="towns")
     */
    private $towns;

    public function __construct() {
        $this->towns = new ArrayCollection();
        $this->active = true;
    }

<?php

namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity (repositoryClass="Repositories\Town") 
 * @Table(name="towns") 
 * @HasLifecycleCallbacks
 */
class Town {

    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @Column(type="string", length=30,unique=FALSE) */
    private $townname;

    /** @Column(type="boolean") */
    private $active;
    // so that we know when a user has added a town
    /** @Column(type="boolean") */
    private $verified;

    /**
     * @OneToMany(targetEntity="User", mappedBy="users")
     */
    private $users;

    /**
     * @ManyToOne(targetEntity="Province", inversedBy="towns")
     * @JoinColumn(name="provinces_id", referencedColumnName="id",nullable=FALSE)
     */
    private $provinces_id;

    public function __construct() {
        $this->users = new ArrayCollection();

        $this->active = true;
    }

I want to create a query using DQL that will give me a list of towns for a given region.

To get a simple list of active towns I am using :

public function findActiveTowns($provinces_id = null)
// we can pass in a specific provinces_id if we want
{

    $qb = $this->_em->createQueryBuilder();
    $qb->select('a.townname, a.id')
    ->from('Entities\Town', 'a');

    if (!is_null($provinces_id)){
        $qb->where('a.provinces_id = :provinces_id AND a.active = TRUE')
        ->setParameter('provinces_id', $provinces_id);
    } else {
        $qb->where('a.active = TRUE');
    }

    $towns=$qb->getQuery()->getResult();

    // make pairs array suitable for select lists
    $options = array();
    foreach ($towns as $key => $value) {
        $options[$value['id']] = $value['townname'];
    }
    return $options;
}

Now, to get to the point. How do I set up the joins and get this working so that we can pass in a region_id and return all of the towns in the region.

In native SQL I’d do something like this :

SELECT towns.id
FROM  `towns` 
INNER JOIN  `provinces` 
INNER JOIN  `regions` 
WHERE regions.id =1

Thanks.

  • 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-28T06:04:24+00:00Added an answer on May 28, 2026 at 6:04 am

    A few things first.

    1. Don’t name your fields with _id, because they are not identifiers, but relations to other objects. Join column annotation goes with the real DB name, field in object model go without.
    2. Write/generate get/set/add methods for all fields to encapsulate them, so u can actually use them. You can’t read private fields from “the outside”.

    As for you question, haven’t tested it, but something like this should work.

    class Town {
        /**
         * @ManyToOne(targetEntity="Province", inversedBy="towns")
         * @JoinColumn(name="provinces_id", referencedColumnName="id",nullable=FALSE)
         */
        private $province;
    

    class Province {
        /**
         * @ManyToOne(targetEntity="Region", inversedBy="provinces")
         * @JoinColumn(name="regions_id", referencedColumnName="id",nullable=FALSE)
         */
        private $region;
    

    $qb->select('a.townname, a.id')
       ->from('Entities\Town', 'a')
       ->leftJoin('a.province', 'p');
    
    if (!is_null($provinces_id) && !is_null($region_id)){
        $qb->where('a.province = :province AND a.active = TRUE')
           ->andWhere('p.region = :region')
           ->setParameter('province', $provinces_id)
           ->setParameter('region', $region_id);
    } else {
        $qb->where('a.active = TRUE');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple entity structure defined in my project: Country > Region >
I have entities Basket and BasketItem : /** * Acme\BasketBundle\Entity\Basket * * @ORM\Entity(repositoryClass=Acme\BasketBundle\Repository\BasketRepository) *
I have three entities: Country, State and City with the following relationships: When creating
I am using Entity Framework and DevExpress 10.5 XtraGrid. Imagine that we have entities
Let's say I have 2 entities in my data layer (Country and Language) that
I have 5 entity and use the custom model for the three of them
I'm using Doctrine 2 ORM and I have this problem. I have three Entities,
I have entities Group and User . the Group entity has Users property which
I have entities like these ones: Entity [products(147)] Entity [manufacturer(23)/products(131)] Entity [manufacturer(17)/products(131)] Now, I'm
I have a very simple Country entity which I want to cache. This works

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.