Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. $this->new_recommended = is_array($this->new_recommended) ? $this->new_recommended : [];
  2.  
  3. $relation = $this->getProducts();
  4. $pivot = $relation->via->from[0];
  5.  
  6. $exists = (new Query())
  7. ->select(current($relation->link))
  8. ->from($pivot)
  9. ->where([key($relation->via->link) => $this->getPrimaryKey(), current($relation->link) => $this->new_recommended])
  10. ->indexBy(current($relation->link))
  11. ->all();
  12.  
  13. $this->getDb()
  14. ->createCommand()
  15. ->delete($pivot,
  16. ['and', key($relation->via->link) . '=:value', ['NOT IN', current($relation->link), array_keys($exists)]],
  17. [':value' => $this->getPrimaryKey()]
  18. )
  19. ->execute();
  20.  
  21. foreach ($this->new_recommended as $product) {
  22. if (!isset($exists[$product]))
  23. $rows[] = [$this->getPrimaryKey(), $product];
  24.  
  25. }
  26.  
  27. if (!empty($rows)) {
  28. $this->getDb()
  29. ->createCommand()
  30. ->batchInsert($pivot, [key($relation->via->link), current($relation->link)], $rows)
  31. ->execute();
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement