示例#1
0
 /**
  * @param \Mirasvit\Blog\Model\Post $post
  * @return $this
  */
 protected function saveProducts($post)
 {
     $table = $this->getTable('mst_blog_post_product');
     if (!$post->hasProductIds()) {
         return $this;
     }
     $productIds = $post->getProductIds();
     $oldProductIds = $this->getProductIds($post);
     $insert = array_diff($productIds, $oldProductIds);
     $delete = array_diff($oldProductIds, $productIds);
     $connection = $this->getConnection();
     if (!empty($insert)) {
         $data = [];
         foreach ($insert as $productId) {
             if (empty($productId)) {
                 continue;
             }
             $data[] = ['product_id' => (int) $productId, 'post_id' => (int) $post->getId()];
         }
         if ($data) {
             $connection->insertMultiple($table, $data);
         }
     }
     if (!empty($delete)) {
         foreach ($delete as $productId) {
             $where = ['post_id = ?' => (int) $post->getId(), 'product_id = ?' => (int) $productId];
             $connection->delete($table, $where);
         }
     }
     return $this;
 }