statamic / eloquent-driver

Provides support for storing your Statamic data in a database, rather than flat files.
https://statamic.dev/tips/storing-content-in-a-database
MIT License
104 stars 73 forks source link

Join collection entries doesn't seem to work #132

Closed maxi032 closed 1 year ago

maxi032 commented 1 year ago

I am trying to join table entries with itself based on a few criterias. This is the query:

$query = EntryFacade::query()->leftJoin('entries as eTags', fn ($join) => $join
                ->whereColumn('eTags.id', 'entries.origin_id')
            )->select('entries.*','eTags.data as origin_data')
                ->where('entries.collection', 'news')
                ->where('entries.published', true)
                ->where('entries.origin_id', null)
                ->where('entries.site', $locale)
                ->limit($limit)->offset($offset);
        $entries = $query->get();
    I get 10 items as I have $limit set to 10 but the problem is that all the properties of those items are null.

If I run the query in a mysql client, I get the expected result.

This is the query:

 "query" => "select * from `entries` left join `entries` as `eTags` on `eTags`.`id` = `entries`.`origin_id` where `entries`.`collection` = ? and `entries`.`published` = ? and `entries`.`origin_id` is null and `entries`.`site` = ? limit 10 offset 0 ◀"
    "bindings" => array:3 [▼
      0 => "news"
      1 => true
      2 => "en"
    ]
    "time" => 1.15
    
I am using:

Environment

Cache

Drivers

Statamic

Statamic Addons

ryanmitchell commented 1 year ago

Are you trying to get the origin data for any entry? If so this PR should resolve that: https://github.com/statamic/eloquent-driver/pull/114