• Resolved jerrystewart99

    (@jerrystewart99)


    Hi,

    I’m trying to programmatically get a list of events between two dates. Actually between ‘now’ and two days time.
    After trying the sql route (which ended in tears, lost within with the tec custom tables logic), I thought I’d try and do it properly and after discovering the ORM API documentation I got quite excited. [https://docs.theeventscalendar.com/apis/orm/]

    The documents give some examples but not a complete list of keywords, syntax etc.

    I was expecting to find an and() function to combine logical expressions together. There is no such function.

    The best I’ve come up with so far is

    $events = tribe_events()
    ->where(‘start_date’, ‘>=’, ‘2023-02-14’)
    ->where(‘start_date’, ‘<=’, ‘2023-02-16’) ->pluck( ‘ID’ );

    Whatever combination of dates I use, I get 150 results which is clearly incorrect.

    Can anyone help get me going, or point me to some more extensive documentation

    Thanks in advance, Jerry

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter jerrystewart99

    (@jerrystewart99)

    Follow up: So, I’ve solved my initial problem but my question remains.. Where is the API defined?

    I’ve created a statement that seems to work by a mixture of looking at the examples given in the docs and a bit of trial and error.. as follows:

    $results = tribe_events()->where( ‘starts_after’, ‘now’ )
    ->where( ‘starts_before’, ‘+2 days’ )
    ->order_by( ‘event_date’ )
    ->order( ‘ASC’ )
    ->all();

    <rant>

    I guessed at the keyword ‘starts_before’. It seemed logical as there was a ‘starts_after’ in the documented example. Are there other useful keywords? where are they defined? I also guessed ‘now’ as it seemed logical.

    Why doesn’t where(‘end_date’, ‘<=’, ‘2023-02-16’) work?

    Why is it order_by( ‘event_date’ ) instead of order_by( ‘start_date’ ) which would seem more logical as start_date is a field in the returned ‘Event Object’ but event_date isn’t.

    When you chain where() clauses, it seems to default to a logical AND operation. What if you wanted a logical OR operation?

    So many questions! I’d really love to understand this API more as it is truly elegant and nice to use.

    </rant>

    Plugin Support Darian

    (@d0153)

    Hi @jerrystewart99

    Thanks for reaching out. I apologize for the late reply.

    I guessed at the keyword ‘starts_before’. It seemed logical as there was a ‘starts_after’ in the documented example. Are there other useful keywords? where are they defined? I also guessed ‘now’ as it seemed logical.

    You could try the following keywords found in the-events-calendar/src/Tribe/Repositories/Event.php line 147.

    Why doesn’t where(‘end_date’, ‘<=’, ‘2023-02-16’) work?

    You could try where('ends_on_or_before','2023-02-16')

    For your other questions, let me check with the team and I’ll get back to you on this once I have new information.

    Plugin Support Darian

    (@d0153)

    Hi @jerrystewart99

    Thanks for patiently waiting.

    The base repository where function is defined here:?https://github.com/the-events-calendar/tribe-common/blob/master/src/Tribe/Repository.php#L518

    Let me know if you have further questions or concerns.

    Thread Starter jerrystewart99

    (@jerrystewart99)

    Hi @d0153 Darian,

    That’s great, thanks. I can definitely work with that.

    • Jerry
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting started with the ORM API’ is closed to new replies.