Yii2 Query caching with external variable

Hi, thanks for reading.

The query caching for query using external variable in not working when used in a model \models\Article. It used to work in Yii2-beta.

The yii2-beta code was


public static function cachedarticle($id) {

	$i=(int) $id;

	\Yii::$app->db->beginCache(43600);

	$art=\Yii::$app->db->createCommand('SELECT `t`.`id` AS `t0`, `t`.`title` AS `t1`, `t`.`content` AS `t2`, `t`.`tags` AS `t4`, `d`.`tname` AS `t3` FROM `article` `t`

			INNER JOIN `authors` `d` ON `t`.`author`=`d`.`id` WHERE `t`.`id`='.$i)->queryOne();

	\Yii::$app->db->endCache();

	return $art;

}

It was working.

When I tried the same code in yii2-rc as


public static function cachedarticle($id) {

	$i=(int) $id;

	$db=\Yii::$app->db;

	$art=$db->cache(function($db, $i) {

		return $db->createCommand('SELECT `t`.`id` AS `t0`, `t`.`title` AS `t1`, `t`.`content` AS `t2`, `t`.`tags` AS `t4`, `d`.`tname` AS `t3` FROM `article` `t`

			INNER JOIN `authors` `d` ON `t`.`author`=`d`.`id` WHERE `t`.`id`='.$i)->queryOne();

	}, 43600);

	return $art;

}

Error is shown as below

PHP Warning – yii\base\ErrorException

Missing argument 2 for app\models\Article::app\models\{closure}()

How shall I deal with this situation? Any help? Thanks in advance.

The answer was simple, I could have read some more of documentation.


$art=$db->createCommand('SELECT `t`.`id` AS `t0`, `t`.`title` AS `t1`, `t`.`content` AS `t2`, `t`.`tags` AS `t4`, `d`.`tname` AS `t3` FROM `article` `t`

			INNER JOIN `authors` `d` ON `t`.`author`=`d`.`id` WHERE `t`.`id`='.$i)-> cache(3600)->queryOne();

Thanks for this great framework. cheers