Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $this->new_recommended = is_array($this->new_recommended) ? $this->new_recommended : [];
- $relation = $this->getProducts();
- $pivot = $relation->via->from[0];
- $exists = (new Query())
- ->select(current($relation->link))
- ->from($pivot)
- ->where([key($relation->via->link) => $this->getPrimaryKey(), current($relation->link) => $this->new_recommended])
- ->indexBy(current($relation->link))
- ->all();
- $this->getDb()
- ->createCommand()
- ->delete($pivot,
- ['and', key($relation->via->link) . '=:value', ['NOT IN', current($relation->link), array_keys($exists)]],
- [':value' => $this->getPrimaryKey()]
- )
- ->execute();
- foreach ($this->new_recommended as $product) {
- if (!isset($exists[$product]))
- $rows[] = [$this->getPrimaryKey(), $product];
- }
- if (!empty($rows)) {
- $this->getDb()
- ->createCommand()
- ->batchInsert($pivot, [key($relation->via->link), current($relation->link)], $rows)
- ->execute();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement