public function uninstall() { //drop the data table //$this->getDb()->createCommand()->dropTable($this->getTable())->execute(); //delete the term itself $model = TaxonomyDef::findOne($this->id); $model->delete(); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TaxonomyDef::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'total_count' => $this->total_count]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'class', $this->class])->andFilterWhere(['like', 'data_table', $this->data_table])->andFilterWhere(['like', 'ref_table', $this->ref_table]); return $dataProvider; }
public function removeTerm($object_id, $params = []) { $terms = $this->getTerms($object_id, isset($params['name']) ? $params['name'] : []); foreach ($terms as $term => $value) { $term = $this->getTaxonomyTerm($term); $data['term_id'] = $term->id; $data['object_id'] = $object_id; $query = new Query(); if ($query->from($this->getTable())->where($data)->exists($this->getDb())) { $this->getDb()->createCommand()->delete($this->getTable(), $data)->execute(); $term->updateCounters(['total_count' => -1]); TaxonomyDef::updateAllCounters(['total_count' => -1], ['id' => $this->id]); } } }
/** * Assigns terms to an object. * * @param integer $object_id * @param integer|array $params The ID/IDs of the term/terms that need to be assigned. Can be integer or array of integers. * @return An array with the currently assigned TaxonomyTerms. */ public function addTerm($object_id, $params) { $result = []; foreach (TaxonomyTerms::findAll((array) $params) as $term) { $data['term_id'] = $term->id; $data['object_id'] = $object_id; if (!(new Query())->from($this->table)->where($data)->exists(CategoryTerm::getDb())) { Yii::$app->db->transaction(function () use($data, $term, &$result) { CategoryTerm::getDb()->createCommand()->insert($this->table, $data)->execute(); $term->updateCounters(['total_count' => 1]); TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); $result[] = $term; }); } } return $result; }
/** * Add term/s with the ability to make hierarchies. * * The object_id can be skipped. In this case a hierarchy will be created without being attached to an object. * * $params can be a string or an array: * - If string, this is considered to be a root of a hierarchy; * - If array, if only filled with values, this means these are all roots of a hierarchy; * - If array and key => value is given, the key is the parent, the root is the child. * * @param integer $object_id Id to and object. Not mandatory. * @param string|array $params Terms */ public function addTerm($object_id, $params) { $cachedParents = []; $addTerm = function ($parent, $item) use($object_id, &$cachedParents) { if ($this->detectLoop($parent, $item)) { throw new InvalidCallException('Loop detected! Cannot add parent as a child!'); } $term = $this->getTaxonomyTerm($item); if (array_key_exists($parent, $cachedParents)) { $term->parent_id = $cachedParents[$parent]->id; } else { if (is_string($parent)) { $parentTerm = $this->getTaxonomyTerm($parent); $cachedParents[$parent] = $parentTerm; $term->parent_id = $parentTerm->id; } } if ($term->getDirtyAttributes(['parent_id'])) { $term->save(false); } if ($object_id) { $data['term_id'] = $term->id; $data['object_id'] = $object_id; if (!(new Query())->from($this->table)->where($data)->exists(CategoryTerm::getDb())) { Yii::$app->db->transaction(function () use($data, $term) { CategoryTerm::getDb()->createCommand()->insert($this->table, $data)->execute(); $term->updateCounters(['total_count' => 1]); TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); }); } } }; $params = (array) $params; foreach ($params as $parent => $item) { if (is_array($item)) { foreach ($item as $child) { $addTerm($parent, $child); } } else { $addTerm($parent, $item); } } }
public function addTerm($object_id, $params) { 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())) { $transaction = $this->getDb()->beginTransaction(); try { $this->getDb()->createCommand()->insert($this->table, $data)->execute(); $term->updateCounters(['total_count' => 1]); TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); } } } }
public function isInstalled() { return \Yii::$app->db->getTableSchema(TaxonomyDef::tableName(), true) !== null; }
public function down() { $this->dropTable(TaxonomyTerms::tableName()); $this->dropTable(TaxonomyDef::tableName()); }
/** * Finds the TaxonomyDef model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return TaxonomyDef the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = TaxonomyDef::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @depends testInstall */ public function testProperties() { $this->markTestSkipped('Not ready yet.'); $object_id = 3; //1. Add TAG taxonomy $term = new TaxonomyDef(); $term->name = 'test_property'; $term->class = PropertyTerm::className(); $term->data_table = 'sample_property'; $term->ref_table = SampleTable::className(); $this->tester->assertTrue($term->save()); //2. Create data table $this->tester->assertFalse($this->getTaxonomy()->getTerm($term->name)->isInstalled(), 'The term should NOT be installed.'); $this->getTaxonomy()->getTerm($term->name)->install(); $this->tester->assertTrue($this->getTaxonomy()->getTerm($term->name)->isInstalled(), 'The term should be installed.'); //3. Add some data $this->getTaxonomy()->addTerm($term->name, $object_id, ['prop1' => 'value1', 'prop2' => 'value2']); $term->refresh(); //check count on term $this->tester->assertEquals(2, $term->total_count, 'Tag term count not correct!'); //check update of field $this->getTaxonomy()->addTerm($term->name, $object_id, ['prop1' => 'value1_update']); $data = $this->getTaxonomy()->getTerms($term->name, $object_id); $this->tester->assertEquals('value1_update', $data['prop1'], 'Property value not updated'); $data = SampleTable::find()->hasTags()->all(); }
/** * @depends testInstall */ public function testProperties() { $object_id = 3; //1. Add TAG taxonomy $term = new TaxonomyDef(); $term->name = 'test_property'; $term->class = PropertyTerm::className(); $term->data_table = 'sample_property'; $term->ref_table = SampleTable::className(); $this->tester->assertTrue($term->save()); //2. Create data table $this->tester->assertFalse($this->getTaxonomy()->getTerm($term->name)->isInstalled(), 'The term should NOT be installed.'); $this->getTaxonomy()->getTerm($term->name)->install(); $this->tester->assertTrue($this->getTaxonomy()->getTerm($term->name)->isInstalled(), 'The term should be installed.'); //3. Add some data $this->getTaxonomy()->addTerm($term->name, $object_id, ['prop1' => 'value1', 'prop2' => 'value2']); $term->refresh(); //check count on term $this->tester->assertEquals(2, $term->total_count, 'Tag term count not correct!'); //check update of field $this->getTaxonomy()->addTerm($term->name, $object_id, ['prop1' => 'value1_update']); $data = $this->getTaxonomy()->getTerms($term->name, $object_id); $this->tester->assertEquals('value1_update', $data['prop1'], 'Property value not updated'); // 4. Test PropertyTerm::setTerms() $term = $this->getTaxonomy()->getTerm($term->name); $term->setTerms($object_id, ['prop1' => 'value1', 'prop3' => 'value3']); $data = $term->getTerms($object_id); $this->tester->assertTrue(array_key_exists('prop1', $data), 'Property prop1 is missing'); $this->tester->assertTrue(array_key_exists('prop3', $data), 'Property prop3 is missing'); $this->tester->assertFalse(array_key_exists('prop2', $data), 'Property prop2 must not be here'); // 5. Test PropertyTerm::removeTerm() $term->removeTerm($object_id, array_keys($data)); $data = $term->getTerms($object_id); $this->tester->assertEmpty($data, 'Data must be empty'); }
/** * @return \yii\db\ActiveQuery */ public function getTaxonomy() { return $this->hasOne(TaxonomyDef::className(), ['id' => 'taxonomy_id']); }