Yii2 kartik select2 widget showing values too issue - php

I have created a yii2 kartik select2 widget to select multiple car models like below
<?= Select2::widget([
'name' => 'drp-make',
'data' => Car::getCarMakesEnglish(),
'value' => explode(",",$model->drp_make),
'options' => [
'id'=>'drp-make',
'placeholder' => 'All Makes',
'multiple' => true
]
]); ?>
And the function to get data for the select2 like
public static function getCarMakesEnglish(){
$out=array();
$makes=CarMakes::find()->select(['id','make_eng'])->all();
foreach ($makes as $make) {
array_push($out,array($make['id'] => $make['make_eng']));
}
return $out;
}
Its working perfect.But a issue is there.please see the below picture
Its showing values too not only the names.I want to show only the make names.How to do that

Because you are pushing an array to every index of the $out
array_push($out,array($make['id'] => $make['make_eng']))
you should use yii\helpers\ArrayHelper::map() instead that do this for you. Change your function getCarMakesEnglish() to the following
public static function getCarMakesEnglish()
{
$makes = CarMakes::find()->select(['id', 'make_eng'])->all();
return \yii\helpers\ArrayHelper::map($makes,'id','make_eng');
}

In addition to #MuhammadOmerAslam answer, you can avoid ArrayHelper::map() for simple scenario by using column()
public static function getCarMakesEnglish()
{
return CarMakes::find()->select('make_eng')->indexBy('id')->column();
}

Related

add button to perform an action prior to sending to view page

I'm working on a program that is a reservation service for lab systems using Yii2.0. I've been using Yii for a little while but for some reason this one is stumping me
I have all of the systems listed in a gridview and I'd like to have a button in the actionColumn that will run the 'reserve' action and then show the view for the individual system.
I have the button added and it brings the user to the view page but I don't know what I need to do to have it run the reserve action first...or if it's even possible. I've tried pointing it to the reserve action in the controller but of course that looks for a view page rather than an action.
Here's some of the code from what I've been trying after looking through many pages of suggestions:
On the index page
['class' => 'yii\grid\ActionColumn',
'template' => '{reserve}',
'buttons' => [
'reserve' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-ok-circle"></span>',
$url,
[
'title' => 'Reserve',
'data-method' => 'post',
'data-pjax' => 0,
]);
}
],
'urlCreator' => function ($action, $model, $key, $index) {
return Url::toRoute(['cml/view', 'id' => $key]);
}
Function in Controller
public function actionReserve($id)
{
$model = $this->findModel($id);
if ($model->fkReservedTo == 1)
{
$model->fkReservedTo = Yii::$app->user->id;
// shouldn't you call save here
$model->save();
return $this->redirect(['view','id'=>$id]);
}
else
{
Yii::$app->session->setFlash('error', 'This system is not available to be reserved');
return $this->showAlert();
}
}
Any suggestions would be appreciated.
Change
return Url::toRoute(['cml/view', 'id' => $key]);
to
return Url::toRoute(['cml/reserve', 'id' => $key]);
in your urlCreator function.

how to specify table name in addColumn() function in magento

I am trying to get values from a table but don't know where to specify table name in addColumn() function in magento. Kindly help.
Following is the code:
protected function _prepareColumns()
{
$this->addColumn('orders_count', array(
'header' => Mage::helper('sales')->__('Orders'),
'index' => 'orders_count',
'type' => 'number',
'total' => 'sum',
'sortable' => false
));
}
It looks like your Grid code
Whenever Grid is loading firstly it will load a function "_prepareCollection()" where we collect collection data to show in grid. There we define our resource model(or table).
Example:
protected function _prepareCollection() {
$collection = Mage::getResourceModel('module/table_collection');
$this->setCollection($collection);
return parent::_prepareCollection();
}

Magento custom admin module wysiwyg integration

I've created an admin module based off of the tutorial here. I'm attempting to change two of my form inputs to wysiwyg editors based off of information found here. However, whenever I load the edit page I get an error Call to a member function setCanLoadTinyMce() on a non-object. $this->getLayout()->getBlock('head') var_dumps to false.
Namespace/Slides/Block/Adminhtml/Slide/Edit.php looks as follows
class Namespace_Slides_Block_Adminhtml_Slide_Edit
extends Mage_Adminhtml_Block_Widget_Form_Container
{
protected function _prepareLayout()
{
parent::_prepareLayout();
if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled()) {
$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
}
}
protected function _construct()
{
//... Construction stuff
}
}
Namespace/Slides/Block/Adminhtml/Slide/Edit/Form.php
class Cfw_Slides_Block_Adminhtml_Slide_Edit_Form
extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
//...Do some things first, like create the fieldset..
//Add the editable fields
$this->_addFieldsToFieldset($fieldset, array(
'foreground_image' => array(
'label' => $this->__('Foreground Image'),
'input' => 'image',
'required' => false,
),
'background_image' => array(
'label' => $this->__('Background Image'),
'input' => 'editor',
'required' => true,
'config' => Mage::getSingleton('cms/wysiwyg_config')->getConfig(),
'wysiwyg' => true,
),
'description' => array(
'label' => $this->__('Text Overlay'),
'input' => 'editor',
'required' => false,
'config' => Mage::getSingleton('cms/wysiwyg_config')->getConfig(),
'wysiwyg' => true,
)
));
return $this;
}
protected function _addFieldsToFieldset(
Varien_Data_Form_ElementFieldset $fieldset, $fields)
{
$requestData = new Varien_Object($this->getRequest()->getPost('slideData'));
foreach ($fields as $name => $_data) {
if ($requestValue = $requestData->getData($name)) {
$_data['value'] = $requestValue;
}
//Wrap all fields with slideData group
$_data['name'] = "slideData[$name]";
//Generally, label and title are always the same
$_data['title'] = $_data['label'];
//If no new value exists, use the existing slide data.
if (!array_key_exists('value', $_data)) {
$_data['value'] = $this->_getSlide()->getData($name);
}
//Finally, call vanilla funcctionality to add field.
$fieldset->addField($name, $_data['input'], $_data);
}
return $this;
}
}
I'm not sure if you need it, but here's my file structure as well
Namespace
-Slides
--Block
---Adminhtml
----Slide
-----Edit
------Form.php
-----Edit.php
-----Grid.php
----Slide.php
--controllers
---Adminhtml
----SlideConroller.php
--etc
---config.xml
--Helper
---Data.php
--Model
---Resource
----Slide
-----Collection.php
----Slide.php
---Slide.php
--sql
---namespace_slides_setup
----install-0.0.1.php
The issue is that Magento cannot find your head block.
Instead of using:
$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
Try calling it like this:
Mage::app()->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
If that doesn't work, it's a different issue but the problem is still that Magento can't find the head block.
I imagine you no longer need a solution for this, but I ran into the same issue using the same tutorial that you did.
The 'head' block (and thus setCanLoadTinyMce()) was unavailable in the Edit.php and the Form.php via the _prepareLayout() function.
The 'head' block was available in the controller(SlideController.php in your case) between $this->loadLayout() and $this->renderLayout within the editAction() function.
In SlideController.php change
$this->loadLayout()
->_addContent($brandBlock)
->renderLayout();
to
$this->loadLayout() ;
$this->_addContent($brandBlock);
$this->getLayout()->getBlock('head')->setCanLoadTinyMce(true);
$this->renderLayout();

Can't transform results of Eloquent query

I'm building an API in Laravel to learn how to do such a thing. I'm following a Laracasts course to do this, but I'm having some troubles with the parts I want to do for myself.
Currently, I have this function in my controller. It fetches data from two tables and then returns it.
public function lesson($userid)
{
$lessons = DB::table('lessons')
->join('userlessons', 'lessons.id', '=', 'userlessons.lessonsid')
->select('lessons.name', 'lessons.seen')
->where('userlessons.userid','=', $userid)
->get();
return $this->respondWithPagination($lessons, [
'data' => $this->LessonTransformer->transformCollection($lessons)
]);
}
And LessonTransformer is this:
class LessonTransformer extends Transformer
{
public function transformCollection($items)
{
return array_map([$this, 'transform'], $items);
}
public function transform($item)
{
return [
'name' => $item['name'],
'seen' => (bool) $item['seen']
];
}
}
I tried a lot of solutions, some smart, some stupid. But I keep getting this error: Cannot use object of type stdClass as array
If you get such error, probably you need to change:
return [
'name' => $item['name'],
'seen' => (bool) $item['seen']
];
into:
return [
'name' => $item->name,
'seen' => (bool) $item->seen
];
but you haven't showed in which line error appear so it's only a guess

Yii Dropdownlist- show response array as values

After an ajax request i got one array from my controller as response, how can I use this array values in my dropdown list?
Answer
in my view I have
echo CHtml::dropDownList('client_id', '',CHtml::listData($model,'client_id','client_name'), array(
'ajax'=> array(
'type'=>'POST',
'url'=>Yii::app()->baseUrl.'/index.php?r=page/dynamicDropdownList',
'update'=>'#program_id',
'empty'=>'-Select a Client-')));
// I need to populate the response array in this dropdownlist
echo CHtml::dropDownList('program_id','', CHtml::listData($result,'program_id', 'program_name'));
in my controller
public function actionDynamicDropdownList()
{
if($_POST['client_id'] > '0') {
$result = Yii::app()->db->createCommand()->select('program_id, program_name')->from('program')->where('client_id='.$_POST['client_id'].'')->order('program_name')->queryAll();
$this->render('admin', array(
'result' => $result,
));
}
}
Another Problem
Now I got everything working except the second drop downlist is also showing the values of 1st dropdown list with the result.
solution:- I have parse the response and show it in the dropdownlist
This is how your controller should look like:
public function actionIndex() {
$model = new SearchForm();
if ($_GET['SearchForm']) {
$model->attributes = $_GET['SearchForm'];
}
//here is where you put your criteria or query commands
if ($_GET['ajax']) {
$this->renderPartial('index', array(
'model' => $model,
));
} else {
$this->render('index', array(
'model' => $model,
));
}
}
You catch the data from ajax or GET and you do something with it, then you pas the data to the view;
And this should be what the vies contains:
<? echo $form->dropDownList($model, 'position_type', CHtml::listData(PositionType::model()->findAll(array('condition' => 'status=1')), 'id', 'name'), array('class' => 'postdropdown2', 'empty' => array(-1 => 'All'), 'onchange' => 'showDiv(this.value,1);')); ?>
You have to work with data received using ajax in the controller, as it is not visible in the view, if it was not passed using render or renderpartial

Categories