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

INSTANCE OF support in Criteria API Expressions #5908

Open
skornev opened this issue Jun 30, 2016 · 27 comments
Open

INSTANCE OF support in Criteria API Expressions #5908

skornev opened this issue Jun 30, 2016 · 27 comments

Comments

@skornev
Copy link

skornev commented Jun 30, 2016

You can you use "Instance" of in DQL and QB. Maybe I missed something but it looks like there is no way to use it in Expression?

@Ocramius
Copy link
Member

@skornev ->andWhere('a INSTANCEOF B')

@Ocramius Ocramius self-assigned this Jun 30, 2016
@skornev
Copy link
Author

skornev commented Jun 30, 2016

It does not work.

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\Criteria::andWhere() must implement interface Doctrine\Common\Collections\Expr\Expression, string given

@skornev
Copy link
Author

skornev commented Jun 30, 2016

I want to use
Criteria::create()
->where(Criteria::expr()->eq("isFolder", "1"))->andWhere('a INSTANCEOF OF B)

@Ocramius
Copy link
Member

Ocramius commented Jul 1, 2016

Ah, I thought this was about DQL only (and the DQL expression builder), not about the criteria API, sorry. Re-opening and renaming issue.

@Ocramius Ocramius reopened this Jul 1, 2016
@Ocramius Ocramius removed their assignment Jul 1, 2016
@Ocramius Ocramius changed the title INSTANCE OF in Expression INSTANCE OF support in Criteria API Expressions Jul 1, 2016
@SInCE
Copy link

SInCE commented Jul 6, 2016

+1 for an instanceOf() Expression in the Criteria API.

@JeromeGill
Copy link

Yes please!

@massimilianobraglia
Copy link

it would be awesome!

@greydnls
Copy link

greydnls commented Sep 7, 2016

👍 Yes please.

@imunhatep
Copy link

+1

3 similar comments
@jonbev
Copy link

jonbev commented Dec 8, 2016

+1

@martinsifra
Copy link

+1

@dc-dstepanov
Copy link

+1

@trickeyone
Copy link

+1 Definitely! 👍

@tvequaud
Copy link

tvequaud commented Mar 7, 2017

+1 (and nothing since june 2016 ???)

@Ocramius
Copy link
Member

Ocramius commented Mar 7, 2017 via email

@pixels-farm
Copy link

So sad....

@courtney-miles
Copy link

+1

@theofidry
Copy link

The interested has been clearly shown already so stop +1 and do a PR if you're interested...

@courtney-miles
Copy link

so stop +1

Apologies -- my +1 was not intended to aggravate anyone.

Some projects embrace +1 as a way to gauge interest in features in the absence of voting.

@imunhatep
Copy link

@theofidry I made a brief research on this issue. "INSTANCE OF" expression is based on Entity annotations and QueryBuilder, which resolves expressions to DQL. But Criteria expressions resolved to raw SQL. As I understand, Criteria expression resolver (BAsicEntityPersister) should be rewritten to use QueryBuilder. That would be a huge PR and I doubt that it will pass CR by Doctrine team.

@theofidry
Copy link

@imunhatep thanks for looking into it. I guess then it's up to the core team to determine if this can be done in 3.x in which case it can be added to the 3.0 milestone or if this feature won't be implemented

@imunhatep
Copy link

imunhatep commented Jun 2, 2017

I were thinking a little bit on this problem and probably i was wrong. There is no need to rewite Context expression resolver to use QueryBuilder it would be overkill. A simplier approach would be just add a similar logic for resolving INSTANCE OF expression from QueryBuilder to Context expression resolver. Or to not duplicate business logic.. Move INSTANCE OF expression resolving logic to a separate handler. And add filtering for collections. It's still not an easy task.. But at least possible.

@spackmat
Copy link

Hey, two years have gone since the last post. Any news about this issue?

I stumbled upon the same problem as described in doctrine/collections#113 and didn't find another smart solution: Cannot use the discriminator column directly ("Unrecognized field"), cannot use the specific properties of the subclasses ("Unrecognized field" as they are ManyToOne-relations which isn't supported by the Criteria), cannot use instanceOf, cannot hydrate the whole Collection to filter it (it can be very huge in my case as it is an activity log).

How did others solve that? For now I avoid the Collection and have a repository method to get the count of log entries of a specific type. This is suboptimal, because all my other entities can handle their isDeletable() state on their own counting their restricting relations.

@trickeyone
Copy link

@spackmat Unfortunately, v1.5 implemented strict checking which makes my solution from #113 not work any longer. I submitted #168 which has been added to 2.0 milestone. Hopefully we'll get a fix soon. For my projects, I've had to lock them at 1.4 to keep this functionality.

@spackmat
Copy link

I did a dirty workaround in my case, since I only wanted to enable a user to be deleted, if it only has activities editing a User entity (itself in most cases) and preventing deletion if it has activities of any other type. So when there are less than 20 activities, I hydrate them all with a filter() and when there are 20 or more, I assume that there will be at least one of another type and the user cannot be deleted. So if one creates a new user, logs in as this user and edits this user 20 times, it is incorrectly considered undeletable and thus can only be hidden. I can live with that.

@SenseException
Copy link
Member

Of course Pull Requests are always welcome and your time and effort is appreciated.

@shmaltorhbooks
Copy link

any news there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests