Your IP : 216.73.217.87


Current Path : /hdd/hdd11/www/codedit/models/
Upload File :
Current File : /hdd/hdd11/www/codedit/models/BlogComments.php

<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;

class BlogComments extends \yii\db\ActiveRecord {

    public static function tableName() {
        return '{{%blog_comments}}';
    }

    public function behaviors() {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'date_added',
                'updatedAtAttribute' => NULL,
            ],
        ];
    }

    public function rules() {
        return [
            [['article_id', 'name', 'email', 'comment'], 'required'],
            [['article_id', 'date_added'], 'integer'],
            [['comment'], 'string'],
            [['email'], 'email']
        ];
    }

    public function attributeLabels() {
        return [
            'comment_id' => Yii::t('app', 'ID на коментара'),
            'article_id' => Yii::t('app', 'ID на статията'),
            'name' => Yii::t('app', 'Име'),
            'email' => Yii::t('app', 'Email адрес'),
            'comment' => Yii::t('app', 'Коментар'),
            'date_added' => Yii::t('app', 'Дата на добавяне'),
        ];
    }

    public function addComment($articleID) {
        $this->article_id = $articleID;
        return $this->save();
    }

    public function deleteComment($commentInfo) {
        return $commentInfo->delete();
    }

    public function getBlogComments($where = [], $limit = null) {
        $cacheKey = md5('view-comments-where-' . json_encode($where) . '-limit-' . json_encode($limit));
        $comments = Yii::$app->cache->get($cacheKey);
        if ($comments === false) {
            $comments = $this->find()->where($where)->limit($limit);
            Yii::$app->cache->set($cacheKey, $comments, 5 * 60);
        }
        return $comments;
    }

    public function getLatestBlogCommentsCount() {
        $blogCommentsCount = $this->find()->where('`date_added` > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 10 DAY))')->count();
        return $blogCommentsCount;
    }

    public function getBlogComment($commentID) {
        $comment = BlogComments::findOne($commentID);
        return $comment;
    }

    public function getArticle() {
        return $this->hasOne(BlogArticles::className(), ['article_id' => 'article_id']);
    }

}