Skip to content

Loading…

DDC-819: Attempting to search a recursive relationship by null results in an error... #5337

Closed
doctrinebot opened this Issue · 10 comments

2 participants

@doctrinebot

Jira issue originally created by user omega:

I have an entity which has a many to one relationship pointing to itself. This is ultimately set to map parent/child relationships.

Consider the following snippet:

    // Start out assuming we are searching for "Pages" with no parent specified (root level).
    $parentId = null;

    // Check to see if we're fetching pages that are a child of a parent.
    if(array*key_exists("parent_id", $*GET)) {
        $parentId = $*GET['parent*id'];
    }

    $pages = $this->entityManager->getRepository("MyNamespace\Models\Page")->findByParent($parentId);

When this code is run, PHP tells me:

PHP Fatal error:  Uncaught exception 'Doctrine\\ORM\\ORMException' with message 'You need to pass a parameter to 'findByParent''

At which point, it's obvious that it's expecting a parameter an receiving none. The problem here however is that I have no way of matching for null. Is there some kind of surrogate null php-class that can be created to represent a model-null?

@doctrinebot

Comment created by @beberlei:

This is a duplicate of DDC-817, querying by associated ids inside the repositories is currently not possible. You have to use DQL for it.

@doctrinebot

Comment created by omega:

While that is relevant to this issue, the problem I outlined here isn't just to do with being able to query an association by ID. It's about how I want to match for entities that have null specified.

This problem would arise with any regular field. Note: the error that I'm getting "you need to pass a parameter to findByParent". That's a PHP error, not Doctrine2. Which means the method isn't even getting a chance to run.

I understand that currently it won't work once it does run, but there has to be some way to test for null...

@doctrinebot

Comment created by @beberlei:

you are right, there is another bug :)

@doctrinebot

Comment created by @beberlei:

Fixed!

@doctrinebot

Comment created by omega:

Unrelated to the fix, split this off if necessary: Looking at the other issue you linked in, it appears as though the column that stores the actual foreign key will be forced into becoming a primary key. Which is not really optimal. Is there any way to do this without creating another primary key on the entity?

@doctrinebot

Comment created by @beberlei:

err i just realized that this fix is bullshit.

the query turns into WHERE foo = ''.

There is no conversion into IS NULL. This case can only be solved by DQL.

@doctrinebot

Comment created by @beberlei:

I reverted this patch in the fix for DDC-817

@doctrinebot

Issue was closed with resolution "Won't Fix"

@doctrinebot

Comment created by @beberlei:

@Alexander: I don't get your foreign key to primary key force question, there is no such requirement.

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.0-RC1 milestone
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.