Show custom field in query list in cakephp3 - cakephp

I want to create a dropdown with custom field but my list query append id field into the query. How to show only selected fields in my query.
$this->loadModel('CardTypes');
$cardTypes = $this->CardTypes->find('list')->select(['code', 'name']);
In my view
$this->Form->select('card_type_id', $cardTypes, [ 'default' => 'DELTA']);

see the manual
$cardTypes = $this->CardTypes->->find('list', [
'keyField' => 'code',
'valueField' => 'name'
]);

Related

Solr and TYPO3 faceting

I've created a field called "international" in my TYPO3 project. This field is a checkbox and is displayed on my backend with an associated label.
'international' => [
'exclude' => FALSE,
'label' => 'LLL:EXT:projects/Resources/Private/Language/locallang_db.xlf:tx_projects_domain_model_requestprojects.international',
'config' => [
'type' => 'check',
'items' => [
// label, value
['LLL:EXT:projects/Resources/Private/Language/locallang_db.xlf:tx_projects_domain_model_requestprojects.international.items.1', 1],
],
]
],
If I go to my frontend, the faceting is indeed showing the field as link to filter my content thanks to this :
fieldRenderingInstructions {
international =< plugin.tx_solr.search.faceting.facets.international.renderingInstruction
international.field = international_intS
}
faceting {
facets.international {
label.data = LLL:EXT:skin/Resources/Private/Language/locallang.xlf:tx_projects_domain_model_requestprojects.international
field = international_intS
renderingInstruction = TEXT
renderingInstruction {
field = optionValue
wrap = {LLL:EXT:projects/Resources/Private/Language/locallang_db.xlf:tx_projects_domain_model_requestprojects.international.items.|}
insertData = 1
}
}
}
Starting with this, I have 2 questions:
Why does my content which have the field "international" not selected are triggering an "extra faceting" this the name of my root page?
If I have 2 "international" and 3 "international" not checked, my facets returns "international (2)" and "root page (3)" ? (the 3 have an empty value)
How can I hide this facet, but keep getting the value that was set to my content ?
I need to hide this facet on front (but probably I will have to fix the "bug" from 1), but keep the given value.
That was quite simple, actually.
Just added this in my facet
includeInAvailableFacets = 0

CakePHP pagination, how do I sort by the count of a contained model?

Using CakePHP 2.3, I'm retrieving data using a paginator. So, say my models are Countries having many Cities, and in my CountryController I have...
$this->Paginator->settings = [
'fields' => [
'id'
'country_name'
],
'contain' => [
'City' => [
'id'
'city_name',
'conditions' => [
'population >' => 1000000;
]
]
]
];
...which gets me a list of all counties with each row containing a list of any populous cities.
In the view I am obviously able to iterate foreach ($cities as $city) and echo $country['country_name'] etc. and also if I wish I can show a count of the contained cities by echoing count($country['City']).
Using the paginator I can sort the country results by passing back a field name in the query string, e.g. sort=country_name, but how can I get the results to sort by the count of the contained cities?
It is unfortunately not possible to sort by the count of the hasMany Table using the custom Cakephp Pagination. Your best bet is to use counterCache as described in the Docs.
You will need to have a field in country table, named as city_count. This field will be updated in the Country Table automatically by Cakephp whenever there is a save operation on city table.
Since you only want to count the cities with population > 100K. You can specify the condition in counterScope which will only update the column when condition is met.
This can be defined in your City Model as below:
class City extends AppModel {
public $belongsTo = array(
'Country' => array(
'counterCache' => true,
'counterScope' => array(
'City.population > ' => 1000000
)
)
);
}

CakePHP 3 hasMany update strange behaviour

I have JobsTable:
This is relation definition:
$this->hasMany( 'JobContracts', [
'foreignKey' => 'job_id'
] );
Saving code:
$entity = $this->patchEntity( $entity, $toSave, [
'fieldList' => ['notes],
'associated' => [
'JobContracts' => ['fieldList' => ['id', 'checked']]
]
] );
And now:
if I put this notes in fieldList then JobContracts are NOT saved properly.
If I remove fieldList, then I am able to save it properly.
Question is Why? I need to control base model fields also. Any suggestions?
Ive already checked: http://book.cakephp.org/3.0/en/orm/saving-data.html#avoiding-property-mass-assignment-attacks
You need to allow assigning the association property too, not only notes. If you don't, then the associated data is never going to be set on the resulting entity, and consequently is not going to be saved.
Check the docs that you've linked again, the tags example shows exactly that:
// Only allow changing the title and tags
// and the tag name is the only column that can be set
$entity = $this->patchEntity($entity, $data, [
'fieldList' => ['title', 'tags'],
'associated' => ['Tags' => ['fieldList' => ['name']]]
]);
$this->save($entity);
http://book.cakephp.org/3.0/en/orm/saving-data.html#avoiding-property-mass-assignment-attacks
So, add job_contracts to the field list, and you should be good.

Cakephp3 Saving Multiple dropdown selections to database

I have a form with a dropdown that is multiple => true
echo $this - > Form - > input('test_id', [
'options' => $tests,
'required' => true,
'empty' => 'Select Tests',
'multiple' => true
]);
But when i submit the form only one value is saved in database.
I have searched for solution and found this:
Multiple select in input Cakephp
It suggests using SaveMany in my Controller, but i can't figure it out how to use it to get my desired output.
I'm very new to this framework so any help would be appreciated.
sorry i m also new but as other answers and question i don't see 'selected' => $selected in your form
CakePHP 2.0 Select form Mulitple selected
and How to set selection in mulitiselect list box in cakephp

get the products collection filter by attributes with array of values

i want to get the products collection filter by attributes with array of values in magento.
eg:get collection of products whose color from array('red','blue','green') or brand from array('x','y','z').....
note: the attribute filter will not be the intersection .I have mentioned OR.So it will be joined to form final products collection
You can try something like this :
$productCollection =Mage::getModel('catalog/product')->getCollection()
$productCollection ->addFieldToFilter(
array('color', 'brand'),
array(
array('in' => array('red', 'blue', 'green')),
array('in' => rray('x','y'))
)
);
But color is a select attribute you should pass option's id of red/blue/green values
Take a look # code to filter product by attribute by color in magento and Magento - Wiki - Using Collections in Magento
$filter = array(
'attribute' => 'color',
'in' => array('red', 'blue', 'green'), // you may need get the code id of each color
),
array(
'attribute' => 'brand',
'in' => array('x','y'),
),
));
$productCollection =Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter($filter)

Resources