Example #1
0
 /**
  * @param ArticleModel $article
  * @return $this
  */
 protected function saveStoreRelation(ArticleModel $article)
 {
     $oldStores = $this->lookupStoreIds($article->getId());
     $newStores = (array) $article->getStores();
     if (empty($newStores)) {
         $newStores = (array) $article->getStoreId();
     }
     $table = $this->getTable('gemtoo_blog_article_store');
     $insert = array_diff($newStores, $oldStores);
     $delete = array_diff($oldStores, $newStores);
     if ($delete) {
         $where = ['article_id = ?' => (int) $article->getId(), 'store_id IN (?)' => $delete];
         $this->getConnection()->delete($table, $where);
     }
     if ($insert) {
         $data = [];
         foreach ($insert as $storeId) {
             $data[] = ['article_id' => (int) $article->getId(), 'store_id' => (int) $storeId];
         }
         $this->getConnection()->insertMultiple($table, $data);
     }
     return $this;
 }