How to display first name and last name in drop down list in yii2 - yii2-basic-app

I have Employee model, controller and view. Employee table contains FirstName, MiddleName, LastName, etc.
Now I wanted to display Employee FirstName and LastName together in yii2 dropdown list. How to do that?

The dropdown list expects items as an array. Just concat first, middle and last name, add it to the array and pass it into the dropdown method.
$names = [trim($employee->firstName . ' ' . $employee->middleName . ' ' . $employee->lastName)];
echo dropDownList('full-name', null, $names);
Or you can define a fullName method for Employee and use that.
Inside Employee class
public function getFullName() {
return trim($this->firstName . ' ' . $this->middleName . ' ' . $this->lastName);
}
Usage
$names = [$employee->fullName];

You can use ArrayHelper . First Import ArrayHelper class as below.
use yii\helpers\ArrayHelper;
Assuming your variable name as $model. Map id with first_name and last_name as below.
$data=ArrayHelper::map($model,'id',function($model){ return $model->first_name.' '.$model->last_name;});
Code for dropdown
<?php echo $form->field($model, 'user_id')->dropDownList($data); ?>

Related

Magento2 DOB Customer attribute Save

While customer creation , i need to store dob attribute for customer.which is not saving. facing issue like,
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customer_entity.value_id' in 'field list', query was: SELECT customer_entity.value_id FROM customer_entity WHERE (attribute_id='11' AND entity_id='199')
meanwhile,if i added custom date attribute in customer entity it get saved.
Any one have any idea about it.
Thanks in advance.
I am storing custom attributes as given below,
$data = []; //Array of attributes with key as attribute code
$customer = $this->customerModel->load($customerId);
$customerData = $customer->getDataModel();
$customerResource = $this->customerAttrFactory->create();
foreach ($data as $attrCode => $attrValue):
$customerData->setCustomAttribute($attrCode, $attrValue);
$customer->updateData($customerData);
$customerResource->saveAttribute($customer, $attrCode);
endforeach;
No need to save dob as an attribute.
Customer entity table has dob field so we can set it through customer model as given below.
$websiteId = $this->storeManager->getWebsite()->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$data['dob'] = $formData['dob'];
$customer->setData($data);
$customer->save();

How to update tables many times with Entity Framework

I have two tables with relationships, they are :
Table users - Table Region
----------------- -----------------
' idUser ' ' idRegion '
'-------------- ' '---------------'
' email ' ' regionName '
'---------------' '---------------'
' password '
'---------------'
' user_name '
'---------------'
' phone_number '
'---------------'
' id_Region '
'---------------'
In my case, it has three states of inserting and updating datas.
The user will populate just the Email and password rows.
The user is able to populate the rest of rows including the RegionName.
And finally, the user can update any rows that he wants but email and password.
So, the first case I already did, it's easy, the second works fine, but the problem is when I try to update for the second time, inside of my Regiontable it creates a new row instead of change the selected field.
My Code :
public HttpResponseMessage Update(UsersGetModel usersGetModel)
{
using (var context = new GoneDatabase())
{
try
{
var user = context.users.Where(a => a.idUser== usersGetModel.id).FirstOrDefault();
user.userName = usersGetModel.Name;
user.PhoneNumber = usersGetModel.PhoneNumber;
user.Region = new region{ regionName= usersGetModel.Region };
context.Entry(user).State = System.Data.Entity.EntityState.Modified;
context.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
It's creating a new row because you're literally telling it to by constructing a new Region instance at
user.Region = new region{ regionName= usersGetModel.Region };
Rather, you need to retrieve an existing Region instance from the DbContext and use that, like
user.Region = context.Set<Region>().Where(r => r.Name.Equals(usersGetModel.Region).SingleOrDefault();
A couple notes:
"SingleOrDefault" will throw an exception if there is more than one row in the Region table that matches the "Where" criteria
You need to put some constraints on your database tables to prevent #1 from happening, if business logic/specs indicate it

Use SugarCRM Beans to retrieve data

I needed to convert this SQL query to bean format.
"SELECT first_name, last_name FROM leads INNER JOIN contacts_leads_1_c ON
leads.id=contacts_leads_1_c.contacts_leads_1leads_idb where
contacts_leads_1_c.contacts_leads_1contacts_ida='".$bean->id."'";
I have this already in place
$lead = new Lead();
$where = "contacts_leads_1_c.contacts_leads_1contacts_ida = '$bean->id' ";
$lead_list = $lead->get_full_list("", $where,true);
$all_leads = array();
foreach($all_leads as $leads){
$bean->contacts_leads = $ref->first_name . ',' . $ref->last_name;
This is the problem
$lead_list = $lead->get_full_list("", $where,true);
Thanks in advance,
If $bean is already a contact you can grab a list of the related fields:
$leads = $bean->get_linked_beans('FIELDNAME','Leads');
FIELDNAME can be found in the vardefs and isn't the relationship name it's the name of the field that defines the link.
Note: In your example you're creating an empty array then looping over it.

How to change the database field orders in zend

In zend project I fetch data from the database and show in a table. It shows data in the same order as stored in the database. But I want to show the fileds in diffeent order than in database order. But I don't know how to do thid. Here I means field order not the row order.
Please help me in this regard.
Thanks
Its hard to say how youre displaying but lets assume you have made Table Classes for each table i would do something like this:
<?php foreach $zendDbRowObject->getTable()->getDisplayOrder() as $fieldName): ?>
<?php echo $zendDbRowObject->$fieldName; ?>
<?php endforeach; ?>
So then in your Table clas for a particular table you can create a property/method to get the fields in the order you want them in:
public function getDisplayOrder() {
// fake column names obviously... use yours here.
return array(
'column5',
'column1',
'column4',
'column2',
'column3'
);
}
when you do db select, you can select the coloumns that you want, so instead of select *,
you do select col5,col3,col2,col6,col1 from tablename
This will change the order of the columns
$select = $db->select()
->from(array('t' => 'table'),
array('t.col2', 'p.col1`'));

Zend Query Select

Hi i need to do a simple query but something is wrong. I have $name and $surname and i need to search the (possible multiple) id that rappresent that name and surname and put all the id, name and surname in a array
I do this query:
$result=$this->_db_table->select()->where('name=?',$name)
->where('surname=?', $surname)->query()
->fetchAll();
$array=$result->toArray();
return $array;
If i use
$result=$this->_db_table->fetchAll();
$array=$result->toArray();
return $array
it work correctly and i have an array whith all the value in the database in that table. What is wrong in my first code???
After doing this
$result=$this->_db_table->select()->where('name=?',$name)
->where('surname=?', $surname)->query()
->fetchAll();
$result is already an array its not an object . So simply use it instead of calling toArray on it.
Correct code wd be
$result=$this->_db_table->select()->where('name=?',$name)
->where('surname=?', $surname)->query()
->fetchAll();
return $result;