【问题标题】:Adding an attribute to yii2 active record model that is not in database向不在数据库中的 yii2 活动记录模型添加属性
【发布时间】:2017-01-25 22:10:47
【问题描述】:

我有一个 mySQL 数据库,它有一个表 videos 和两列 start_timeend_time,格式为 2017-01-24 15:38:11.

我有一个扩展 \yii\db\ActiveRecord 的活动记录模型 Videos,我想添加一些不在数据库中的附加属性。

我正在尝试将时间戳拆分为单独的日期和时间,然后我可以在我的 gridview 小部件中显示为列。我通过直接在视图中将列值设置为类似这样的函数成功地做到了这一点......

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel'  => $searchModel,
    'columns' => 
    [
        [
        'class' => '\kartik\grid\DataColumn',
        'attribute' => 'videodate',
        'value' =>  function($model)
            {
                $datetime = new DateTime('start_time');
                $date = $datetime->format('Y-m-d');
                return $date;
            },
        ],  
    ],
    'responsive'=>true,
    'hover'=>true
]); ?>

但这并不理想,因为我希望能够在我的 videoSearch 模型中使用日期和时间作为属性进行过滤和排序。

根据我的研究,我已在我的 rules() 和 attributeLabel() 函数中将属性添加到 Video 模型中,但是我不确定如何设置该值。我最初的想法是像关系一样做......

public function getVideodate(){
    $datetime = new DateTime('start_time');
    $date = $datetime->format('Y-m-d');
    return $date;
}

但是,这不会产生预期的结果。我想如果我可以在模型中定义和设置它,那么将它添加到搜索模型应该很简单,因为我的临时解决方案(在视图中)使这显着复杂化。

我打算在他改进的 gridview 小部件上使用 kartik 的日期范围和时间过滤器。提前谢谢,请让我知道我还可以包括什么。

尼克

【问题讨论】:

    标签: php activerecord yii2


    【解决方案1】:

    在你的模型中定义虚拟变量

    class Video extends \yii\db\ActiveRecord {
    
        public $video_date; // defining virtual attribute
        public $video_time; // defining virtual attribute
    
        public function rules()
        {
            return [
                // other rules ...
                [['video_date', 'video_time'], 'safe'],
            ];
        }
    
        // this method is called after using Video::find()
        // you can set values for your virtual attributes here for example
        public function afterFind()
        {
            parent::afterFind();
    
            $datetime = new DateTime('start_time');
    
            $this->video_date = $datetime->format('Y-m-d');
            $this->video_time = $datetime->format('H:i:s');
        }
    
        // this method is called right before inserting record to DB
        // after calling save() on model
        public function beforeSave($insert)
        {
            if (parent::beforeSave($insert)) {
                if ($insert) {
                    // if new record is inserted into db
                } else {
                    // if existing record is updated
                    // you can use something like this 
                    // to prevent updating certain data
                    // $this->status = $this->oldAttributes['status'];
                }
    
                $this->start_time = $this->video_date . ' '. $this->video_time;
    
                return true;
            }
    
            return false;
        }
    
    }
    

    鉴于您随后使用 video_date / video_time 作为属性,您也可能希望将属性标签添加到模型中。

    【讨论】:

    • 感谢您的帮助和快速响应!现在我只遇到了字符串到对象转换和时间格式(datetime() 和 strtotime())的问题,但这些在网上都有很好的记录。如果您可以包含一些 cmets 或参考 afterFind() 和 beforeSave($insert) 函数是什么以及为未来的读者做什么,那将会很有帮助。
    • 不客气,好主意,我会编辑我的帖子并添加 cmets
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多