Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Workflow] Add tip on how to add a WHERE constraint on a multi-state marking store#20086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
richardhj wants to merge2 commits intosymfony:5.4
base:5.4
Choose a base branch
Loading
fromrichardhj:patch-2
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletionsworkflow.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -283,6 +283,15 @@ if you are using Doctrine, the matching column definition should use the type ``
this Doctrine type will store its value only as a string, resulting in the
loss of the object's current place.

.. tip::

On a database level, querying or filtering for multiple state marking stores
requires special handling. When using Doctrine with MySQL, you can install
`scienta/doctrine-json-functions` and enable the `JSON_CONTAINS_PATH` doctrine
function. Then you can filter for a current place as follows:
``$qb->andWhere("JSON_CONTAINS_PATH(item.currentPlaces, 'one', '$.draft') <> 0")``
(where `draft` is the place to be checked)
Copy link
Member

@lyrixxlyrixxAug 6, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't know if it works.

I personally use:

privatefunctionfilterByMarking(QueryBuilder$queryBuilder,array$markings,bool$and =false):void    {$entityAlias =$queryBuilder->getRootAliases()[0];$contains = [];foreach ($markingsas$marking) {$contains[] =$queryBuilder->expr()->eq(sprintf('JSON_CONTAINS(%s.marking, %s)',$entityAlias,$queryBuilder->expr()->literal(json_encode([$marking =>1], \JSON_THROW_ON_ERROR)),            ),1);        }if ($and) {$queryBuilder->andWhere($queryBuilder->expr()->andX(...$contains));        }else {$queryBuilder->andWhere($queryBuilder->expr()->orX(...$contains));        }$queryBuilder->andWhere($queryBuilder->expr()->isNotNull("{$entityAlias}.marking"));    }

With this class

useDoctrine\ORM\Query\AST\Functions\FunctionNode;useDoctrine\ORM\Query\AST\Node;useDoctrine\ORM\Query\Lexer;useDoctrine\ORM\Query\Parser;useDoctrine\ORM\Query\SqlWalker;class JsonContainsextends FunctionNode{privateconststringFUNCTION_NAME ='JSON_CONTAINS';public ?Node$jsonDocExpr =null;public ?Node$jsonValExpr =null;public ?Node$jsonPathExpr =null;publicfunctiongetSql(SqlWalker$sqlWalker):string    {$jsonDoc =$sqlWalker->walkStringPrimary($this->jsonDocExpr);$jsonVal =$sqlWalker->walkStringPrimary($this->jsonValExpr);$jsonPath ='';if ($this->jsonPathExpr) {$jsonPath =',' .$sqlWalker->walkStringPrimary($this->jsonPathExpr);        }returnsprintf('%s(%s, %s)',self::FUNCTION_NAME,$jsonDoc,"{$jsonVal}{$jsonPath}");    }publicfunctionparse(Parser$parser):void    {$parser->match(Lexer::T_IDENTIFIER);$parser->match(Lexer::T_OPEN_PARENTHESIS);$this->jsonDocExpr =$parser->StringPrimary();$parser->match(Lexer::T_COMMA);$this->jsonValExpr =$parser->StringPrimary();if ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) {$parser->match(Lexer::T_COMMA);$this->jsonPathExpr =$parser->StringPrimary();        }$parser->match(Lexer::T_CLOSE_PARENTHESIS);    }}

Anyway, before merging this PR,item.currentPlaces,one, and$.draft must be explained

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

BTW, PostgreSQL and mariadb have different option to deal with JSON

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not sure we should document this in detail. It looks too convoluted. If this is truly useful/common, could Symfony provide something to ease this? Otherwise, maybe we can just mention that you have to install some package or create your own JSON function to query this.

Not sure what to do here 😐


Accessing the Workflow in a Class
---------------------------------

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp