Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
ogizanagi wants to merge1 commit intosymfony:2.7fromogizanagi:ticket_12573
Closed

Conversation

ogizanagi
Copy link
Contributor

QA
Bug fix?kind of
New feature?yes, but debatable
BC breaks?no
Deprecations?no
Tests pass?No
Fixed tickets#12573,#4087
LicenseMIT

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 theUniqueEntity 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 bothCustomer &Society entities, when trying to register aCustomer.

#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 acurrent value for therepository 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 becurrent.

Discuss:

  • For now, therepository 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

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
ContributorAuthor

Choose a reason for hiding this comment

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

Thanks 👍

@ogizanagi
Copy link
ContributorAuthor

Any news about this ? A single test was added.

@iltar : When working with repositories as services, I guess you use something similar tothis gist ?
So, your question wasn't about specifying a service id in the newrepository 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 theEntityManagerInterface::getRepository() method, which uses the repository factory.

Or do you create your repositories without using the factory ?

@linaori
Copy link
Contributor

@ogizanagi What it basically comes down to is this:

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

@ogizanagi
Copy link
ContributorAuthor

@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 therepository option (Except that it is more conform to the option name, but I think it should be renamed).

@stof
Copy link
Member

@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 useManagerRegistry::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).

…idatorThe 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.
@ogizanagiogizanagi deleted the ticket_12573 branchJuly 2, 2015 07:47
lemoinem added a commit to lemoinem/symfony that referenced this pull requestDec 11, 2015
lemoinem added a commit to lemoinem/symfony that referenced this pull requestDec 12, 2015
lemoinem added a commit to lemoinem/symfony that referenced this pull requestDec 16, 2015
lemoinem added a commit to lemoinem/symfony that referenced this pull requestFeb 2, 2016
lemoinem added a commit to lemoinem/symfony that referenced this pull requestFeb 2, 2016
lemoinem added a commit to lemoinem/symfony that referenced this pull requestFeb 2, 2016
lemoinem added a commit to lemoinem/symfony that referenced this pull requestFeb 2, 2016
lemoinem added a commit to lemoinem/symfony that referenced this pull requestFeb 3, 2016
fabpot added a commit that referenced this pull requestOct 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#7057This 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@ogizanagi@linaori@stof@jderusse@fabpot@webmozart

[8]ページ先頭

©2009-2025 Movatter.jp