how to generating a dropdown list in yii2 - yii

I tried to make a dropdown list in yii2 using this link : How to make a drop down list in yii2?
my code is :
<?php use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>
<?= $form->field($model, 'parent_id')-> dropDownList($listdata); ?>
but I have a problem in line of using ArrayHelper
the problem is:
PHP Notice – yii\base\ErrorException
Array to string conversion.......! I tested the below code :
$listData=ArrayHelper::map(Product::find()->asArray()->all(),'id','name');
but it dos not solved and has the same error!
whats the problem? can somebody help me?

You are trying to echo an array, change <?= to <?php in:
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>

Try like this
<?php
use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $form->field($model, 'parent_id')->dropDownList(
ArrayHelper::map(Product::find()->all(),'id','name'),
['prompt'=>'Select '])
?>

Related

Class 'yii\models\user' not found in view yii2

I am using YII2 advanced and I am trying from some hour but not any code working for me. I want to create dropdown from user table in my post page. I have found this error Class 'yii\models\user' not found. i have created this post with GII. my _form.php code
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\models\user;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $model app\models\Posts */
/* #var $form yii\widgets\ActiveForm
<?php/* <?= $form->field($model, 'id')->textInput(['value' => \Yii::$app->user->identity->id]) ?>
use yii\models\user;
*/
?>
<div class="posts-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'post_title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'post_description')->textarea(['rows' => 6]) ?>
<?php
echo Html::activeDropDownList($model, 'author_id',ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Why not working please anyone help me.
Thanks in advence
User::find() shows that you would use a User model, so really in uppercase as scaisEdge has answered to you.
However you have to know, in what namespace your User model is sitting. Yii2 doesn't have a User model, you have to create it. You mentioned, that you are using the advanced template. In that case you should create a User model in 'common/models/User.php' instead of 'models/User.php'. Follow the description in the Yii2 Guide.
In php typically a class name begin with an Uppercase char so in a unix like env (with file system based on a case sensitive constrain ) this must match try
use yii\models\User;

I can not create Dropdownbox in Yii framework

I want to create dropdownbox in Yii framework which have name is Category,I copy in the stackoverflow but it has error,
<form>
<?php
$list = CHtml::listData(Categories::model()->findAll(array('order' => 'cate_name')), 'id', 'cate_name');
echo $form->dropDownList("Category", 'cate_name', $list);
?>
</form>
here is error:
Undefined variable: form
Your form should looks like this:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'your-form',
'enableAjaxValidation'=>false,
)); ?>
<?php
$list = CHtml::listData(Categories::model()->findAll(array('order' => 'cate_name')), 'id', 'cate_name');
echo $form->dropDownList("Category", 'cate_name', $list);
?>
<?php $this->endWidget(); ?>
NOTE: replace form tag with form widget
Instead of using html form tag, you have to use yii form widget.
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'form_id',
)); ?>
For tutorial: http://www.yiiframework.com/doc/guide/1.1/en/form.view

change header after login in yii

How do I change the header after login, or is using another header even the right way to do it? Meaning there would be two different headers(guest/user). I've searched for it, mostly about redirects of the entire page, not what I am looking for.
Sorry for the noob question. :/
Make two files in your view with guest_header.php and user_header.php when user is logged in user user_header.php as your header file in your layout or simply use guest_header.php
and in your column layout use it like below
when user is logged in
<?php if(Yii::app()->user->id): ?>
<?php $this->beginContent('//layouts/user_header.php'); ?>
when user is guest
<?php else: ?>
<?php $this->beginContent('//layouts/guest_header.php'); ?>
<?php endif; ?>
Added this line
<?php $this->endContent(); ?>
<?php $this->beginContent('//layouts/main'); ?>
<?php echo $content; ?>
</div><!-- content -->
<?php $this->endContent(); ?>

Changing text beside upload field in Yii Framework

hopefully a quick question here, have not been able to find out the answer on google. Anyway I have the following File upload field.
<div class="row">
<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data')); ?>
<?php echo $form->labelEx($model, 'doc'); ?>
<?php echo CHtml::activeFileField($model, 'doc'); ?>
<?php echo $form->error($model, 'doc'); ?>
</div>
The question I have is how to change the default text? On the page in question the upload field comes up with "No file chosen" beside the button when the page loads. I wish to change this text, anyone tell me how? Thanks.
I quick & dirty fix is this:
<?php echo CHtml::activeFileField($model, 'type',array('style'=>'width:100px;', 'onchange' => 'this.style.width = "100%";')); ?>

Yii Framework: Create a link with multiple parameters CHTML::link

I would like to link to a different controller, passing in multiple parameters.
Documentation:
http://www.yiiframework.com/wiki/48/by-example-chtml/
<?php echo CHtml::link('Link Text',array('controller/action',
'param1'=>'value1',
'param2'=>'value2',
'param3'=>'value3')); ?>
<?php echo CHtml::link('Link Text',
Yii::app()->createUrl('controller/action',array(
'param1'=>'value1',
'param2'=>'value2',
'param3'=>'value3')); ?>