There is a database called u13461 (it contains a table of results and 3 fields id , date , result ) I create a model.results file.php (In the models folder) I am writing here:

 namespace app\models; use yii\db\ActiveRecord; class results extends ActiveRecord { const STATUS_INACTIVE = 0; const STATUS_ACTIVE = 1; /** * @return string название таблицы, сопоставленной с этим ActiveRecord-классом. */ public static function tableName() { return 'result'; } } 

Next, in the SiteController controller , I write:

 use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\EntryForm; use app\models\results; class SiteController extends Controller { public function actionIndex() { $query = results::find()->all(); return $this->render('index', [ 'query' => $query ]); } } 

In the views file index.php I write:

 echo "<pre>"; print_r($query); echo "</pre>"; 

As a result, in addition to the data from the database, I get another bunch of data in an incomprehensible structure and the data from the database are duplicated. Why?

Here is what shows:

 Array ( [0] => app\models\results Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 6 [date] => [result] => 0 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 6 [date] => [result] => 0 ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) ) [1] => app\models\results Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 6 [date] => [result] => 0 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 6 [date] => [result] => 0 ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) ) [2] => app\models\results Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 6 [date] => [result] => 0 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 6 [date] => [result] => 0 ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) ) [3] => app\models\results Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 6 [date] => [result] => 0 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 6 [date] => [result] => 0 ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) ) ) 
  • What does echo $ query ['id'] display? - Urmuz Tagizade
  • Displays just 1 - Fitstd
  • Try perezalit folder Vendor. I checked it myself. Displays everything as needed. Nothing is duplicated. - Urmuz Tagizade
  • 1. I work without a composer 2. Perezalil when I add -> asArray, then already displays Array ([0] => Array ([id] => 1 [date] => 2016-04-15 [result] => 3) [ 1] => Array ([id] => 2 [date] => 2016-04-14 [result] => 2) [2] => Array ([id] => 3 [date] => 2016-04 -13 [result] => 1)) 1 - Fitstd
  • That's all) Now everything is true - Urmuz Tagizade

3 answers 3

The model, the results inherited from ActiveRecord , and it in turn is BaseActiveRecord , and the one from Model and ActiveRecordInterface .

And the Model inherits extends Component implements IteratorAggregate, ArrayAccess, Arrayable , etc.
Accordingly, when a request is received, we create an instance of the results class and call some methods. And since the class instance contains the methods and fields of the parent classes, then with print_r you can see the information about them.

If you look at these classes, for example Model , you will see

  /** * @var array validation errors (attribute name => array of errors) */ private $_errors; /** * @var ArrayObject list of validators */ private $_validators; /** * @var string current scenario */ private $_scenario = self::SCENARIO_DEFAULT; 

or class Component

  /** * @var array the attached event handlers (event name => handlers) */ private $_events = []; ... 

Just what we see on the display. They are not taken from the database; these are just inherited fields. Consider this debug information.

  • Thanks for the answer, but how can I get everything out without debugging information? - Fitstd
  • well, apparently -> asArray, after find =) That is, results::find()->asArray()->all() But I so rudely called it debugging information. For to call all inherited fields debugging information somehow brute force)) - Alexey Shimansky

you get the model. In the model you have:

  1. Attributes, the current values ​​of your fields.
  2. _old_attributes - If you changed the model - but did not save, you can always see the original data in this array.
  3. _errors - model errors, after validation
  4. _related - connections with other models, etc., etc.

All this can be found in the official documentation http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html

This is not just "debug information", it can and should be used.

    Let me add to the above. Also, look at the datapowers - they can allow you to flexibly customize the display in the listing http://www.yiiframework.com/doc-2.0/yii-data-activedataprovider.html

    If you use the ListView widget http://www.yiiframework.com/doc-2.0/yii-widgets-listview.html with this type of dataprovider, then you can literally “clear” the output of the “unwanted” fields in two lines and display only you need. flexible tool in fact. For example

     <?php $resultsProvider = new ActiveDataProvider([ 'query' => Result::find(), 'pagination' => [ 'pageSize' => 3, ], ]); echo ListView::widget([ 'dataProvider' => $resultsProvider, 'itemOptions' => ['class' => 'item'], 'itemView' => '_result-item-view', ]); ?> 

    and in the view template for each _result-item-view output model you can output as you like - for example

     <p class="clearfix"> <?=Html::encode($model->getAttribute('id'))?> <?=Html::encode($model->getAttribute('result'))?> </p>