DDC-750: Named scopes (conditional columns and associations) #925

Closed
opened 2026-01-22 12:55:43 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 17, 2010).

Jira issue originally created by user jantichy:

I would like to propose new functionality - conditional columns and associations. I mean something like named scopes in Rails active record.

For example if I have association to all my shirts:

/*** @Entity **/
class Person 
{
    /****
     * @OneToMany(targetEntity="Shirt", mappedBy="person")
     */
    private $shirts;
}

And if I want red shirts only often, I woult appreciate something like:

/*** @Entity **/
class Person 
{
    /****
     * @OneToMany(targetEntity="Shirt", mappedBy="person")
     */
    private $shirts;

    /****
     * @OneToMany(targetEntity="Shirt", mappedBy="person", condition="shirt.color='red'")
     */
    private $redShirts;
}

This would help me to increase application efficiency in particular cases (if I need only red shirts in one user request) because only one SELECT for red shirts is sent to database. Moreover, I wouldn't have to define anything more. Today I have to define the select or the restriction manually, for example in following way:

/*** @Entity **/
class Person 
{
    /****
     * @OneToMany(targetEntity="Shirt", mappedBy="person")
     */
    private $shirts;

    private $redShirts = NULL;

    public function getRedShirts()
    {
        if (is_null($this->redShirts)) {
            $this->redShirts = array();
            foreach ($this->shirts as $shirt) {
                if ($shirt->color == 'red') {
                    $this->redShirts[] = $shirt;
                }
            }
        }
        return $this->redShirts;
    }
}

Thanks, Jan

Originally created by @doctrinebot on GitHub (Aug 17, 2010). Jira issue originally created by user jantichy: I would like to propose new functionality - conditional columns and associations. I mean something like named scopes in Rails active record. For example if I have association to all my shirts: ``` /*** @Entity **/ class Person { /**** * @OneToMany(targetEntity="Shirt", mappedBy="person") */ private $shirts; } ``` And if I want red shirts only often, I woult appreciate something like: ``` /*** @Entity **/ class Person { /**** * @OneToMany(targetEntity="Shirt", mappedBy="person") */ private $shirts; /**** * @OneToMany(targetEntity="Shirt", mappedBy="person", condition="shirt.color='red'") */ private $redShirts; } ``` This would help me to increase application efficiency in particular cases (if I need only red shirts in one user request) because only one SELECT for red shirts is sent to database. Moreover, I wouldn't have to define anything more. Today I have to define the select or the restriction manually, for example in following way: ``` /*** @Entity **/ class Person { /**** * @OneToMany(targetEntity="Shirt", mappedBy="person") */ private $shirts; private $redShirts = NULL; public function getRedShirts() { if (is_null($this->redShirts)) { $this->redShirts = array(); foreach ($this->shirts as $shirt) { if ($shirt->color == 'red') { $this->redShirts[] = $shirt; } } } return $this->redShirts; } } ``` Thanks, Jan
admin added the New Feature label 2026-01-22 12:55:43 +01:00
admin closed this issue 2026-01-22 12:55:43 +01:00
Author
Owner
@doctrinebot commented on GitHub (Aug 17, 2010): - duplicates [DDC-551: Consider adding ability to specify additional join conditions on a @JoinTable / @JoinColumn](http://www.doctrine-project.org/jira/browse/DDC-551)
Author
Owner

@doctrinebot commented on GitHub (Aug 17, 2010):

Comment created by @beberlei:

this is what i described at http://www.doctrine-project.org/projects/orm/2.0/docs/reference/limitations-and-known-issues/en#current-limitations:applying-filter-rules-to-any-query

We want this feature too, just have to be careful how we can implement it due to the difference of Repository and DQL access to entities.

@doctrinebot commented on GitHub (Aug 17, 2010): Comment created by @beberlei: this is what i described at http://www.doctrine-project.org/projects/orm/2.0/docs/reference/limitations-and-known-issues/en#current-limitations:applying-filter-rules-to-any-query We want this feature too, just have to be careful how we can implement it due to the difference of Repository and DQL access to entities.
Author
Owner

@doctrinebot commented on GitHub (May 17, 2011):

Comment created by wilmoore:

Just updating the link posted by Benjamin:
http://www.doctrine-project.org/docs/orm/2.0/en/reference/limitations-and-known-issues.html#applying-filter-rules-to-any-query

@doctrinebot commented on GitHub (May 17, 2011): Comment created by wilmoore: Just updating the link posted by Benjamin: http://www.doctrine-project.org/docs/orm/2.0/en/reference/limitations-and-known-issues.html#applying-filter-rules-to-any-query
Author
Owner

@doctrinebot commented on GitHub (Aug 26, 2011):

Comment created by @beberlei:

Duplicate of DDC-551

@doctrinebot commented on GitHub (Aug 26, 2011): Comment created by @beberlei: Duplicate of [DDC-551](http://www.doctrine-project.org/jira/browse/DDC-551)
Author
Owner

@doctrinebot commented on GitHub (Aug 26, 2011):

Issue was closed with resolution "Duplicate"

@doctrinebot commented on GitHub (Aug 26, 2011): Issue was closed with resolution "Duplicate"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#925