how to concatenate two columns in yii2 activerecord? - activerecord

I am new to developing in yii2 (and yii). I have a query that returns name and last name of customers along with other columns. I am using activeDataProvider to display the records in a table but I need the name and lastname be displayed in one table column.
What I want to know is:
1. how do I concatenate the columns in yii2 activerecord?
2. How do I display the columns in one table columns made gridView?
below is what I have tried that none works:
1. $query->select(['customer.*', 'fullname'=>concat('customer.name'," ",'customer.lastname')]);
2. 'dataProvider' => $model['dataProvider'],
'columns' => [
'id',
'name'.' '.'lastname',
'customer_orders',
'created_at:datetime'

Use GridView like below.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute' => 'name',
'value' => function ($model) {
return $model->name . ' ' . $model->lastname;
}
]
'customer_orders',
'created_at:datetime'
]
]) ?>
Refer Yii2 GridView

Related

save both field same name relationship one to one backpack laravel

I have two tables with relationship one to one hasOne and have column same name
CRUD::addField([
'label' => "Title",
'type' => 'text',
'name' => 'new_title', // the db column for the foreign key
]);
CRUD::addField([
'label' => "Title",
'type' => 'text',
'name' => 'achive.new_title', // the db column for the foreign key
'entity' => 'achive',
]);
I just want show once but save both to two table
You need something that's call the callbacks method, the simplicity of that method is you can control what you want after create, edit, delete and etc.
So, in your controller just need one field name,
CRUD::addField([
'label' => "Title",
'type' => 'text',
'name' => 'new_title',
]);
After that, you can create a new trait in your controller for creating/updating your relationship,
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
And then, declare the store function,
public function store() {
// Get the value from the request
$request = $this->crud->getRequest();
// And then you can insert/update data into your relationship table
\DB::table('achives') // Your relation table
->updateOrInsert(
['new_title' => $request->new_title],
['other_column_name' => 'Value of your other column']
);
$response = $this->traitStore();
// do something after save
return $response;
}
The updateOrInsert method accepts two arguments: an array of conditions by which to find the record, and an array of column and value pairs indicating the columns to be updated. more info here

How to add multi select value in database in October CMS Backend?

Not inserted multi select value in database.
My fields.yaml Code is :
related_recipe:
label: 'Related Recipe'
span: auto
nameFrom: recipe_title
descriptionFrom: description
attributes: {multiple:'multiple'}
type: relation
My model code is :
public $belongsTo = [
'related_recipe' => [
'Qdata\Taeq\Models\Recipe',
'conditions' => 'status = 1'
],
];
Currently only one selected value inserted in database.need add multiple value in database. Can any one have the solution of it ?
You should use a $belongsToMany relation in this case.
$belongsTo means your model is link with a single entity of your Recipe model.
To do so with relation you need to go with "belongsToMany" relation. for example:
In your Model
'related_recipes' => [
'Qdata\Taeq\Models\Recipe',
'table' => 'pivot_table_name',
'key' => 'foreign_key_of_pivot_table',
'otherKey' => 'other_key',
],
In related another Model
'makers' => [
'Qdata\Taeq\Models\AnotherModel',
'table' => 'pivot_table_name',
'key' => 'foreign_key_of_pivot_table',
'otherKey' => 'other_key',
],
this will save your multiselect dropdown data in the related pivot table in your database.

cant get ESelect2 extension in yii concatenate two or more columns in the dropdown

Eselect2 is the extension am using and myADropDown() fetches the data and displays but then my need is concatenate two or more columns in the dropdown
model1 view
<?php $this->widget('ext.select2.ESelect2', array(
'name' => 'id',
'model' => $model,
'options' => array(
'placeholder' => $model->getAttributeLabel('id'),
),
'data' => $model->myADropDown(),
));?>
model1
public function getConcatened()
{
return $this->name.' '.$this->locate.' '.$this->rating;
}
public function myADropDown()
{
$vid=Yii::app()->SESSION['vid'];
$gid=Model2::model()->xyz($vid);
$list=CHtml::listData($gid,'id','concatened');
return $list;
}
// id is fk in another tbl
in the dropdownlist my need is name ,location,rating for each person , am unable to do so
Please Let me know how do i achieve it
For concatenating the table fields in listdata, Please check this Yii forum answer:
Link
As per the link suggestion, you need to add a function in model to get concatenated string.
public function __get($key){
$action = 'get'.str_replace(' ','',ucwords(str_replace('_',' ',$key)));
if(method_exists($this, $action)) return $this->$action();
return parent::__get($key);
}

Magento Admin formfield multiselect selected

I´m currently developing a custom module for magento thats going to list employees.
I have figured out almost everything. The only thing I got left is how the selected values is going to be highlighted.
The problem I´m having is for the backend.
I got 2 tabs per employee, one for employee data and one tab for magento categories.
1 employee can have 1 or more categories.
The database table that the categories are stored in are a non-eav table.
So my question is
What in a multiselect determines which values are selected? As it is now, only one value is selected.
I think you can do this by simply passing in an array of the id's to be selected into the 'value' attribute of the field being added for the multiselect in the _prepareForm() method. Something like the following.
$fieldset->addField('category_id', 'multiselect', array(
'name' => 'categories[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('mymodule/mymodel')->getMymodelValuesForForm(),
'value' => array(1,7,10),
));
The id of the form element (e.g. category_id) must not be an attribute in your model, otherwise when the form values get set with $form->setValues() later on, the attribute value will be overwritten.
I normally store multiple selections as a text column separated by commas much like most magento modules handles stores which requires a slightly different approach as shown below.
In the form block for the tab with the multiselect, you firstly define the element to be displayed like so in the _prepareForm() method. You then get the values from the model and set put them into the form data.
protected function _prepareForm()
{
...
$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
...
if ( Mage::getSingleton('adminhtml/session')->getMymodelData() )
{
$data = Mage::getSingleton('adminhtml/session')->getMymodelData();
} elseif ( Mage::registry('mymodel_data') ) {
$data = Mage::registry('mymodel_data')->getData();
}
$data['store_id'] = isset($data['stores']) ? explode(',', $data['stores']) : array();
$form->setValues($data);
}
I normally store the selected stores (categories as in your case) in the main model as a text column and comma separated values of ids, hence the explode.
In the controller for for the edit action, I put the model being edited into the mage registry so we can load it and it's values in the step above.
Mage::register('mymodel_data', $model);
Thanks for answering.
This is how my field looks like:
$fieldset->addField('npn_CatID', 'multiselect', array(
'label' => Mage::helper('employeelist')->__('Kategori'),
'class' => 'required-entry',
'required' => true,
'name' => 'npn_CatID',
'values' => $data,
'value' => array(3,5)
));
npn_CatID is the value in my db where the category id is saved.
I have tried to change the name and field ID but cant get it working.
When its the field id is like above ONE value is selected and its the last one inserted for the chosen employee
My data array looks likes
array(array('value' => '1', 'label' => 'USB'), array('value' => '2', 'label' => 'Memories'))

Codeigniter Update Multiple Records with different values

I'm trying to update a certain field with different values for different records.
If I were to use MySql syntax, I think it should have been:
UPDATE products
SET price = CASE id
WHEN '566423' THEN 49.99
WHEN '5681552' THEN 69.99
END
WHERE code IN ('566423','5681552');
But I prefer to use Active Record if it's possible.
My input is a tab delimited text which I convert into an array of the id and the desired value for each record:
$data = array(
array(
'id' => '566423' ,
'price' => 49.99
),
array(
'id' => '5681552' ,
'price' => 69.99
)
);
I thought this is the proper structure for update_batch, but it fails. Here's what I've tried:
function updateMultiple()
{
if($this->db->update_batch('products', $data, 'id'))
{
echo "updated";
}
else
{
echo "failed )-:";
}
}
And I get failed all the time. What am I missing?

Resources