B
B
BonBon Slick2017-10-25 21:57:53
PostgreSQL
BonBon Slick, 2017-10-25 21:57:53

Complex under the request, how to compose it correctly?

DB driver: PostgreSQL

->join('region as region', function ($join) use ($localLang, $userDetails) {
                    $join->on('songs.restriction_id', '=', 'region.relation_restriction_id')
                        ->where('region.slug', 'ILIKE', '%global%')
                        ->where('songs.restriction_type', '=', 1)
                        ->orWhere('region.slug', 'ILIKE', '%' . $userDetails->region . '%')
                        ->where('songs.restriction_type', '=', 1)
                        ->orWhere('songs.restriction_type', '=', 2)
                        ->where('region.slug', '!ILIKE', 2);????
                })

In this query, I'm doing a join on the lock table by region. There is a sampling of music, in different regions it is prohibited. There are two types of blocking:
1 - music is only available in this region
2 - music is prohibited only in this region.
When a user makes a search, we return to him the music that is available globally + only in his region.
->where('region.slug', 'ILIKE', '%global%')
                        ->where('songs.restriction_type', '=', 1)
                        ->orWhere('region.slug', 'ILIKE', '%' . $userDetails->region . '%')
                        ->where('songs.restriction_type', '=', 1)

Here I filtered, took all the music that is available everywhere, as well as music that is available only in the region specified in the user settings.
It remains to exclude music that is not available for the user's region.
->orWhere('songs.restriction_type', '=', 2)
->where('region.slug', '!ILIKE', '%' . $userDetails->region . '%'); ?
->where('region.slug', 'NOT ILIKE', '%' . $userDetails->region . '%'); ?
->where('region.slug', 'NOT LIKE', '%' . $userDetails->region . '%'); ?
? // отфильтровать всю музыку которая запрещена для региона юзера

2nd type of blocking, music is not available in the user's region.
Regional lock table:
id | relation_restriction_id | lang | name | slug |  descr |  timestamps

relation_restriction_id is responsible for the relationship between categories for different languages. I.e:
row = 1 | 233 | en | Global | global-en | descritpion blah blah blah | timestamps 
row = 2 | 233 | ru | Всемирная | global-ru | описание                     | timestamps 
row = 3 | 234 | de | Hlobal | global-de | na nemezkom opisanie      | timestamps

The rest of the fields I think are not needed in the explanation.
While giving errors like:
"SQLSTATE[0A000]: Feature not supported: 7 ERROR:  FOR SHARE is not allowed with GROUP BY clause (SQL: select....

This error is apparently due to the fact that it is used SELECT ... FOR UPDATEor ->sharedLock()which must also be used in the request. The search example is just an example.
I don’t know how to approach the request correctly now, can it be done on Laravel? Subqueries? Or what is the best and right way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2018-05-06
@BonBonSlick

When a user makes a search, we return to him the music that is available globally + only in his region.

  1. Request 1: Music that is available globally
  2. Request 2: music that is available in the region
  3. Query 3: left join query1 and query 2
  4. ???
  5. Profit!!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question