dropDownList is not fully populated - yii

In the view I have a such code
<?= $form->field($model, 'Language')->dropDownList(ArrayHelper::map(Doodles::getLanguages(), "Language", "language"), ['class'=>'form-control','prompt' => 'Choose language'])->label('Language') ?>
In the Doodles::getLanguages method
return Doodles::findBySql('select distinct language from doodles')->asArray()->all();
And getLanguages method returns
array (size=2)
0 =>
array (size=1)
'language' => string 'rus' (length=3) 1 =>
array (size=1)
'language' => string 'en' (length=2)
In result page I got a select with not filled value attributes and with only one option instead two
<select id="utdoodles-language" class="form-control" name="UtDoodles[Language]">
<option value="">Choose language</option>
<option value="" selected="">en</option>
</select>
Addition:
Doodles::getLanguages method return an array:
array (size=2)
0 =>
array (size=1)
'language' => string 'rus' (length=3)
1 =>
array (size=1)
'language' => string 'en' (length=2)

In the Doodles::getLanguages method change as below :
return Doodles::find()->select('language')->distinct()->asArray()->all();

Related

how to set base_url in trntv filekit yii2

I am using trntv filekit in yii2, when I update profile the widget save profile image correctly in storage but save wrong base_url in database? var_dump result show following code :
C:\wamp64\www\realestate\frontend\modules\user\controllers\DefaultController.php:72:
array (size=6)
'path' => string '1/8fxqQhjo7lhtCkMnNDM6mdo2sODCUtVb.png' (length=38)
'name' => string 'Capture' (length=7)
'size' => string '33507' (length=5)
'type' => string 'image/png' (length=9)
'order' => string '' (length=0)
'base_url' => string '/source' (length=7
but I want to get following
base_url' => string 'http://localhost/realestate/storage/web/source' (length=45)
How can I change base_url?
'fileStorage'=>[
'class' => 'trntv\filekit\Storage',
'baseUrl' => '#web/image' // you should assign this way
'filesystem'=> ...
// OR
'filesystemComponent' => ...
],
https://github.com/trntv/yii2-file-kit#file-storage

remove button from CGridView with condition

Hi I have CRUD generated CGridView in yii. I need to add a new button to CGridView rows and hide it if appointment_status(one of CGridView column) value equals 0
This is my code of CGridView,
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'bookings-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'email',
'telephone',
'time',
'employee',
'appointment_status',
'client_ip',
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => 'CHtml::button("$data->appointment_status",array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$data->id))."\'"))',
'visible'=>$data->appointment_status==1,
),
array(
'class' => 'CButtonColumn',
),
),
));
But all I'm getting is error stating,
Undefined variable: data
It would be great help if someone can look into it.
you can use like this:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'bookings-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'email',
'telephone',
'time',
'employee',
'appointment_status',
'client_ip',
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => function ($data) {
if ($data->appointment_status == 1) {
return CHtml::button("$data->appointment_status", array("onclick" => "document.location.href=\'" . Yii::app()->controller->createUrl("controller/action", array("id" => $data->id)) . "\'"));
} else {
return;
}
}
),
array(
'class' => 'CButtonColumn',
),
),
));
Your 'visible' handling the column visibility and not the button, you can use custom attribute on model to create and handle the button visibility.
add to your model:
public function getConfirmationButton()
{
if ($data->appointment_status == 1) {
return CHtml::button($this->appointment_status,array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$this->id))."\'"));
} else {
return '';
}
}
and call it in your view:
..........
'link' => array(
'header' => 'Confirmation',
'type' => 'raw',
'value' => '$data->confirmationButton',
),
...........
visible is a boolean or a PHP expression that will be evaluated to give a boolean. During the evaluation $data is assigned to the current item from the dataProvider used. $data doesn't exist outside of the evaluation function evaluateExpression(). As such the implementation should be:
`visible` => '$data->appointment_status == 1',
You need to quote value of visible key in link array. So instead of this:
'visible'=>$data->appointment_status==1
It should be:
'visible'=>'$data->appointment_status==1'
it should work now.
You will get undefined variable because visible not allow any callback.
Try this solution, it's yii2 code and i don't know much of Yii.
'delete' => function ($url, $model) {
return ($model->isVisible($model)) ?
Html::a('<span class="glyphicon glyphicon-trash"></span>',
$url,
['title' => Yii::t('app', 'Delete')]) : '';
public static function isVisible($data)
{
return ($data->appointment_status == 1) ? true : false;
}

How can i get first and last record id in yii CGridview?

I want first and last record id form $dataprovider which is passed to gridview which i need to pass to this link.
array(
'name' => 'msg',
'value' => 'CHtml::link(substr(strip_tags($data->msg),0,30)." .....",Yii::app()->createUrl("Mail/view",array("id"=>$data->primaryKey,"flag"=>"inbox","tab"=>'.$tab.',"category"=>'.$category.')))',
'type' => 'raw',
'header' => 'Message',
),
Declare two attributes in your controller.
public $first=null;
public $last=null;
Define a function in controller to render your link
public function renderMailViewLink($data,$row){
// you can return anything you want here. whether a link or whatever
// access $this->first->id , $this->last->id
return CHtml::link($data->anyAttribute,array('someRoute','id'=>$data->id,'first'=>$this->first->id))
}
CActiveDataProvider has a method getData(), which return array of all active records.
in actionIndex
$dataProvider = $model->search()
$data = $dataProvider->getData();
$this->first = reset($data );
$this->last = end($data);
And finally in your view
array(
'name' => 'msg',
'value' => array($this,'renderMailViewLink'),
'type' => 'raw',
'header' => 'Message',
),
Please note this code is not tested. But thats how it can be achieved

Symfony DQL - transform the array query result

I'm trying to transform the array of my query result.
First I did this query:
public function findAllTraduction()
{
return $this->createQueryBuilder('c')
->select('c.key, c.content, c.id, f.locale')
->leftJoin('c.TraductionFile', 'f')
->groupBy('c.key')
->getQuery()
->getArrayResult();
}
The result is like that:
array
0 =>
array
'key' => string 'FORMAT1'
'content' => string 'login'
'id' => int 507
'locale' => string 'en'
1 =>
array
'key' => string 'FORMAT1'
'content' => string 'connecter'
'id' => int 508
'locale' => string 'fr'
2 =>
array
'key' => string 'FORMAT2'
'content' => string 'password'
'id' => int 503
'locale' => string 'en'
3 =>
array
'key' => string 'FORMAT2'
'content' => string 'mot de passe'
'id' => int 504
'locale' => string 'fr'
What I would like to have is an array like that:
array
'FORMAT1' =>
array
'en' =>
array
'content' => string 'login'
'id' => int 507
'fr' =>
array
'content' => string 'connecter'
'id' => int 508
'FORMAT2' =>
array
'en' =>
array
'content' => string 'password'
'id' => int 503
'fr' =>
array
'content' => string 'mot de passe'
'id' => int 504
In fact, for each same 'key' (here 'FORMAT1' and 'FORMAT2', regroup by 'locale' (EN and FR)
Is it possible to do that in the query ?
I tried with GROUPBY and DISTINCT but nothing happened...
If not possible in query, may be redraw an array with loop....
thank you !
Doctrine provides a way to specify indexBy when creating the QueryBuilder in order to index the resulting array not in a sequential way but by a specific key :
createQueryBuilder($alias, $indexBy = null)
So you may want to try something like createQueryBuilder('c', 'c.key') to use c keys as array indexes
I found a solution with foreach for redraw the array ...
$catalogue = [];
foreach ($catalogues_array as $index => $val) {
$key = $val['key'];
$locale = $val['locale'];
unset($catalogues_array[$index]['key']);
unset($catalogues_array[$index]['locale']);
$catalogue[$key][$locale] = $catalogues_array[$index];
}
return $catalogue;
the result :
array (size=421)
'base_template.page_title' =>
array (size=2)
'en' =>
array (size=6)
'content' => string 'Audio Video Caption - Subtitling & Transcription of media.' (length=58)
'description' => null
'traductionControle' => boolean false
'id' => int 1
'domain' => string 'messages' (length=8)
'bundleName' => string 'app' (length=3)
'fr' =>
array (size=6)
'content' => string 'Audio Video Caption - Sous-Titrage & Transcription de médias.' (length=62)
'description' => null
'traductionControle' => boolean false
'id' => int 9
'domain' => string 'messages' (length=8)
'bundleName' => string 'app' (length=3)
'base_template.meta.keywords' =>
array (size=2)
'en' =>
array (size=6)
'content' => string 'closed captioning, subtitle, speech recognition, transcription, traduction, video, audio' (length=88)
'description' => null
'traductionControle' => boolean false
'id' => int 2
'domain' => string 'messages' (length=8)
'bundleName' => string 'app' (length=3)
'fr' =>
array (size=6)
'content' => string 'sous-titre, reconnaissance vocale, transcription, traduction, video, audio' (length=74)
'description' => null
'traductionControle' => boolean false
'id' => int 10
'domain' => string 'messages' (length=8)
'bundleName' => string 'app' (length=3)

Yii Cgridview how to render in view 2 or more values from the same table in 1 column

I have a Cgridview that has multiple columns. I would like to merge and render the values of 3 columns in 1 column.
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'policy-grid',
'dataProvider'=>$dataProvider,
'filter'=>$dataProvider->model,
'selectableRows' => 1, // you can select only 1 row!!
'selectionChanged'=>'function(id){ var objectId = $.fn.yiiGridView.getSelection(id);
if (isNaN(objectId) || objectId == ""){return;} location.href =
"'.$this->createUrl('policy/view').'&id="+$.fn.yiiGridView.getSelection(id);}',
'htmlOptions'=>array('class'=>'grid-view grid_pointer',),
'columns'=>array(
array(
'name' => 'compliance',
'type' => 'html',
'value' => '$data->status == "ACTIVE" ? (CHtml::image($data->compliance ==
"SUFFICIENT" ? "images/policy_sufficient.png" : "images/policy_insufficient.png"))
: "N/A" ',
'filter' => VFFormUtil::getFilter(VFFormUtil::POLICY_COMPLIANCE),
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'status',
'filter' => VFFormUtil::getFilter(VFFormUtil::ACTIVE_INACTIVE),
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'coverage_left',
'htmlOptions' => array('style'=>'text-align: center'),
),
array(
'name' => 'model_year',
'htmlOptions' => array('style'=>'text-align: center'),
),
'make',
'model',
How would I combine 'model_year','make' and 'model', into 1 column in the views?
The correct way would be to create a new property variable in the model, and join the values afterFind().
See How to have multiple fields (D/M/Y) for single date property in Yii?, but you have to swap the find and save functions, as that user wants to do the opposite (split single column into three fields instead).
You can create a getter in the model:
public function getModelLabel()
{
return $this->model_year.' '.$this->make.' '.$this->model;
}
Then you can use modelLabel in your GridView as if it were a real attribute.