<?php

namespace app\models\base;

use Yii;

/**
 * This is the model class for table "story_relation".
 *
 * @property int $news_id
 * @property int $topic_id
 * @property string $type
 */
class StoryRelation extends BaseFrontendClass
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'story_relation';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['news_id', 'topic_id'], 'integer'],
            [['type'], 'required'],
            [['type'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'news_id' => 'News ID',
            'topic_id' => 'Topic ID',
            'type' => 'Type',
        ];
    }

    /**
     * {@inheritdoc}
     * @return StoryRelationQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new StoryRelationQuery(get_called_class());
    }

	/**
     * {@inheritdoc}
     * @return status
     */
    public function saveNewsStory($id, $topics)
    {
		$status = true;
		foreach( $topics as $item ){
			$status &= $this->db->createCommand()->insert(self::tableName(), [
				'news_id' => $id,
				'topic_id' => $item,
			])->execute();
		}
        return $status;
    }

}
