public function add(Searchable $obj) { // Get Primary Key $attributes = $obj->getSearchAttributes(); $index = $this->getIndex(); $doc = new \ZendSearch\Lucene\Document(); // Add Meta Data fields foreach ($this->getMetaInfoArray($obj) as $fieldName => $fieldValue) { $doc->addField(\ZendSearch\Lucene\Document\Field::keyword($fieldName, $fieldValue)); } // Add provided search infos foreach ($attributes as $key => $val) { $doc->addField(\ZendSearch\Lucene\Document\Field::Text($key, $val, 'UTF-8')); } // Add comments - if record is content if ($obj instanceof ContentActiveRecord) { $comments = ""; foreach (Comment::findAll(['object_id' => $obj->getPrimaryKey(), 'object_model' => $obj->className()]) as $comment) { $comments .= " " . $comment->message; } $doc->addField(\ZendSearch\Lucene\Document\Field::Text('comments', $comments, 'UTF-8')); } if (\Yii::$app->request->isConsoleRequest) { print "."; } $index->addDocument($doc); $index->commit(); }
/** * On User delete, also delete all comments * * @param CEvent $event */ public static function onUserDelete($event) { foreach (Comment::findAll(array('created_by' => $event->sender->id)) as $comment) { $comment->delete(); } return true; }