Sets a collection of query parameters for the query being constructed - doctrine-orm

I'm trying to build a query by using array of options, as you see this is my array options:
$sort = $this->getParam('sort', 'creation_date');
$order = $this->getParam('order', 'desc');
$options = [
'sort' => [$sort => $order],
'filters' => [
[
'field' => 'state',
'operator' => 'LIKE',
'bind_name' => 'state1',
'value' => 'read',
'type' => 'OR'
],
[
'field' => 'state',
'operator' => 'LIKE',
'bind_name' => 'state2',
'value' => 'green'
]
]
];
As result i have this query:
SELECT COUNT(*) FROM `mytabel` `mytabel` INNER JOIN state_type t ON t.id_state_type = `mytabel`.id_state_type WHERE (`mytabel`.`state` LIKE :state1) **AND** (`mytabel`.`state` LIKE :state2) ORDER BY `mytabel`.`creation_date` desc
Params
state1 %read%
state2 %green%
My question is how i can have OR instead of AND in my query (array options)? because as result i want to have both of them (state1 %read% ,state2 %green%).
Thank you,

Related

Set values for default address fields in Drupal 8

I need to set values for default address fields(langcode, country_code, administrative_area, address_locality ect.) when I create a node. I used below code in the submitForm function of a Form class which is extends by Drupal\Core\Form\FormBase class. But it not works for me.
$venueNode = Node::create([
'type' => 'venue',
'title' => 'Venue',
'field_address' => [
'country_code' => 'US',
'address_line1' => '1098 Alta Ave',
'locality' => 'Mountain View',
'administrative_area' => 'US-CA',
'postal_code' => '94043',
],
]);
$venueNode->save();
I made a mistake here. There should be a 0 index for field_address. Therefore the code should be like below.
$venueNode = Node::create([
'type' => 'venue',
'title' => 'Venue',
'field_address' => [
0 => [
'country_code' => 'US',
'address_line1' => '1098 Alta Ave',
'locality' => 'Mountain View',
'administrative_area' => 'US-CA',
'postal_code' => '94043',
],
],
]);
$venueNode->save();

Laravel 5.5 - Validation of a string inside an object that is inside an array

I have the request:
`array:2 [
"alt_tags" => "1"
"name_pattern" => array:2 [
0 => "{"label":"Main Title","value":"main_title"}"
1 => "{"label":"Store Name","value":"store_name"}"
]
]`
In the class Request, I tried to validate the lines inside the object in the following way:
`$labels = \implode(',', [
'Main Title',
'Store Name',
]);
$values = \implode(',', [
'main_title',
'store_name',
]);
return [
'alt_tags' => 'required|boolean',
'name_pattern' => 'required|array|min:0|max:2',
'name_pattern.*.label' => "sometimes|string|in:{$labels}",
'name_pattern.*.value' => "sometimes|string|in:{$values}",
];`
But since the string is inside the object, then there is no way to get through the file_name_pattern. *.label:
data_get(request()->all(), 'name_pattern.0') // "{"label":"Main Title","value":"main_title"}"
data_get(request()->all(),'name_pattern.0.label'); // return NULL

How to create an entity reference field which will allow unlimited values in a configuration form?

public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this->config('category_dynamic_block.settings');
$form['section_title'] = array(
'#type' => 'textfield',
'#title' => $this->t('Section Title'),
'#description' => $this->t('Enter a Section Title'),
);
$form['term_name'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#selection_settings' => [
'target_bundles' => array('categories'),
],
'#title' => $this->t('Term Name'),
'#description' => $this->t('Enter a Category Vocab Term Name'),
);
$form['page_title'] = array(
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#selection_settings' => [
'target_bundles' => array('article'),
],
'#title' => $this->t('Page Title (' . $i . ')'),
'#description' => $this->t('Enter Page Title to be displayed'),
);
return $form;}
I'm creating a configuration form and I'm trying to find if there is a way in drupal 8 which will allow the user to enter multiple values for $form['page_title'] field.
This question (unlimited text fields with form api) may be what you are looking for: https://drupal.stackexchange.com/questions/208012/unlimited-textfields-with-form-api
Basically you'll need to add some ajax:
'#ajax' => [
'callback' => array($this, 'addMultipleItems'),
'event' => 'change',
'progress' => array(
'type' => 'throbber',
'message' => t('Adding another item...'),
),
],

How to customise the Regex validation messages in a Zend\Form keeping them reusable?

The default validation error message for Regex is
"The input does not match against pattern '%pattern%'"
I can replace it by a custom one like
"Please make an input according to the pattern '%pattern%'"
But then I still have a not really user-friendly message with the internal regex in it. I also can write
"Only capital letters are allowed"
But in this case I need to write a new message for every single regex field.
Is it possible / How to create own, but still flexible/reusable/parameterizable messages?
An example of what I want:
public function getInputFilterSpecification()
{
return [
'foo' => [
'validators' => [
[
'name' => 'Regex',
'options' => [
'pattern' => '/^[A-Z0-9:]*$/',
'pattern_user_friendly' => 'capital letters, numbers, and colons',
'message' => 'The input may only contain the following characters: %pattern_user_friendly%.'
]
],
]
],
'bar' => [
'validators' => [
[
'name' => 'Regex',
'options' => [
// new pattern
'pattern' => '/^[A-Z~:\\\\]*$/',
// new user friendly pattern description
'pattern_user_friendly' => 'capital letters, tildes, colons, and backslashes',
// still the same message
'message' => 'The input may only contain the following characters: %pattern_user_friendly%.'
]
],
]
],
];
}
The solution is to create a custom Validator (extending the Regex), to extend there the list of messageVariables, and to add the logic for setting the value as a property:
class Regex extends ZendRegex
{
protected $patternUserFriendly;
public function __construct($pattern)
{
// s. https://github.com/zendframework/zend-validator/blob/master/src/Regex.php#L34-L36
$this->messageVariables['patternUserFriendly'] = 'patternUserFriendly';
$this->messageTemplates[self::NOT_MATCH] =
'The input may only contain the following characters: %patternUserFriendly%.'
;
parent::__construct($pattern);
if (array_key_exists('patternUserFriendly', $pattern)) {
$this->patternUserFriendly = $pattern['patternUserFriendly'];
}
}
}
class MyFieldset extends ZendFieldset implements InputFilterProviderInterface
{
...
public function init()
{
parent::init();
$this->add(
[
'type' => 'text',
'name' => 'foo',
'options' => [
'label' => _('foo')
]
]);
$this->add(
[
'type' => 'text',
'name' => 'bar',
'options' => [
'label' => _('bar')
]
]);
}
public function getInputFilterSpecification()
{
return [
'bar' => [
'validators' => [
[
'name' => 'MyNamespace\Validator\Regex',
'options' => [
'pattern' => '/^[a-zA-z]*$/',
'patternUserFriendly' => '"a-z", "A-Z"'
]
]
]
]
];
}
}

ZF2 Doctrine 2 ObjectSelect with distinct on field

to populate my form I use the fieldset approach. For one given form field I will use a select and the options are coming directly from an entity like this:
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'city',
'options' => array(
'label' => 'City: ',
'object_manager' => $this->_om,
'target_class' => 'Hotbed\Entity\AllAdresses',
'property' => 'city',
'is_method' => true,
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('postal_code' => $postalCode),
'orderBy' => array('city' => 'ASC'),
),
),
),
'attributes' => array(
'class' => 'form-control input-large',
'required' => '*'
),
)
);
This works pretty well. The only inconvient is that I have to put a distinct on the field city. How can I solve this problem?
Regards Andrea
The way I got around this was to create a function in the repository to return the distinct entities, and then specify that function name in your form element.
So in your case, for example:
In your repository:
public function findDistinctCitiesByPostalCode($postalCode) {
$dql = "SELECT DISTINCT a.city "
. "FROM Hotbed\Entity\AllAdresses a "
. "WHERE a.postalCode :postalCode";
$qry = $this->getEntityManager()->createQuery($dql);
$qry->setParameter('postalCode', $postalCode);
$results = $qry->getArrayResult();
// $results will be an array in the format
// array(array('city' => 'city_1'), array('city' => 'city_1'),....)
// so you'll need to loop through and create an array of entities
foreach ($results as $row) {
$addresses[] = new Hotbed\Entity\AllAdresses(array('city' => $row['city']);
}
return $addresses;
}
And then in your form:
$this->add(
array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'city',
'options' => array(
'label' => 'City: ',
'object_manager' => $this->_om,
'target_class' => 'Hotbed\Entity\AllAdresses',
'property' => 'city',
'is_method' => true,
'find_method' => array(
'name' => 'findDistinctCitiesByPostalCode'
)
)
)
);