/** * Attach headword. * @param string|Headword $headword * @return \static */ public function headword($headword) { if ($headword instanceof Headword) { return $this->andWhere(['headword_guid' => $headword->guid]); } if (is_string($headword)) { $headword = Headword::find()->where(['word' => $headword])->one(); if (!$headword) { return $this; } return $this->andWhere(['headword_guid' => $headword->guid]); } }
/** * Headwords generator. * @param string|\rhoone\extension\Extension|\rhoone\models\Extension|mixed $class * `string` if extension class. * @param string[] $words * @throws InvalidParamException */ public function getHeadwords($class = null, $words = []) { // Method One: if ($class === null || $class === false || is_string($class) && empty($class)) { $query = Headword::find(); } else { $extensions = ExtensionManager::getModels($class); $guids = []; foreach ($extensions as $extension) { $guids[] = $extension->guid; } $query = Headword::find()->guid($guids); } if (is_array($words) && !empty($words)) { $query = $query->word($words); } foreach ($query->all() as $headword) { (yield $headword); } // Method Two: //return Headword::find()->all(); }
/** * * @param string $word * @param Extension $extension * @return boolean */ public static function remove($word, $extension) { $headword = Headword::find()->where(['word' => $word, 'extension_guid' => $extension->guid])->one(); if (!$headword) { Yii::warning($word . ' does not exist.', __METHOD__); return false; } return $headword->delete() == 1; }
/** * @return \yii\db\ActiveQuery */ public function getHeadword() { return $this->hasOne(Headword::className(), ['guid' => 'headword_guid']); }
/** * * @return int */ public function removeAllHeadwords() { return Headword::deleteAll(['extension_guid' => $this->guid]); }