I have to fetch data from the database. The videos have thumb images. These images stored in json array.
But I want to fetch these string based array convert to php array.
I wrote this function and it works fine, but unfortunately I have no idea where or when should I call this convert function.
In the Controller’s Action I get the all Video ActiveRecord object from DB. After I tried to call in the __construct or in the init function but it seems the properties of AR is null in the construct and init functions.
Please check this code snippets with the comments.
// Controller
/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex()
{
$videos = Video::find()->all();
// other codes.
return ['index', ['models' => $videos]];
}
// Video ActiveRecord
/**
* {@inheritdoc}
*/
public __construct($config) {
// This is NULL, after or before the parent::__construct() calling, it does not matter!
$this->thumbs = $this->convertToPHPValueArrayInt($this->thumbs);
parent::__construct($config);
}
/**
* {@inheritdoc}
*/
public function init() {
// single-table inheritance
$this->source = self::TYPE;
// This is NULL, after or before the parent::init() calling, it does not matter!
$this->thumbs = $this->convertToPHPValueArrayInt($this->thumbs);
parent::init();
}
// Getter function
// There is thumbs property but If I call '$model->thumbs' it will not get the converted arrays.
// This is work only if I call in this way: '$model->getThumbs()'
/**
* @return array of video tags
*/
public function getThumbs() {
return $this->convertToPHPValueArrayInt($this->thumbs);
}
//View
<?php foreach ($models as $model) : ?>
<div class="row">
<div class="col-md-12">
<!--<?= $model->title ?>-->
</div>
<div class="col-md-12">
<?php
// Still the format json format.
// It seems the __construct and init functions not called, because I placed an echo function but it not run!
var_dump($model->thumbs);
?>
</div>
</div>
<?php endforeach; ?>
Do you have any idea how I can achieve that the thumbs propery will be modified in the __construct or init function? Or somehow else?
namespace common\models;
class Video extends \common\modules\VideoTube\models\Video
{
public function afterFind() {
throw new Exception("Tadam");
echo "In afterFind method - Before parent call";
die();
parent::afterFind();
echo "In afterFind method - After parent call";
die();
}
}
I do not see the echo arguments on the site, and the php running not died! So I will see the Video title and thumbs on the site… so I am sure the afertFind() method does not call. And the Exception not throwd.
I think the Yii2 uses another mechanism to fill out the object’s property with the values.