T
T
thisall2022-02-18 18:55:18
Yii
thisall, 2022-02-18 18:55:18

How to properly build a query in Yii2?

I have a query and I want to remake it in a query builder so that I don't send a direct query and I don't understand how to build it

CREATE UNIQUE INDEX tag_unique ON tags (entity_id, entity_type, alias_id)
WHERE deleted_at IS NULL;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vilinyh, 2022-02-18
@vilinyh

A regular index can be created via:

(new Query()) // or Yii::$app->db (depends on your needs)
    ->createCommand()
    ->createIndex('tag_unique', 'tags', ['entity_id', 'entity_type', 'alias_id'], true)
    ->execute();

For migrations use yii\db\Migration::createIndex()
But partial indexes (where predicate) are not supported in yii2 and will not be ( https://github.com/yiisoft/yii2/issues/9799 ).
Use plain SQL, or extend the basic functionality if you really want to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question