Example #1
0
 public function removeTerm($object_id, $params = [])
 {
     if (empty($params)) {
         $params = $this->getTerms($object_id);
     }
     foreach ($params as $item) {
         $term = $this->getTaxonomyTerm($item);
         $data['term_id'] = $term->id;
         $data['object_id'] = $object_id;
         $query = new Query();
         if ($query->from($this->table)->where($data)->exists($this->getDb())) {
             $this->getDb()->createCommand()->delete($this->table, $data)->execute();
             $term->updateCounters(['total_count' => -1]);
             Taxonomydef::updateAllCounters(['total_count' => -1], ['id' => $this->id]);
         }
     }
 }
 /**
  * Removes terms from an object.
  *
  * @param integer $object_id The id of the object.
  * @param array|integer $params An array of term IDs or term ID.
  * @return An array with the TaxonomyTerms objects that were removed.
  */
 public function removeTerm($object_id, $params = [])
 {
     $result = [];
     foreach (TaxonomyTerms::findAll((array) $params) as $term) {
         $data['term_id'] = $term->id;
         $data['object_id'] = $object_id;
         $query = new Query();
         if ($query->from($this->table)->where($data)->exists($this->getDb())) {
             Yii::$app->db->transaction(function () use($data, $term, &$result) {
                 $this->getDb()->createCommand()->delete($this->table, $data)->execute();
                 $term->updateCounters(['total_count' => -1]);
                 Taxonomydef::updateAllCounters(['total_count' => -1], ['id' => $this->id]);
                 $result[] = $term;
             });
         }
     }
     return $result;
 }