Lookup/Enum table annotation #6022

Open
opened 2026-01-22 15:24:54 +01:00 by admin · 0 comments
Owner

Originally created by @xxRockOnxx on GitHub (Jul 21, 2018).

Originally assigned to: @Ocramius on GitHub.

Feature Request

Q A
New Feature yes
RFC not sure
BC Break not sure

Summary

There are times where fetching is really unnecessary. Good example are "Enum tables" or "Lookup table"

Imagine this flow:

Request -> Controller -> Service -> Repo

In your request you might have something like

['contacts' => ['type' => 'email', 'value' => 'sample@test.com']]

Where would it fit?

  • My controller transforms those arrays into entities to be passed on service.
  • My service layer is just business logic. It'll be really a pain to do something like ContactTypeRepo::find() especially when you have deep object.
  • My repo layer does CRUD operations only and no logic

Wouldn't it be a lot easier if we have an entity annotation that look like

class Contact
{
    /**
    * @ORM\ManyToOne(targetEntity="ContactType", lookup="true")
    */
    private $type;
}

You can then set it either the id or the entity itself (without coming from entityManager. Just:

$type = new ContactType(1, 'email'); // Manual entity creation instead of ContactTypeRepo::find(1)
$contact->setType($type);

then EntityManager will automatically get the reference for it? Because right now if you do that, the error would be

A new entity was found through the relationship 'App\Models\Contact#type'

Workaround

class Contact
{
    /**
    *  @ORM\Column(type="integer", name="type_id")
    */
    private $type;
}
// Called in controller before sending response
class ContactTransformer
{
    public function transform(Contact $contact)
    {
        if (is_int($contact->getType())) {
            $type = ContactType::getTypeInstance($contact->getType());
        } else {
            $type = $contact->getType();
        }

        return [
            'type' => (new TypeTransformer())->trasnform($type),
            'value' => $contact->getValue()
        ];
    }
}
class ContactType
{
    public const EMAIL = 1;

    public static function getTypeInstance(int $type)
    {
        $names = [
            static::EMAIL => 'Email address'
        ];

        return new static($type, $names[$type]);
    }
}

Thoughts?

Originally created by @xxRockOnxx on GitHub (Jul 21, 2018). Originally assigned to: @Ocramius on GitHub. ### Feature Request | Q | A |------------ | ------ | New Feature | yes | RFC | not sure | BC Break | not sure #### Summary There are times where fetching is really unnecessary. Good example are "Enum tables" or "Lookup table" Imagine this flow: Request -> Controller -> Service -> Repo In your request you might have something like ``` ['contacts' => ['type' => 'email', 'value' => 'sample@test.com']] ``` Where would it fit? - My controller transforms those arrays into entities to be passed on service. - My service layer is just business logic. It'll be really a pain to do something like `ContactTypeRepo::find()` especially when you have deep object. - My repo layer does CRUD operations only and no logic Wouldn't it be a lot easier if we have an entity annotation that look like ``` class Contact { /** * @ORM\ManyToOne(targetEntity="ContactType", lookup="true") */ private $type; } ``` You can then set it either the id or the entity itself (without coming from entityManager. Just: ``` $type = new ContactType(1, 'email'); // Manual entity creation instead of ContactTypeRepo::find(1) $contact->setType($type); ``` then EntityManager will automatically get the reference for it? Because right now if you do that, the error would be > A new entity was found through the relationship 'App\Models\Contact#type' #### Workaround ``` class Contact { /** * @ORM\Column(type="integer", name="type_id") */ private $type; } ``` ``` // Called in controller before sending response class ContactTransformer { public function transform(Contact $contact) { if (is_int($contact->getType())) { $type = ContactType::getTypeInstance($contact->getType()); } else { $type = $contact->getType(); } return [ 'type' => (new TypeTransformer())->trasnform($type), 'value' => $contact->getValue() ]; } } ``` ``` class ContactType { public const EMAIL = 1; public static function getTypeInstance(int $type) { $names = [ static::EMAIL => 'Email address' ]; return new static($type, $names[$type]); } } ``` Thoughts?
admin added the New FeatureWon't FixQuestion labels 2026-01-22 15:24:54 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6022