public function addTerm($object_id, $params)
 {
     foreach ($params as $item => $value) {
         $term = TaxonomyTerms::findOne(['term' => $item, 'taxonomy_id' => $this->id]);
         if (!isset($term)) {
             $term = new TaxonomyTerms();
             $term->taxonomy_id = $this->id;
             $term->term = $item;
             $term->total_count = 0;
             $term->save();
         }
         $data['term_id'] = $term->id;
         $data['object_id'] = $object_id;
         $query = new Query();
         if (!$query->from($this->getTable())->where($data)->exists($this->getDb())) {
             $transaction = $this->getDb()->beginTransaction();
             try {
                 $data['value'] = $value;
                 $this->getDb()->createCommand()->insert($this->getTable(), $data)->execute();
                 $term->updateCounters(['total_count' => 1]);
                 TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]);
                 $transaction->commit();
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         } elseif ($this->updateOnExist) {
             $this->getDb()->createCommand()->update($this->getTable(), ['value' => $value], $data)->execute();
         }
     }
 }
Beispiel #2
0
 public function getTaxonomyTerm($name, $create = true)
 {
     $term = TaxonomyTerms::findOne(['term' => $name, 'taxonomy_id' => $this->id]);
     if ($create and !isset($term)) {
         $term = new TaxonomyTerms();
         $term->taxonomy_id = $this->id;
         $term->term = $name;
         $term->total_count = 0;
         $term->save();
     }
     return $term;
 }