public static function setTags($entryId, $tags) { if (!$tags) { return; } // Write new tags to Tag table try { foreach ($tags as $k => $t) { // Add new tag if id is not exist if (!isset($t['id'])) { $tag = new Tag(); $tag->text = $t['text']; $tag->save(); $tags[$k]['id'] = $tag->id; } } } catch (Exception $e) { return $e->getMessage(); } // Update tags for current entry //TODO: rewrite with junction table relations try { EntryTag::deleteAll(['entry_id' => (int) $entryId]); foreach ($tags as $t) { $e = new EntryTag(); $e->tag_id = $t['id']; $e->entry_id = $entryId; $e->save(); } } catch (Exception $e) { return $e->getMessage(); } }
public function actionGetentry() { $id = Yii::$app->request->get('id'); $tags = EntryTag::findAll(['entry_id' => $id]); $retTags = []; // TODO relations foreach ($tags as $k => $v) { $retTags[] = Tag::findOne($v['tag_id']); } Yii::$app->response->format = Response::FORMAT_JSON; return (object) ['data' => Entry::findOne($id), 'tags' => $retTags]; }