1

How can i convert the following to yii2

$dependency = new CDbCacheDependency('SELECT count(*) FROM job_title');
$jobTitle  = JobTitle ::model()->cache(CACHE_TIMEOUT,$dependency)->findAll($array);
$jobTitleList = CHtml::listData($jobTitle, 'job_title_id','desc');
return $jobTitleList;

I have the following code which does not work.

use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

$items = array();
foreach (Jobsprocess::find()->where($array)->all() as $value) {
    $items[$value->process_id] = $value->jobsprocess;
}
return $items;
Mahendran Sakkarai
  • 8,381
  • 6
  • 44
  • 66
shorif2000
  • 2,582
  • 12
  • 65
  • 137

2 Answers2

9

If I understand your question correctly, you need the following code in Yii 2 format.

Yii 1:

$dependency = new CDbCacheDependency('SELECT count(*) FROM job_title');
$jobTitle  = JobTitle ::model()->cache(CACHE_TIMEOUT,$dependency)->findAll($array);
$jobTitleList = CHtml::listData($jobTitle, 'job_title_id','desc');
return $jobTitleList;

Yii 2:

use yii\helpers\ArrayHelper;
use app\models\JobTitle;

$jobTitleList = ArrayHelper::map(JobTitle::find()->where($array)->all(), 'job_title_id','desc');
return $jobTitleList;

But I don't know what are you doing with Jobsprocess model in the Yii 2 code. Because, In the Yii 1 format you haven't used that model.

Mahendran Sakkarai
  • 8,381
  • 6
  • 44
  • 66
3

Replacement for CHtml::listData in Yii 2 is ArrayHelper::map() Example:

$jobTitleList = ArrayHelper::map($jobTitle, 'job_title_id', 'desc');

Read more in official docs.

arogachev
  • 33,150
  • 7
  • 114
  • 117