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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:01:32+00:00 2026-05-27T19:01:32+00:00

I have several doctrine models that have relationships to other models. Let’s call those

  • 0

I have several doctrine models that have relationships to other models. Let’s call those ItemsOne, ItemsTwo, ItemsThree. Each has a relationship defined to the Products doctrine class (products table) and the relationship is defined as:

$this->hasMany(
    'Models_Products as Products',
    array(
     'local'   => 'id',
     'foreign' => 'product_id',
    )
);

Nothing out of the ordinary there.

I also have another table (prices) which stores namespaced data. By that I mean that the particular table stores data based on a predefined key (i.e. Vinyl, CD, DVD) so the rows would show something like this:

media_namespace  entity_id  quantity    unit_price
CD               4          1000        0.99
DVD              4          2500        1.25
CD               7          3750        0.25
Vinyl            15         75          4.25

The entity_id is the id of each of the models accessing this table. So for instance CD/entity_id = 4 refers to ItemsOne model, DVD/entity_id = 4 refers to the ItemsTwo model, etc.

I want to create a relationship – and I don’t know if this can be done – whereas it will be namespaced in the model. So in the above snipped for the products relationship I need something like (for the ItemsOne model):

$this->hasMany(
    'Models_Prices as Prices',
    array(
     'local'   => 'id',
     'foreign' => 'entity_id',
    )
);

However the above will return all the prices with the entity_id matching the ItemsOne table which is partly correct. In the above example if entity_id = 4, it will return the CD record but also the DVD one which is not what I need. I want it to be filtering the data based on the media_namespace table. So in short what I need is:

prices.media_namespace = 'CD' AND prices.entity_id = itemsone.id

Can the above be reflected in a hasMany relationship in Doctrine? I have searched and cannot something that would help.

Any pointers are more than appreciated!

  • 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-27T19:01:32+00:00Added an answer on May 27, 2026 at 7:01 pm

    You need to use Doctrine subclasses. Create a subclass for each namespace. This will let you reference a specific set of data by class name.

    • class Models_Price
      • subclass Models_Price_CD
      • subclass Models_Price_DVD
      • subclass Models_Price_Vinyl

    In the Price class, you’ll need to specify the following:

    public function setUp()
    {
        parent::setUp();
    
        $this->setSubclasses(
            array(
                'Price_CD' => array(
                    'media_namespace' => 'CD',
                ),
                'Price_DVD' => array(
                    'media_namespace' => 'DVD',
                ),
                'Price_Vinyl' => array(
                    'media_namespace' => 'Vinyl',
                ),
            )
        );
    }
    

    Let’s make the assumption that ItemsOne uses namespace CD, ItemsTwo uses namespace DVD, and ItemsThree uses namespace Vinyl.

    For this situation, you would add the following relationship to each class:

    ItemsOne:

    $this->hasMany(
        'Models_Price_CD as Prices',
        array(
         'local'   => 'id',
         'foreign' => 'entity_id',
        )
    );
    

    ItemsTwo:

    $this->hasMany(
        'Models_Price_DVD as Prices',
        array(
         'local'   => 'id',
         'foreign' => 'entity_id',
        )
    );
    

    ItemsThree:

    $this->hasMany(
        'Models_Price_Vinyl as Prices',
        array(
         'local'   => 'id',
         'foreign' => 'entity_id',
        )
    );
    

    Now, each of the Prices relations will return only what you expect. Doctrine, seeing the subclass, will automatically namespace all the queries you perform on that table.

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

Sidebar

Related Questions

we have several tens of macro enabled excel files, each of those contains few
I have several Select which depend on each other if I select a value
I'm using Symfony and Doctrine, and have several many-to-many relations that work fine. But
I have several foreach loops after each other in a larger foreach loop.. What
I have several (as in 100s) spreadsheets using an addin (let's call it Addin2003).
In my Symfony/Doctrine app, I have a query that orders by RANDOM(). I call
I have several HTML elements (buttons) that fire the same JQuery AJAX request. When
I have several Delphi programs that maintain connections to a database (some Oracle, some
I have several different numbers in a group that range in sizes and would
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>

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.