Skip to content

Example searching via relations attributes #11

@visualight

Description

@visualight

The structure of my database is as follows:
Table listings:

protected $ fillable = [
        'user_id',
        'category_id',
        'title',
        'slug',
        'description',
        'address',
        'number',
        'city',
        'latitude',
        'longitude',
        'phone',
        'E-mail',
        'website',
        'Facebook',
        'twitter',
        'Youtube',
        'has_timetable',
        'timetable',
        'cover',
        'gallery',
        'is_verified',
        'in_postmoderation',
        'is_featured',
        'status'
    ];

Table Keywords:

protected $ fillable = [
        'listing_id',
        'title',
        'slug'
    ];

I query the keywords table against the listing_id to find out if there are keywords for the listing table.

Suddenly my searchable results gives me more results than expected. 11 results for 4 real records in the listings table). Changing the join method does not change anything. If I apply distinct () in the SQL request, the elements are well filtered but the response for:

$ listings = $ results-> paginate (1); // test
$ listings-> lastPage (); // => results 11 (not 4).

I guess if I apply a groupBy it will solve the problem but goupBy seems not to want to work.

I have the following error if I apply as follows:

LISTING MODEL

protected function applySearchableJoins ($ query)
    {
        foreach ($ this-> searchableJoins () as $ table => $ join) {
            $ joinMethod = $ join [2] ?? 'leftJoin';
            $ query -> {$ joinMethod} ($ table, $ join [0], '=', $ join [1]) -> groupBy ('listings.id'); // id or other fields not work
        }
    }

ERROR
SQLSTATE [42000]: Syntax error or access violation: 1055 Expression # 29 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hybrisdev3.keywords.title' which is not functionally dependent on columns in GROUP BY

SQL
select listings. *, (LOCATE ('b', CONCAT (IFNULL ((listings.title), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), '' ), IFNULL ((keywords.title), ''), IFNULL ((CONCAT (users.firstname, "", users.lastname)), '')), 1) + LOCATE ('o', CONCAT (IFNULL ( (listings.title), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), ''), IFNULL ((keywords.title), ''), IFNULL ((CONCAT (users.firstname, "", users.lastname)), '')), 2) + LOCATE ('u', CONCAT (IFNULL ((listings.title), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), ''), IFNULL ((keywords.title), ''), IFNULL ((CONCAT (users.firstname, "", users.lastname)), '')) , 3) + LOCATE ('l', CONCAT (IFNULL ((listings.title), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), ''), IFNULL ( (keywords.title), ''), IFNULL ((CONCAT (users.firstname, "", users.lastname)), '')), 4) + LOCATE ('a', CONCAT (IFNULL ((listings.title ), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), ''), IFNULL ((keywords.title), ''), IFNULL ( (CONCAT (users.firstname, "", users.lastname)), '')), 5) + LOCATE ('n', CONCAT (IFNULL ((listings.title), ''), IFNULL ((listings.description ), ''), IFNULL ((categories.title), ''), IFNULL ((keywords.title), ''), IFNULL ((CONCAT (users.firstname, "", users.lastname)), '' ))), 6) + LOCATE ('g', CONCAT (IFNULL ((listings.title), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), ''), IFNULL ((keywords.title), ''), IFNULL ((CONCAT (users.firstname, "", users.lastname)), '')), 7) + LOCATE ('e', CONCAT (IFNULL ((listings .title), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), ''), IFNULL ((keywords.title), ''), IFNULL ((CONCAT (users .firstname, "", users.lastname)), '')), 8) + LOCATE ('r', CONCAT (IFNULL ((listings.title), ''), IFNULL ((listings.description), '' ), IFNULL ((categories.title), ''), IFNULL ((keywords.title), ''), IFNULL ((CONCAT (users.firstname, "", users.lastname)), '')), 9 ) + LOCATE ('i', CONCAT (IFNULL ((listings.title), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), ''), I FNULL ((keywords.title), ''), IFNULL ((CONCAT (users.firstname, "", users.lastname)), '')), 10) + LOCATE ('e', CONCAT (IFNULL ((listings .title), ''), IFNULL ((listings.description), ''), IFNULL ((categories.title), ''), IFNULL ((keywords.title), ''), IFNULL ((CONCAT (users .firstname, "", users.lastname)), '')), 11)) AS sort_index from listings left join users on users.id = listings.user_id left join categories on categories.id = listings.category_id left join keywords on keywords.listing_id = listings.id where listings.status =? and (listings.title like "% boulangerie%" OR listings.description like "% boulangerie%" OR categories.title like "% boulangerie%" OR keywords.title like "% boulangerie%" OR CONCAT (users.firstname, "" , users.lastname) like "% boulangerie%") and listings.deleted_at is null group by listings.id, listings.id, listings.id order by sort_index asc

LISTING MODEL

protected $searchable = [
        'columns' => [
            'listings.title',
            'listings.description',
            'categories.title',
            'keywords.title',
            'author_full_name' => 'CONCAT(users.firstname, " ", users.lastname)'
        ],
        'joins' => [
            'users' => ['users.id', 'listings.user_id'],
            'categories' => ['categories.id', 'listings.category_id'],
            'keywords' => ['keywords.listing_id', 'listings.id']
        ]
    ];

How I can get only 4 record s as expected without a groupBy method OR how i can apply the groupBy on the search query ? Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions