2016-01-24 29 views
1

使用getAttributes方法時出現錯誤:「調用非成員函數的getAttributes()」。現在如何在Yii中使用yii base model: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. 
    } 
} 
+0

節目還您通知模型代碼..please .. – scaisEdge

+0

我已經發布了答案.. – scaisEdge

回答

1

如果使用all()你獲得的模型集合,然後你應該參考,以

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

否則,您可以

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

在這種情況下,您可以使用

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

的另一部分,我認爲社會上也需要這個答案,因爲Yii的文檔不解釋它。 :)也可能是問題;)? –

+0

是......是與參考文檔相關的常見問題......並詢問是否合理.. – scaisEdge