Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Doctrine] [Bridge] Add a "repository" option to the uniqueEntity validator #12977

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

Closed
wants to merge 1 commit into from
Closed

[Doctrine] [Bridge] Add a "repository" option to the uniqueEntity validator #12977

wants to merge 1 commit into from

Conversation

ogizanagi
Copy link
Contributor

Q A
Bug fix? kind of
New feature? yes, but debatable
BC breaks? no
Deprecations? no
Tests pass? No
Fixed tickets #12573, #4087
License MIT

See #12573 for more explanations.

I made a pull request for the 2.7 branch, as pragmatically, this is a new feature. But IMO, it's questionable, as it is an easy-pick for older branches and solves what might be considered as a bug, or at least induces one. This could easily be merged into 2.3 without any bad behavior.

The new repository option expects an entity path, in order to select its repository.
This allows to use properly the UniqueEntity constraint on hierarchical (inheritance) entities, using the root class repository in order to check the uniqueness, and not just the child repository.

Given this inheritance scheme:

User
 ├─ Customer
 └─ Society

The code below is now sufficient in order order to check username uniqueness in both Customer & Society entities, when trying to register a Customer.

#ACME\UserBundle\Entity\User.php
/**
 * @ORM\Entity
 *
 * @UniqueEntity(fields="username", repository="ACME\UserBundle\Entity\User")
 *
 */
class User { ... }

Not specifiying the repository option will not cause any issue, and keep the same behavior as before.

Further:

The above case, IMO, might be the most used one, and it seems perhaps redondant to specify the current class as the repository to use for the constraint. Maybe we could imagine a current value for the repository option, auto-detecting the class on which the constraint is bound. (feasible ??)

Some other values could be considered in this way:

  • current: As described above, will detect the exact class on which the constraint was bound, and use its repository.
  • root: Will search for the top-level entity (User) and automatically use its repository.
  • real: the current behavior. Is the default value for the repository option (or null).

For 3.0, I think the default value should be current.

Discuss:

  • For now, the repository option name is disputable, as it expects an entity path in order to load its repository, and not the repository itself. Should it be renamed ?
  • If I define my Repositories as service, would I be able to somehow define a service for this as well ? (suggested by @ITAR)

@linaori
Copy link
Contributor

linaori commented Dec 15, 2014

I define my Repositories as service, would I be able to somehow define a service for this as well? I think that would complete this feature.

@@ -113,7 +113,27 @@ public function validate($entity, Constraint $constraint)
}
}

$repository = $em->getRepository(get_class($entity));
if(null != $constraint->repository) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null !== $constraint->repository

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@ogizanagi
Copy link
Contributor Author

Any news about this ? A single test was added.

@iltar : When working with repositories as services, I guess you use something similar to this gist ?
So, your question wasn't about specifying a service id in the new repository option, but just knowing if this implementation will be able to retrieve your repository from the entity class name, right ?
I don't see any issue, as we use the EntityManagerInterface::getRepository() method, which uses the repository factory.

Or do you create your repositories without using the factory ?

@linaori
Copy link
Contributor

linaori commented May 28, 2015

@ogizanagi What it basically comes down to is this:

services:
    app.user.repository:
        class: App\Repository\UserRepository
        factory: [@doctrine.orm.entity_manager, getRepository]
        arguments:
            - App:User 

@ogizanagi
Copy link
Contributor Author

@iltar : Then the entityManager and factory will be able to retrieve the repository through the entity class name. I don't think there is any benefit specifying the repository service id in the repository option (Except that it is more conform to the option name, but I think it should be renamed).

@stof
Copy link
Member

stof commented May 28, 2015

@iltar the repository retrieved from the EntityManager will still be the same instance than the one in your container.

Btw, be careful when registering your repositories as services: it means you cannot use ManagerRegistry::resetManager anymore (like when referencing the entity manager directly in your services) because the repository service will not be reset to be recreated later with the new Repository instance using the new EntityManager instance (and services in which it was injected won't receive the new instance even if you reset the repository in the container). This might be an issue in long-running processes (queue consumers for instance).

…idator

The repository option expects an entity path, in order to select its repository.
This allows to use properly the UniqueEntity constraint on hierarchical (inheritance) entities, using the root class repository in order to check the uniqueness, and not just the child repository.
@ogizanagi ogizanagi closed this Jun 16, 2015
@ogizanagi ogizanagi deleted the ticket_12573 branch July 2, 2015 07:47
lemoinem added a commit to lemoinem/symfony that referenced this pull request Dec 11, 2015
lemoinem added a commit to lemoinem/symfony that referenced this pull request Dec 12, 2015
lemoinem added a commit to lemoinem/symfony that referenced this pull request Dec 16, 2015
lemoinem added a commit to lemoinem/symfony that referenced this pull request Feb 2, 2016
lemoinem added a commit to lemoinem/symfony that referenced this pull request Feb 2, 2016
lemoinem added a commit to lemoinem/symfony that referenced this pull request Feb 2, 2016
lemoinem added a commit to lemoinem/symfony that referenced this pull request Feb 2, 2016
lemoinem added a commit to lemoinem/symfony that referenced this pull request Feb 3, 2016
fabpot added a commit that referenced this pull request Oct 14, 2016
…ed by the UniqueEntity validator (ogizanagi)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[DoctrineBridge] Add a way to select the repository used by the UniqueEntity validator

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12573, #4087, #12977
| License       | MIT
| Doc PR        | symfony/symfony-docs#7057

This is a cherry pick of #12977 on ~~2.8~~ 3.2 branch, as it is clearly a new feature, even if it was primary introduced in order to fix an inappropriate behavior that might be considered as a bug.

Commits
-------

00d5459 [Doctrine] [Bridge] Add a way to select the repository used by the UniqueEntity validator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants