Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support relations via array type columns #362

Closed
Tigrov opened this issue Jun 9, 2024 · 4 comments
Closed

Support relations via array type columns #362

Tigrov opened this issue Jun 9, 2024 · 4 comments
Labels
type:feature New feature

Comments

@Tigrov
Copy link
Member

Tigrov commented Jun 9, 2024

Currently AR supports scalar to array relations:

$this->hasMany(Item::class, ['id' => 'item_ids'])

where id is integer column
and item_ids is integer array column

But AR does not support array to scalar relations
$this->hasMany(Promotion::class, ['item_ids' => 'id'])

The reason is in preparing the query condition. Currently it is only IN condition

$this->andWhere(['in', $attributes, $values]);

Which will generate condition like id IN (1, 2, 3)

But for array column this should be arrays overlap condition like item_ids && ARRAY[1, 2, 3] (Postgres)

To solve the issue it requires:

  1. Realize ArrayOverlapCondition and JsonOverlapCondition (for json arrays) in db packages
  2. Add AR::columnType($columnName) method to get type of the column
  3. Prepare condition according to the column type:
match ($columnType) {
    'array' => $this->andWhere(new ArrayOverlapCondition($attributes, $values)),
    'json' => $this->andWhere(new JsonOverlapCondition($attributes, $values)),
    default => $this->andWhere(['in', $attributes, $values]),
};
@samdark
Copy link
Member

samdark commented Jun 11, 2024

What's the support in other DBs?

@Tigrov
Copy link
Member Author

Tigrov commented Jun 12, 2024

What's the support in other DBs?

  • Pgsql, Mssql, Oracle support arrays but db package has realization only for Pgsql;
  • Mysql, Pgsql, Sqlite, Mssql, Oracle support json but db package has realization only for Mysql, Pgsql, Sqlite.
    But it may not be possible to implement JsonOverlapCondition for all DBMSs. At least for Mysql and Pgsql it is easy to implement.

@samdark
Copy link
Member

samdark commented Jun 12, 2024

If it's MySQL and PostgreSQL then it is worth doing.

@Tigrov
Copy link
Member Author

Tigrov commented Aug 17, 2024

Done with #375

@Tigrov Tigrov closed this as completed Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature
Projects
None yet
Development

No branches or pull requests

2 participants