// +---------------------------------------------------------------------- namespace app\services\product\label; use app\dao\product\label\StoreProductLabelAuxiliaryDao; use app\services\BaseServices; /** * 标签辅助表 * Class StoreProductLabelAuxiliaryServices * @package app\services\product\label * @mixin StoreProductLabelAuxiliaryDao */ class StoreProductLabelAuxiliaryServices extends BaseServices { /** * StoreProductLabelAuxiliaryServices constructor. * @param StoreProductLabelAuxiliaryDao $dao */ public function __construct(StoreProductLabelAuxiliaryDao $dao) { $this->dao = $dao; } /** * 关联标签 * @param int $productId * @param array $labelIds * @return bool */ public function saveLabelRelation(int $productId, array $labelIds) { $data = []; foreach ($labelIds as $labelId) { $data[] = [ 'product_id' => $productId, 'label_id' => $labelId ]; } $this->dao->delete(['product_id' => $productId]); if ($labelIds) { $this->dao->saveAll($data); } return true; } }