11

var_dump of $array returns this :

array (size=3)
  0 => 
    object(frontend\models\Notifications)[101]
      private '_attributes' (yii\db\BaseActiveRecord) => 
        array (size=5)
          'id' => int 1
          'created_on' => string '2015-11-12 12:12:15' (length=19)
          'user_id' => int 1
          'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
          'is_seen' => int 0
      private '_oldAttributes' (yii\db\BaseActiveRecord) => 
        array (size=5)
          'id' => int 1
          'created_on' => string '2015-11-12 12:12:15' (length=19)
          'user_id' => int 1
          'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
          'is_seen' => int 0
      private '_related' (yii\db\BaseActiveRecord) => 
        array (size=0)
          empty
      private '_errors' (yii\base\Model) => null
      private '_validators' (yii\base\Model) => null
      private '_scenario' (yii\base\Model) => string 'default' (length=7)
      private '_events' (yii\base\Component) => 
        array (size=0)
          empty
      private '_behaviors' (yii\base\Component) => 
        array (size=0)
          empty
  1 => 
    object(frontend\models\Notifications)[108]
      private '_attributes' (yii\db\BaseActiveRecord) => 
        array (size=5)
          'id' => int 2
          'created_on' => string '2015-11-12 12:12:15' (length=19)
          'user_id' => int 1
          'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
          'is_seen' => int 0
     ................................
     ................................
     ................................

But the json_encode($array) returns [{}, {}, {}]. What I attempted: Tried changing the character encoding of the whole database to utf8_general_ci.

The character encoding is utf8_general_ci for my table and so is for my 'text' column of the table. What could be the issue?

Ramesh Pareek
  • 1,601
  • 3
  • 30
  • 55

1 Answers1

18

The array you show has all the properties as private. this mean that this value are not available outside their class's scope.

you can look at this SO for some suggestion

Using json_encode on objects in PHP (regardless of scope)

Community
  • 1
  • 1
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • 1
    thanks, I realized that it was a yii model object. not an array. could you please explain how private properties can be converted into 'json_encodable' array? if you think this question is going to help community you may kindly consider an upvote :) – Ramesh Pareek Jan 24 '16 at 15:55
  • But seems you want access to an active record content.. for this there are a lot of function in Yii without accessing like a row object .. – ScaisEdge Jan 24 '16 at 15:57
  • yes the toArray method does it, but I wanted to write my own solution. this will also help me learn something new. Not looking for a complete solution, just some guidance on how to go about this. – Ramesh Pareek Jan 24 '16 at 15:59
  • Not only toArray .. Look at the related doc http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html all the private attribute ara available by proper functions.. _attributes by attributes(). or getAttribuets() _oldAttributes by getOldAttributes and so on .. – ScaisEdge Jan 24 '16 at 16:04
  • I'm trying to call this get attributes like this: but it's saying :Call to a member function getAttributes() on a non-object......$notifications = Notifications::return_new()->getAttributes();...... the return_new() method has the following code : return Notifications::find()->where(['is_seen' => 0])->all();.......please help how to use the getAttributes(), even Yii docs does not say anything about this – Ramesh Pareek Jan 24 '16 at 16:33
  • $this depend where you call .. yoy can try $model->getAttributes(); Anyway I suggest you of post a proper related question with an explanation of the you need and goal and the code you are using... for this .. – ScaisEdge Jan 24 '16 at 16:40
  • kindly see my new question here http://stackoverflow.com/questions/34978552/how-to-use-yii-base-model-getattributes-method-in-yii – Ramesh Pareek Jan 24 '16 at 16:50