berlindb / core

All of the required core code
MIT License
254 stars 28 forks source link

Date Query Could Use More-Clear Documentation #67

Closed alexstandiford closed 4 years ago

alexstandiford commented 4 years ago

It seems like it's only possible to filter by a single date column. If I'm correct about that, it would be nice if Query could accept an array of date queries instead of a single query, or something like that.

Imagine you have a table, books with these columns:

There's currently no way to query against both date_created and date_published in BerlinDB.

I would expect that it would be possible to-do something like:


// Select books that were published after last Tuesday, and also only select the fields that were created since yesterday.
$query = new Book_Query( array(
    'date_query' => array(
        'relation' => 'AND',
        array(
            'field' => 'date_created',
           array(
                'after' => 'yesterday'
            )
        ),
        array(
            'field' => 'date_published',
           array(
                'after' => 'last tuesday'
            )
        )
    )
) );
ashleyfae commented 4 years ago

Have you tried this?

$query = new Book_Query( array(
    'date_created_query' => array(
        'after' => 'yesterday',
    ),
    'date_published_query' => array(
        'after' => 'last tuesday',
    )
) );
alexstandiford commented 4 years ago

Ah, yep, that works. This is what I get for posting something too soon. I could have sworn it was do-able, and I was scanning the query code, and never found it. Thanks @ashleyfae!

This isn't documented in the code anywhere - the only way you can really tell that it's do-able is from this block of code:

        // Possible dates
        $possible_dates = $this->get_columns( array( 'date_query' => true ), 'and', 'name' );
        foreach ( $possible_dates as $date ) {
            $key = "{$date}_query";
            $this->query_var_defaults[ $key ] = false;
        }

I'm going to at least submit a PR to clarify this in the docs.

alexstandiford commented 4 years ago

PR #68