With CakePHP 3, displayValue option allows you to change the field which will be displayed in your view, for example a name instead of an id.
find('list', [
'keyField' => 'no_user',
'order' => ['Users.name' => 'asc'],
'valueField' => ['name']
]);
In my example above, I want to display name and first name, but I can't find how to properly use valueField to display multiple fields.
I tried :
'valueField' => ['name', 'first_name']
The output is name;first_name in my view.
The book only gives an example with a single field.
How can I format it to have name first_name ?
Thanks in advance
Take a look at virtual properties: http://book.cakephp.org/3.0/en/orm/entities.html#creating-virtual-properties.
Looking at http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs could say you can use virtual properties.
Related
unfortunately documentation doesn't cover how to use options available in configureListFields ListMapper when you add fields to the list.
This is my basic code
$listMapper
->add('myField', null, array(
'label' => LabelHelper::LABEL_MY_FIELD,
'code' => // what should I put here ... $this->methodName() is not working
))
I want to use 'code' option (docs - section 7.2.1), because I would like to customize just one filed final display. I don't want to rewrite the row template.
As stated in the code section I've tried simple method that returns string, but nothing happened in the list view (I've cleared cache etc.).
Answer is simple. You just put method name without brackets
$listMapper
->add('myField', null, array(
'label' => LabelHelper::LABEL_MY_FIELD,
'code' => 'methodName'
))
Method should be stored in corresponding Entity class
I'm trying to log each action (insert/update/delete) in the application and I'm doing this by getting the dirty and original values after saving the entity. The problem is that all values of the associated entities are returned as dirty and even is_new flag is set to true but actually I'm updating. What causes this behavior and how can I avoid it?
Example:
$data = [
'name' => $name,
'something' => $something,
'Table1' => [
'id' => $idWhereUpdatingTable1,
'field1' => $field1,
'field2' => $field2,
],
'Table2' => [
'id' => $idWhereUpdatingTable2,
'field3' => $field3,
'field4' => $field4,
],
];
$options = ['associated' => ['Table1', 'Table2']];
$updatedEntity = $this->patchEntity($entity, $data, $options);
$save = $this->save($updatedEntity);
// Successfully logging the changes in the main entity
// Trying to log the changes in the associated entities
foreach($save->table1 as $entity)
{
// everything here is set to dirty (even ID field but it's not an insert) and I'm not able to fetch the updated fields only. Also getOriginal() doesn't return the old values.
}
I did some digging into the dirty() function within an Entity and according to the API if you do not explicitly ask it to check a property then it will just tell you if the Entity has any dirty properties.
So doing
$entity->dirty('title'); Tells you if the tile is dirty but running $entity->dirty(); will just tell you if any property in the entity is dirty.
http://api.cakephp.org/3.1/class-Cake.ORM.Entity.html#_dirty
You may want to make code conditional based on whether or not fields have changed in an entity.
For example, you may only want to validate fields when they change:
// See if the title has been modified. CakePHP version 3.5 and above
$entity->isDirty('title');
// CakePHP 3.4 and Below use dirty()
$entity->dirty('title');
In Rails, we can specify the allowed parameters to be used in the controller when saving data. So, with params being the submitted data, I can do this:
params.require(:person).permit(:name, :age)
Which will ensure that the :person key is present and will filter out anything that is not a person's :name or :age.
Is there any way in CakePHP to accomplish this?
EDIT: I know I can write PHP, I want to know if there's a Cake component / plugin that already does this.
Something in this PHP way:
// submited data
$this->request->data['Person'] = array(
'name' => 'Salines',
'age' => '41',
'job' => 'Web Developer'
);
// check if isset and filter out anything that is not a person's name or age
if(isset($this->request->data['Person']))
{
$permit = array('name' => '','age' => '');
$this->request->data['Person'] = array_intersect_key($this->request->data['Person'],$permit);
}
//and return $this->request->data like
array(
'Person' => array(
'name' => 'Salines',
'age' => '41'
)
);
I'm looking for a Cake-provided solution (if there is one)
Well, define "cake-provided", you mean by the framework itself? No, the core doesn't have this functionality but there are two plugins.
For Cake3: Plum Search
For Cake2 & 3: CakeDC Search
For Cake3 I would go for Plum-Search, it is written by the same person as the initial code of the other plugin but a complete rewrite and makes a better use of Cake3.
Next time you ask name your exact Cake version.
Both plugins implement the PRG pattern but don't explicitly allow or deny query parameters. They'll only grab the parameters you specified in your filter declaration and turn them into the request. Validate and save to exclude unwanted fields.
Make a url link like this
echo $this->Html->url(array('controller'=>'users','action'=>'hello','par1'=>23,'par2'=>'sud'));
In hello function in users controller
pr($this->params->named['par1']);
pr($this->params->named['par2']);
I am using CakePHP2.4 and the search plugin https://github.com/CakeDC/search
I have the following
Employee hasOne EmployeeProfile
Employee hasMany Qualification
So I have a single search bar.
the search bar will search using LIKE through the following fields
Employee.name
EmployeeProfile.email
Qualification.title
how do I configure the model Employee->filterArgs for this search?
This is a cross-posting of the original issue here
The documentation includes an example.
'username' => array('type' => 'like', 'field' => array('User.username', 'UserInfo.first_name')),
You just have to make sure the models you're calling are available in your find() call. In the example the find will do a like on the users table username and on the user_infos first_name field at the same time.
I'd like to expand on this as I've been trying to setup a search on a hasMany relationship for a few hours and couldn't find anything. Mark mentionned "custom bindModel (as hasOne) for hasMany relationship (Qualification)". Here's how it's done :
$this->Employee->bindModel(array(
'hasOne' => array(
'Qualification' => array(
'foreignKey' => false,
'conditions' => array('Employee.id = Qualification.employee_id')
)
)
), false);
Just bind it before your paginate and add Qualification.title in your field list in your filterArgs
I have the following line in my add/edit Course views:
echo $this->Form->input(
'Competency',
array(
'label' => 'Which competencies does this course address?',
'multiple' => 'checkbox'
)
);
(Modelled as Course hasMany Competencies)
I'd like to customise the label that gets output for each competency but can't seem to find a way of doing so - the 'label' field seems to work as a group heading rather than changing the label for the individual checkboxes.
(What I'm ideally after is rather than just displaying Competency.name I can display Competency.name plus the Competency.code as the label)
NB I thought about changing the displayField but that would change it everywhere and it's only here I'd like to be different.
Create a virtual field [details]:
//in your Competency model
var $virtualFields = array(
'name_code' => 'CONCAT(Competency.name, " ", Competency.code)'
);
Then in your controller, before retrieving the data, set your displayField to your just-created virtual field:
//in your controller prior to the find
$this->Competency->displayField = 'name_code';
Since you're setting displayField in the controller, it doesn't set it permanently, so no need to set it back, but if you're doing more finds immediately after this, you can always set back to name if you want.