Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💡 Scope Classes for Relation Attributes #493

Open
butschster opened this issue Aug 5, 2024 · 0 comments
Open

💡 Scope Classes for Relation Attributes #493

butschster opened this issue Aug 5, 2024 · 0 comments
Labels
type:feature New feature.

Comments

@butschster
Copy link
Contributor

butschster commented Aug 5, 2024

I have an idea!

Add support for scope classes in relation attributes to allow for more complex and reusable query conditions in entity relationships.

Problem

Currently, CycleORM allows defining relationships for an entity using attributes, but there's no built-in way to add additional conditions or complex queries using these attributes. This limitation makes it difficult to handle scenarios where more specific criteria are needed for relationship resolution, such as selecting the latest record or applying custom filtering.

Proposed Solution

Introduce a new scope parameter in relation attributes that accepts a class implementing a scope interface. This class would allow developers to define complex query conditions that can be reused across different relations.

Example Usage

use Cycle\Annotated\Annotation\Relation\HasOne;
use Cycle\Annotated\Annotation\Entity; 
use App\Entity\LatestAddressScope;

#[Entity] 
class User 
{ 
    #[HasOne(target: Address::class, scope: LatestNotEmptyAddressScope::class)] 
    public ?Address $address; 
}

Scope Class Example

use Cycle\ORM\Select;

class LatestAddressScope implements Select\ScopeInterface
{  
    public function apply(Select\QueryBuilder $query): void
    { 
        $query->where('address', '!=', '')->orderBy('created_at', 'DESC'); 
    }
}

Benefits

  • Allows for more flexible and powerful relationship definitions
  • Improves code reusability by separating query logic into dedicated classes
  • Enhances readability and maintainability of entity definitions
  • Provides a consistent way to apply complex conditions across different types of relationships

Supported Relations

This feature would be applicable to the following relation types:

  • HasOne
  • HasMany
  • ManyToMany
  • Morphed

I believe this feature would greatly enhance the flexibility and power of CycleORM's relationship handling, making it easier for developers to implement complex data models and queries.

@butschster butschster added the type:feature New feature. label Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature.
Projects
Status: No status
Development

No branches or pull requests

1 participant