0

I have users, orders and plans. When client buy plan, data is save in orders , plans are in account_plan and the information for user is in table users. In table orders is when the plan start and when it is expired. I use for Select2 ArrayHelper, but do not show the column here is a query

 $masAcc[0] = Yii::t('app', 'Choose plan');
 $masAcc['----------------'] = 
ArrayHelper::map(
\backend\models\Orders::find()
->select('orders.*,account_planLang.name as name')
->leftJoin('orders_props','`orders_props`.`order_id`= `orders`.`id`')
->leftJoin('account_plan','`orders_props`.`product_id`=`account_plan`.`id`')
->leftJoin('account_planLang','account_plan.id=account_planLang.plan_id')
->where('`orders`.`dt_end`<CURDATE() + INTERVAL 5 DAY AND `orders`.`dt_end`<CURDATE()')
->all(), 'id', 'name');

but the error is : Getting unknown property: backend\models\Orders::name here is Select2:

 $form->field($model, 'account')->widget(Select2::classname(), [
                        'model' => $model,
                        'theme' => 'bootstrap',
                        'options' => [
                            'placeholder' => Yii::t('app', 'app.choose'),
                            'class' => 'form-control select2'
                        ],
                        'data' => $masAcc,
                        'pluginOptions' => [
                            'allowClear' => true,
                        ],
                    ]);
                    }
0

2 Answers 2

2

That is because your query returns list of Orders models, which does not have name column, so it cannot represent result of this query. You need to use asArray() when you want to query field that is not available in model:

ArrayHelper::map(
    \backend\models\Orders::find()
        ->select('orders.*,account_planLang.name as name')
        ->leftJoin('orders_props','`orders_props`.`order_id`= `orders`.`id`')
        ->leftJoin('account_plan','`orders_props`.`product_id`=`account_plan`.`id`')
        ->leftJoin('account_planLang','account_plan.id=account_planLang.plan_id')
        ->where('`orders`.`dt_end`<CURDATE() + INTERVAL 5 DAY AND `orders`.`dt_end`<CURDATE()')
        ->asArray() // <- this
        ->all(), 
    'id', 
    'name'
);

Or add name field to your model:

class Orders extends ActiveRecord {

    public $name;

    // ...

}
Sign up to request clarification or add additional context in comments.

2 Comments

I think user need to fetch account_planLang.name
he is using alias account_planLang.name as name for the field. @SudharshanNair
1

I guess there is no need to use ArrayHepler::map(). Try this way:

Orders::find()
  ->select('account_planLang.name as name, table_name.id as id')
  ->leftJoin('orders_props','`orders_props`.`order_id`= `orders`.`id`')
  ->leftJoin('account_plan','`orders_props`.`product_id`=`account_plan`.`id`')
  ->leftJoin('account_planLang','account_plan.id=account_planLang.plan_id')
  ->where('`orders`.`dt_end`<CURDATE() + INTERVAL 5 DAY AND `orders`.`dt_end`<CURDATE()')
  ->indexBy('id')
  ->column();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.