如何在Yii中使用yii \ base \ model:getAttributes()方法?

时间:2016-01-24 16:49:50

标签: php yii

使用getAttributes方法时出错:“在非对象上调用成员函数getAttributes()”。

现在,在我的控制器中:

$notifications = Notifications::return_new()->getAttributes();

var_dump($notifications);

在模型中

public static function return_new(){
return Notifications::find()->where(['is_seen' => 0])->all();   
}

现在,Yii文档说getAttribute()接受一个数组作为参数,所以我试过

$notifications = Notifications::return_new()->getAttributes('text'); 

但它仍然存在同样的错误。有帮助吗?

以下是模型

<?php

namespace frontend\models;

use Yii;

 */
class Notifications extends \yii\db\ActiveRecord
{

    public static function tableName()
    {
        return 'notifications';
    }

    public function rules()
    {
        return [
            [['created_on', 'user_id', 'text'], 'required'],
            [['user_id'], 'integer'],
            [['created_on'], 'safe'],
            [['text'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'created_on' => 'Created On',
            'user_id' => 'User ID',
            'text' => 'Text',
        ];
    }
    public static function count_new()
    {
    $new = Notifications::find()->where(['is_seen' => 0])->all();
    return count($new);
    }
    public static function return_new(){
    return Notifications::find()->where(['is_seen' => 0])->all();   
    }
    public function return_all(){
    return Notifications::find()->all();
    }
    public static function checkst(){
    return Notifications::find()->where(['id' => 3])->one();
    }

    public function return_by_date () {
        // write something here.
    }
}   

1 个答案:

答案 0 :(得分:1)

如果您使用libvlc_media_stats_t stats; if (libvlc_media_get_stats(vlcmedia, &stats)) { long p=libvlc_media_player_get_time(vlcmediaplayer); if (p) bitrate=stats.i_read_bytes/p; } ,您将获得一组模型,然后您应该参考

all()

否则你可以

 Notifications::return_new()[0]->getAttributes();

在这种情况下你可以使用

 public static function return_new(){
   return Notifications::find()->where(['is_seen' => 0])->one();   
  }