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

[Question] include null values for date filter #669

Closed
aledeg opened this issue Aug 5, 2016 · 16 comments
Closed

[Question] include null values for date filter #669

aledeg opened this issue Aug 5, 2016 · 16 comments

Comments

@aledeg
Copy link

aledeg commented Aug 5, 2016

I would like to always include null values while searching for dates.
It seems impossible for now. Correct me if I am wrong.

I tried to set the same field with different values but as expected, only one is used.

filter.subscription_date_filter:
    parent: "api_platform.doctrine.orm.date_filter"
    arguments:
        - validFrom: 'include_null_before'
          validFrom: 'include_null_after'
    tags: [ { name: 'api_platform.filter', id: 'filter.subscription_date' } ]

Did I miss something?

@dunglas
Copy link
Member

dunglas commented Aug 6, 2016

According to https://github.com/api-platform/docs/blob/master/core/filters.md#managing-null-values, it should work. You only need one line (before or after dpeending of the order you want).

Can you provide your entity mapping?

@aledeg
Copy link
Author

aledeg commented Aug 10, 2016

If I choose 'include_null_after', I have the NULL values when I filter the date with the after parameter. But I won't have a match with the before parameter.

If I choose 'include_null_before, I have the NULL values when I filter the date with the before parameter. But I won't have a match with the after parameter.

I check the code and it works as expected. But it's not what I am trying to achieve.
I would like to have NULL values displayed all the time whether I am filtering with before or after

Regarding the entity mapping, I think it's irrelevant since it's not related. I include it anyway.

namespace ApiBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiProperty;

/**
 * @ORM\Entity
 * @ORM\Table(name="contact")
 * @ApiResource(attributes={
 *     "filters"={
 *         "filter.contact_date"
 *     }
 * })
 */
class Contact
{

    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $birthDate;

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }
    public function setBirthDate(\DateTime $birthDate = null)
    {
        $this->birthDate = $birthDate;
    }

    public function getBirthDate()
    {
        return $this->birthDate;
    }

}

The filter configuration:

filter.contact_date_filter:
    parent: 'api_platform.doctrine.orm.date_filter'
    arguments:
        - birthDate: include_null_after
    tags:
        - { name: 'api_platform.filter', id: 'filter.contact_date' }

What I would like to do instead is something like:

filter.contact_date_filter:
    parent: 'api_platform.doctrine.orm.date_filter'
    arguments:
        - birthDate: include_null_always
    tags:
        - { name: 'api_platform.filter', id: 'filter.contact_date' }

or like:

filter.contact_date_filter:
    parent: 'api_platform.doctrine.orm.date_filter'
    arguments:
        - birthDate: [include_null_after, include_null_before]
    tags:
        - { name: 'api_platform.filter', id: 'filter.contact_date' }

@teohhanhui
Copy link
Contributor

@aledeg Can you perhaps add a Behat test case for this? 😄

@Simperfit
Copy link
Contributor

@aledeg I agree with @teohhanhui could you please add a test ?

@teohhanhui
Copy link
Contributor

I'm trying to understand this...

If you specify include_null_before, then NULL is treated as the smallest value (the beginning of time); if you specify include_null_after, then NULL is treated as the largest value (the end of time).

Or in other words: a NULL value cannot be both the smallest and the largest value at the same time.

@aledeg
Copy link
Author

aledeg commented Dec 15, 2016

Sorry guys, I didn't have time to reply. The thing is I don't want to consider null values as the smallest or the biggest values. I want to see them all the time.

I'll try to give an example of what I am trying to achieve.

@teohhanhui
Copy link
Contributor

I understand what you're trying to do, but I'm not convinced that it makes logical sense. Though I'm not against adding this feature...

@aledeg
Copy link
Author

aledeg commented Dec 23, 2016

I took api-platform/api-platform code and I added a dob nullable field in the Foo object.
I then wrote the following test:

Feature: Foo filter

    Scenario: Get foo list filtered by dob
        Given I add 'Content-Type' header equal to 'application/json'
        When I send a "POST" request to "/foos" with body:
        """
        {
            "bar": "bar 1",
            "dob": "2015-01-01"
        }
        """
        When I send a "POST" request to "/foos" with body:
        """
        {
            "bar": "bar 2",
            "dob": "2016-01-01"
        }
        """
        When I send a "POST" request to "/foos" with body:
        """
        {
            "bar": "bar 3"
        }
        """
        When I send a "GET" request to "/foos"
        Then the JSON node 'hydra:member' should have 3 elements
        When I send a "GET" request to "/foos?dob[before]=2015-10-10"
        Then the JSON node 'hydra:member' should have 2 elements
        When I send a "GET" request to "/foos?dob[after]=2015-10-10"
        Then the JSON node 'hydra:member' should have 2 elements

I tried all available options for my date filter and my tests are failing for all options. It's normal regarding how the filter is generated.

@teohhanhui
Copy link
Contributor

Yes, it is expected behaviour. You're welcome to send a Pull Request to add this option. Others might find it useful too...

@Simperfit
Copy link
Contributor

@aledeg do you want to add a PR ?

@aledeg
Copy link
Author

aledeg commented Jun 14, 2017

Sure but I need to find some time to do it :)

@aledeg
Copy link
Author

aledeg commented Oct 2, 2017

I've started to look into it. I have a question regarding the contributing process. I've read the documentation but it's no help.

I've looked for the behat tests to add/modify regarding that issue but I couldn't find anything. Is there something to add in those tests or I just need to write unit tests ?

@Simperfit
Copy link
Contributor

@aledeg You need to update the existing behat tests by adding a new Scenario and maybe a new fields on one of the tests entity, this is all located in the tests/Fixtures folder.

Do you want to do it ?

@aledeg
Copy link
Author

aledeg commented Apr 9, 2018

I'll try to find some time to do it.

@coolfarmer
Copy link

coolfarmer commented Dec 18, 2019

Hi @aledeg , do you finally find a solution? I'm trying to have NULL for my date but even with the filter I can't even have my null values.

/**
 * @ApiResource(normalizationContext={"groups"={"employee"}})
 * @ApiFilter(DateFilter::class, properties={"dateProperty": DateFilter::INCLUDE_NULL_BEFORE_AND_AFTER})
 * @ORM\Table(name="employee")
 */
class Employee
{
    // ...

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Groups({"employee"})
     */
    private $firedDate;

    public function getFiredDate(): ?\DateTimeInterface
    {
        return $this->firedDate;
    }

    // ...
}

I'm calling the url http://api.local:8785/employees/1.
Every time I just have no value, but I want to have null in my json instead of nothing.

Any help?

@teohhanhui
Copy link
Contributor

@coolfarmer Please use the #api-platform channel on Symfony Slack for support questions. Thank you!

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

5 participants