ApiPlatform Regex property filter with Postgresql - postgresql

I'm trying to add a custom filter to an entity in my ApiPlatform project that allows me to filter on specific property given a regex pattern.
Following the ApiPlatform documentation I came up with the following class (this is a near copy of the their example, only the where-clause is different):
<?php
namespace App\Filter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
final class RegexFilter extends AbstractContextAwareFilter
{
protected function filterProperty(
string $property,
$value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
string $operationName = null
) {
// otherwise filter is applied to order and page as well
if (
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass)
) {
return;
}
$parameterName = $queryNameGenerator->generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters
$queryBuilder
->andWhere(sprintf('(o.%s ~ :%s) = true', $property, $parameterName))
->setParameter($parameterName, $value);
}
// This function is only used to hook in documentation generators (supported by Swagger and Hydra)
public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}
$description = [];
foreach ($this->properties as $property => $strategy) {
$description["regexp_$property"] = [
'property' => $property,
'type' => 'string',
'required' => false,
'swagger' => [
'description' => 'Filter using a regex. This will appear in the Swagger documentation!',
'name' => 'Custom name to use in the Swagger documentation',
'type' => 'Will appear below the name in the Swagger documentation',
],
];
}
return $description;
}
}
When I run my code this results in the following DQL:
SELECT o FROM App\Entity\Vehicle o WHERE (o.licensePlate ~ :licensePlate_p1) = true ORDER BY o.id ASC
However I cannot get the Lexer to understand the tilde ~ character:
Doctrine\ORM\Query\QueryException
[Syntax Error] line 0, col 56: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '~'
How can I make Doctrine understand the tilde?

Turns out I was pretty close, simPod's answer helped me fill the gaps.
I copied his custom DQL function and added it in my Doctrine yaml configuration
In my RegexFilter I had to slightly modify the where-clause:
->andWhere(sprintf('REGEX(o.%s, :%s) = true', $property, $parameterName))

Related

Sonata/Symfony using MongoDB filtering for Null

I'm trying to customize a filter in sonata admin that returns all the objects that have null on one specific field... In MySQL, there was a function that I used to get the null values which were the is null.
I'm trying to do the same but using mongo this time it gives an error that the isnull function is not defined. Is there any function similar to that in the form to use in odm?
My code is the following:
->add("isRoot", 'doctrine_mongo_callback', array(
'callback' => function ($queryBuilder, $alias, $field, $value) {
/**
* #var QueryBuilder $queryBuilder
*/
if ($value['value']) {
if ($value['value'] == 0) {
$queryBuilder->andWhere($queryBuilder->expr()->isNull($alias.'.mainCategory'));
return true;
} else {
$category = $this->getConfigurationPool()->getContainer()->get('doctrine_mongodb.odm.document_manager')->getReference(ArticleCategory::class, $value['value']);
$queryBuilder->andWhere($queryBuilder->expr()->eq($alias.'.category', $category));
return true;
}
}
},
'field_type' => ChoiceType::class,
'field_options' => array(
'choices' => $this->getCategoryChoices()
),
'label' => 'mainCategory'
));
}
Is there a function in ODM similar to isNull in ORM?
the following is the error:
Attempted to call an undefined method named "isNull" of class "Doctrine\ODM\MongoDB\Query\Expr".
So I figured how to query instead of
$queryBuilder->andWhere($queryBuilder->expr()->isNull($alias.'.mainCategory'));
Using the following:
$queryBuilder->field('mainCategory')->equals(null);
I saw this solution on the following link: enter link description here

TYPO3 TCA make the 'default' value dynamic

The title is rather self explanatory, but what i would like to have is a dynamic default value.
The idea behind it is to get the biggest number from a column in the database and then add one to the result. This result should be saved as the default value.
Lets take for example this code:
$GLOBALS['TCA'][$modelName]['columns']['autojobnumber'] = array(
'exclude' => true,
'label' => 'LLL:EXT:path/To/The/LLL:tx_extension_domain_model_job_autojobnumber',
'config' => [
'type' => 'input',
'size' => 10,
'eval' => 'trim,int',
'readOnly' =>1,
'default' => $result,
]
);
The SQL looks like this:
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_extension_domain_model_job');
$getBiggestNumber = $queryBuilder
->select('autojobnumber')
->from('tx_extension_domain_model_job')
->groupBy('autojobnumber')
->orderBy('autojobnumber', 'DESC')
->setMaxResults(1)
->execute()
->fetchColumn(0);
$result = $getBiggestNumber + 1;
So how can i do that "clean"?
I thought about processCmdmap_preProcess but i dont know how to pass the value to the coorisponding TCA field. Plus i do not get any results on my backend when i use the DebuggerUtility like i get them when i use processDatamap_afterAllOperations after saving the Object.
Can someone point me to the right direction?
I don't think it is supported to create a dynamic default value, see default property of input field.
What you can do however, is to create your own type (use this instead of type="input"). You can use the "user" type. (It might also be possible to create your own renderType for type="input", I never did this, but created custom renderTypes for type= "select").
You can look at the code of InputTextElement, extend that or create your own from scratch.
core code: InputTextElement
documentation for user type
more examples in FormEngine documentation
Example
(slightly modified, from documentation)
ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][<current timestamp>] = [
'nodeName' => 'customInputField',
'priority' => 40,
'class' => \T3docs\Examples\Form\Element\CustomInputElement::class,
];
CustomInputElement
<?php
declare(strict_types = 1);
namespace Myvendor\MyExtension\Backend\FormEngine\Element\CustomInputElement;
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
// extend from AbstractFormElement
// alternatively, extend from existing Type and extend it.
class CustomInputElement extends AbstractFormElement
{
public function render():array
{
$resultArray = $this->initializeResultArray();
// add some HTML
$resultArray['html'] = 'something ...';
// ... see docs + core for more info what you can set here!
return $resultArray;
}
}

Search for a Keyword through All the Properties of an Entity, Symfony2

I am using the Lexik Form Filter Bundle to filter a result set from an entity repository.
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Lexik\Bundle\FormFilterBundle\Filter\Form\Type as Filters;
class ItemFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', Filters\TextFilterType::class)
->add('description', Filters\TextFilterType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
'validation_groups' => array('filtering')
));
}
public function getBlockPrefix()
{
return 'item_filter';
}
}
As per the default behavior of this filter, the final query which is built after attaching the filter conditions, will have the following WHERE condition structure.
SELECT * FROM AppBundle\Entity\Item a WHERE
a.name = 'nameValue'
AND a.description = 'descriptionValue'
(Assume the conventional directory structure)
My task is to add one more field to the filter form. This will not be attached to any particular property. Upon submission of the filter form, this input has to be searched through all the properties of the entity. In other words, the resulting query has to be something like this.
SELECT * FROM AppBundle\Entity\Item a WHERE
a.name LIKE '%nameValue%'
OR a.description LIKE '%descriptionValue%'
(Please note the keywords OR and LIKE here.)
I prefer to adhere to the way Lexik Filter maintains the filter data (using the session), so it is better if I can achieve this using this filter. But I am clueless about what type of custom field I need to create and how I can alter the condition builder logic. Since this keyword is not attached to a specific property, is this even possible using this filter?
I think you are looking for the 'apply_filter' option of all filter types.
Documentation can be seen here Lexik Form Filters - Customize Condition Operators
My keyword filter looks like this, searching on 5 fields; $fields
->add('keyword', TextFilterType::class, array(
'required' => false,
'attr' => array(
'placeholder' => 'Keyword..'
),
'apply_filter' => function(QueryInterface $filterQuery, $field, $values) {
if($values['value'] === null || $values['value'] === '') {
return null;
}
/** #var Expr $expr */
$expr = $filterQuery->getExpr();
$fields = [
'exhibitorName',
'exhibitorStandNumber',
'address.city',
'address.postcode',
'address.country',
];
$params = [];
$expression = $expr->orX(); ///andX for must match all
foreach($fields as $field) {
$paramName = sprintf('p_%s', str_replace('.','_', $field));
$ex = $expr->like($field, ':' . $paramName);
$expression->add($ex);
$params[$paramName] = '%' . $values['value'] . '%';
}
return $filterQuery->createCondition($expression, $params);
}
))

Yii CJuiAutoComplete for Multiple values

I am a Yii Beginner and I am currently working on a Tagging system where I have 3 tables:
Issue (id,content,create_d,...etc)
Tag (id,tag)
Issue_tag_map (id,tag_id_fk,issue_id_fk)
In my /Views/Issue/_form I have added a MultiComplete Extension to retrieve multiple tag ids and labels,
I have used an afterSave function in order to directly store the Issue_id and the autocompleted Tag_ids in the Issue_tag_map table, where it is a HAS_MANY relation.
Unfortunately Nothing is being returned.
I wondered if there might be a way to store the autocompleted Tag_ids in a temporary attribute and then pass it to the model's afterSave() function.
I have been searching for a while, and this has been driving me crazy because I feel I have missed a very simple step!
Any Help or advices of any kind are deeply appreciated!
MultiComplete in Views/Issue/_form:
<?php
echo $form->labelEx($model, 'Tag');
$this->widget('application.extension.MultiComplete', array(
'model' => $model,
'attribute' => '', //Was thinking of creating a temporary here
'name' => 'tag_autocomplete',
'splitter' => ',',
'sourceUrl' => $this->createUrl('Issue/tagAutoComplete'),
// Controller/Action path for action we created in step 4.
// additional javascript options for the autocomplete plugin
'options' => array(
'minLength' => '2',
),
'htmlOptions' => array(
'style' => 'height:20px;',
),
));
echo $form->error($model, 'issue_comment_id_fk');
?>
AfterSave in /model/Issue:
protected function afterSave() {
parent::afterSave();
$issue_id = Yii::app()->db->getLastInsertID();
$tag; //here I would explode the attribute retrieved by the view form
// an SQL with two placeholders ":issue_id" and ":tag_id"
if (is_array($tag))
foreach ($tag as $tag_id) {
$sql = "INSERT INTO issue_tag_map (issue_id_fk, tag_id_fk)VALUES(:issue_id,:tag_id)";
$command = Yii::app()->db->createCommand($sql);
// replace the placeholder ":issue_id" with the actual issue value
$command->bindValue(":issue_id", $issue_id, PDO::PARAM_STR);
// replace the placeholder ":tag_id" with the actual tag_id value
$command->bindValue(":tag_id", $tag_id, PDO::PARAM_STR);
$command->execute();
}
}
And this is the Auto Complete sourceUrl in the Issue model for populating the tags:
public static function tagAutoComplete($name = '') {
$sql = 'SELECT id ,tag AS label FROM tag WHERE tag LIKE :tag';
$name = $name . '%';
return Yii::app()->db->createCommand($sql)->queryAll(true, array(':tag' => $name));
actionTagAutoComplete in /controllers/IssueController:
// This function will echo a JSON object
// of this format:
// [{id:id, name: 'name'}]
function actionTagAutocomplete() {
$term = trim($_GET['term']);
if ($term != '') {
$tags = issue::tagAutoComplete($term);
echo CJSON::encode($tags);
Yii::app()->end();
}
}
EDIT
Widget in form:
<div class="row" id="checks" >
<?php
echo $form->labelEx($model, 'company',array('title'=>'File Company Distrubution; Companies can be edited by Admins'));
?>
<?php
$this->widget('application.extension.MultiComplete', array(
'model' => $model,
'attribute' => 'company',
'splitter' => ',',
'name' => 'company_autocomplete',
'sourceUrl' => $this->createUrl('becomEn/CompanyAutocomplete'),
'options' => array(
'minLength' => '1',
),
'htmlOptions' => array(
'style' => 'height:20px;',
'size' => '45',
),
));
echo $form->error($model, 'company');
?>
</div>
Update function:
$model = $this->loadModel($id);
.....
if (isset($_POST['News'])) {
$model->attributes = $_POST['News'];
$model->companies = $this->getRecordsFromAutocompleteString($_POST['News']
['company']);
......
......
getRecordsFromAutocompleteString():
public static cordsFromAutocompleteString($string) {
$string = trim($string);
$stringArray = explode(", ", $string);
$stringArray[count($stringArray) - 1] = str_replace(",", "", $stringArray[count($stringArray) - 1]);
$criteria = new CDbCriteria();
$criteria->select = 'id';
$criteria->condition = 'company =:company';
$companies = array();
foreach ($stringArray as $company) {
$criteria->params = array(':company' => $company);
$companies[] = Company::model()->find($criteria);
}
return $companies;
}
UPDATE
since the "value" porperty is not implemented properly in this extension I referred to extending this function to the model:
public function afterFind() {
//tag is the attribute used in form
$this->tag = $this->getAllTagNames();
parent::afterFind();
}
You should have a relation between Issue and Tags defined in both Issue and Tag models ( should be a many_many relation).
So in IssueController when you send the data to create or update the model Issue, you'll get the related tags (in my case I get a string like 'bug, problem, ...').
Then you need to parse this string in your controller, get the corresponding models and assigned them to the related tags.
Here's a generic example:
//In the controller's method where you add/update the record
$issue->tags = getRecordsFromAutocompleteString($_POST['autocompleteAttribute'], 'Tag', 'tag');
Here the method I'm calling:
//parse your string ang fetch the related models
public static function getRecordsFromAutocompleteString($string, $model, $field)
{
$string = trim($string);
$stringArray = explode(", ", $string);
$stringArray[count($stringArray) - 1] = str_replace(",", "", $stringArray[count($stringArray) - 1]);
return CActiveRecord::model($model)->findAllByAttributes(array($field => $stringArray));
}
So now your $issue->tags is an array containing all the related Tags object.
In your afterSave method you'll be able to do:
protected function afterSave() {
parent::afterSave();
//$issue_id = Yii::app()->db->getLastInsertID(); Don't need it, yii is already doing it
foreach ($this->tags as $tag) {
$sql = "INSERT INTO issue_tag_map (issue_id_fk, tag_id_fk)VALUES(:issue_id,:tag_id)";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":issue_id", $this->id, PDO::PARAM_INT);
$command->bindValue(":tag_id", $tag->id, PDO::PARAM_INT);
$command->execute();
}
}
Now the above code is a basic solution. I encourage you to use activerecord-relation-behavior's extension to save the related model.
Using this extension you won't have to define anything in the afterSave method, you'll simply have to do:
$issue->tags = getRecordsFromAutocompleteString($_POST['autocompleteAttribute'], 'Tag', 'tag');
$issue->save(); // all the related models are saved by the extension, no afterSave defined!
Then you can optimize the script by fetching the id along with the tag in your autocomplete and store the selected id's in a Json array. This way you won't have to perform the sql query getRecordsFromAutocompleteString to obtain the ids. With the extension mentioned above you'll be able to do:
$issue->tags = CJSON::Decode($_POST['idTags']);//will obtain array(1, 13, ...)
$issue->save(); // all the related models are saved by the extension, the extension is handling both models and array for the relation!
Edit:
If you want to fill the autocomplete field you could define the following function:
public static function appendModelstoString($models, $fieldName)
{
$list = array();
foreach($models as $model)
{
$list[] = $model->$fieldName;
}
return implode(', ', $list);
}
You give the name of the field (in your case tag) and the list of related models and it will generate the appropriate string. Then you pass the string to the view and put it as the default value of your autocomplete field.
Answer to your edit:
In your controller you say that the companies of this model are the one that you added from the Autocomplete form:
$model->companies = $this->getRecordsFromAutocompleteString($_POST['News']
['company']);
So if the related company is not in the form it won't be saved as a related model.
You have 2 solutions:
Each time you put the already existing related model in you autocomplete field in the form before displaying it so they will be saved again as a related model and it won't disapear from the related models
$this->widget('application.extensions.multicomplete.MultiComplete', array(
'name' => 'people',
'value' => (isset($people))?$people:'',
'sourceUrl' => array('searchAutocompletePeople'),
));
In your controller before calling the getRecordsFromAutocompleteString you add the already existing models of the model.
$model->companies = array_merge(
$model->companies,
$this->getRecordsFromAutocompleteString($_POST['News']['company'])
);

Zend form populate method

I have a Zend form below is the code
public function editAction()
{
try
{
$data = Zend_Auth::getInstance()->getStorage()->read();
$this->view->UserInfo = $data['UserInfo'];
$this->view->Account = $data['Account'];
$UserEditForm = $this->getUserEditForm();
$this->view->UserEditForm = $UserEditForm;
$params = $this->_request->getParams();
if ($params['user'])
{
$UserResult = $this->_user_model->getUserData($params['user']);
$UserAddressResult = $this->_user_model->getAddressData($params['user']);
$UserInfo = Array(
'UserId' => $UserResult['user_id'],
'EmailAddress' => $UserResult['email'],
'UserName' => $UserResult['username'],
'Title' => $UserResult['Title'],
'FirstName' => $UserResult['firstname'],
'LastName' => $UserResult['lastname'],
'Gender' => $UserResult['gender'],
'DateOfBirth' => date('m/d/Y', strtotime($UserResult['dateofbirth'])),
'AddressLine1' => $UserAddressResult['address_1'],
'AddressLine2' => $UserAddressResult['address_2'],
'City' => $UserAddressResult['city'],
'State' => $UserAddressResult['state_id'],
'PostalCode' => $UserAddressResult['postcode'],
'Country' => $UserAddressResult['country_id'],
'CompanyName' => $UserAddressResult['company'],
'WorkPhone' => $UserAddressResult['workphone'],
'HomePhone' => $UserAddressResult['homephone'],
'Fax' => $UserAddressResult['fax'],
'IsDashboardUser' => $UserResult['is_dashboard_user']
);
$UserEditForm->populate($UserInfo);
$this->view->UserEditForm = $UserEditForm;
}
if ($this->_request->isPost())
{
$values = $this->_request->getPost();
unset($values['Save']);
if ($UserEditForm->isValid($values))
{
$Modified_date = date('Y-m-d H:i:s');
$UserData = $this->_user_model->CheckEmail($values['EmailAddress']);
$UpdateData = $this->_user_model->UpdateUserData($UserData['user_id'], $values, $Modified_date, $data['UserId']);
if ($UpdateData != null)
{
return $this->_helper->redirector('userlist','index','user');
}
}
}
}
catch (Exception $exception)
{
echo $exception->getCode();
echo $exception->getMessage();
}
}
Because my form elements names are different then my table field names i have to declare a Array $UserAddressResult see above code to match the table field name with form element name.
Is there a another way to populate form without declaring this array.
Please don't suggest that i have to keep my table field name and form element name same. I cannot do that as per our naming convention standards.
Your naming convention is inconsistent. If it were consistent, you could perhaps use a simple regular expression to transform column names. But you can't, so you will have to do it manually one way or the other.
If you need to construct this array of values in more than one place in your code, you should consider moving the logic to the form itself (extend it with a public function populateFromUserAndResult($userResult, $userAddress)) or an action helper.
We decided to keep same name of form elements as per our table fields name. For now this is the only solutions i feel. if i come across any other solutions in future will update here...
Thanks for all your replies....