Concat three or more fields in doctrine - symfony

Doctrine query builder allows me to concat two fields only.
class Expr {
// ...
public function concat($x, $y); // Returns Expr\Func
To concatenate 3 fields I use:
$qb->expr()->concat(
'table.field1',
$qb->expr()->concat('table.field2', 'table.field3')
);
And the SQL will be:
CONCAT('table.field1', CONCAT('table.field2', 'table.field3'))
How to get one concat?
When I try to call directly
new Expr\Func('CONCAT', array('table.field1', 'table.field2', 'table.field3'));
Executing query gives me an error
[Syntax Error] line 0, col 237: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','
Dumping DQL:
CONCAT('table.field1', 'table.field2', 'table.field3')
Dumping SQL using $qb->getQuery()->getSQL():
[Syntax Error] line 0, col 237: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','

CONCAT with variable number of arguments has been introduced in Doctrine ORM version 2.4.0, so you are probably using an older version. I've tested a similar DQL in my project and works as expected.
You should upgrade your Doctrine ORM deps with:
php composer.phar require doctrine/orm:~2.4
Note that (for now) variable arguments are supported only by Expr\Func constructor, but is not supported in Expr::concat method; you still need this syntax:
new Expr\Func('CONCAT', array('table.field1', 'table.field2', 'table.field3'));
UPDATE I've created a PR on GitHub to support multiple arguments in Expr::concat

Related

expectExceptionMessageRegExp() function is deprecated in phpunit 9 . What's the other alternate function that i can use instead?

Am getting below issue after switching phpunit version from 7.1 to 9.5.28
Failed asserting that exception of type "Error" matches expected exception "Exception".
expectExceptionMessageRegExp() is deprecated.
expectExceptionMessageRegExp() is deprecated in PHPUnit 8 and will be removed in PHPUnit 9. Use expectExceptionMessageMatches() instead.
Example :
function testResponse(){
$this->expectExceptionMessageRegExp("/Unable to emit response/");
}
Replace with :
function testResponse(){
$this->expectExceptionMessageMatches("/Unable to emit response/");
}

Can't add a field to a model in Symfony, bin/console crashes

I'm working with Sylius framework. I'm following the guide to customize models.
I am trying to add a field notice to a model Taxon which is already overridden in my project. For that, I added the field description to Taxon.orm.yml of the model:
MyProject\Bundle\ShopBundle\Entity\Taxon:
type: entity
table: sylius_taxon
# {Relationships code...}
fields:
# {Some existing fields...}
notice:
type: text
nullable: true
I also added a field, a getter and a setter to the overriding Taxon class.
Then I'm trying to run bin/console doctrine:migrations:diff, but when I run bin/console even without any arguments, it crashes with the following exception:
[Doctrine\DBAL\Exception\InvalidFieldNameException]
An exception occurred while executing 'SELECT s0_.code AS code_0, s0_.tree_left AS tree_left_1, s0_.tree_right AS tree_right_2, s0_.tree_level AS tree_level_3, s0_.position AS position_4, s0_.id AS id_5, s0_
.created_at AS created_at_6, s0_.updated_at AS updated_at_7, s0_.enabled AS enabled_8, s0_.default_markup AS default_markup_9, s0_.notice AS notice_10, s0_.tree_root AS tree_root_11, s0_.parent_id AS parent_
id_12 FROM sylius_taxon s0_ WHERE s0_.parent_id IS NULL ORDER BY s0_.tree_left ASC':
SQLSTATE[42S22]: Column not found: 1054 Unknown column 's0_.notice' in 'field list'`
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42S22]: Column not found: 1054 Unknown column 's0_.notice' in 'field list'`
[PDOException]
SQLSTATE[42S22]: Column not found: 1054 Unknown column 's0_.notice' in 'field list'
If I remove the changes to Taxon.orm.yml then bin/console works again. What is missing in my changes?
One of my bundles' configruation contained that model's repository, that's it. I temporarily deleted the bundle's configuration from config.yml, and bin/console worked.
When you add new field you should doctrine:schema:update

symfony2 - createQueryBuilder Doctrine build query with first capital letter

I' following a symfony2 tutorial and I've problems with one step.
Tutorial link: http://intelligentbee.com/blog/2013/08/12/symfony2-jobeet-day-6-more-with-the-model/
I'm in 'Refactoring' step. I've one this 3 steps:
1- I've correctly modified /src/Ibw/JobeetBundle/Resources/config/doctrine/Job.orm.yml file specifiying the repository
2- I've run the command: php app/console doctrine:generate:entities IbwJobeetBundle
3- And I've added the specified tutorial function por JobRepository.php
$qb = $this->createQueryBuilder('j')
->where('j.expires_at > :date')
->setParameter('date', date('Y-m-d H:i:s', time()))
->orderBy('j.expires_at', 'DESC');
BUT when I refresh my code I get this error:
An exception occurred while executing 'SELECT j0_.id AS id0, j0_.type
AS type1, j0_.company AS company2, j0_.logo AS logo3, j0_.url AS url4,
j0_.position AS position5, j0_.location AS location6, j0_.description
AS description7, j0_.how_to_apply AS how_to_apply8, j0_.token AS
token9, j0_.is_public AS is_public10, j0_.is_activated AS
is_activated11, j0_.email AS email12, j0_.expires_at AS expires_at13,
j0_.created_at AS created_at14, j0_.updated_at AS updated_at15,
j0_.category_id AS category_id16 FROM Job j0_ WHERE j0_.expires_at > ?
ORDER BY j0_.expires_at DESC' with params ["2016-03-17 15:47:19"]:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'jobeet.Job'
doesn't exist
In the symfony profiler I can see the full query:
SELECT j0_.id AS id0, j0_.type AS type1, j0_.company AS company2,
j0_.logo AS logo3, j0_.url AS url4, j0_.position AS position5,
j0_.location AS location6, j0_.description AS description7,
j0_.how_to_apply AS how_to_apply8, j0_.token AS token9, j0_.is_public
AS is_public10, j0_.is_activated AS is_activated11, j0_.email AS
email12, j0_.expires_at AS expires_at13, j0_.created_at AS
created_at14, j0_.updated_at AS updated_at15, j0_.category_id AS
category_id16 FROM Job j0_ WHERE j0_.expires_at > '2016-03-17
15:47:19' ORDER BY j0_.expires_at DESC
the table name it's in uppercase! 'Job', not 'job'
Any can help me, please?
You can try by modifying the entity definition in Job.orm.yml by adding the table attribute:
IbwJobeetBundleEntityJob:
type: entity
table: job
repositoryClass: IbwJobeetBundleRepositoryJobRepository

When I try to update a node I get the error "Cannot Update constructed nodes"

When I try to update a node using the XQuery below I get the error XDMP-UPCONSTNODES: xdmp:node-replace(...) "Cannot Update constructed nodes"
let $_ := xdmp:node-replace($mydoc/docVersions, $otherVersions)
This is because the built in node-replace doesn't work on in-memory documents.
You can use the in-mem-update.xqy versions to do this.
import module namespace mem = "http://xqdev.com/in-mem-update" at '/MarkLogic/appservices/utils/in-mem-update.xqy';
let $_ := mem:node-replace($mydoc/docVersions, $otherVersions)

Expected known function, got 'MD5'

I need to do a search like this:
//Project\MyBundle\Repository
$query = $this->getEntityManager()->getRepository('ProjectMyBundle:Product')->createQueryBuilder('p')
->where('MD5(p.id) = :id')
->setParameter('id', $id )
->getQuery()
->getSingleResult();
I get the id on MD5 and have to search for an id on MD5 in the database.
When I do a search, I showed up, gives me the following error:
[Syntax Error] line 0, col 51: Error: Expected known function, got 'MD5'
Indicated that lib:
https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/Md5.php
But I've put it inside the folder and now I need to know where it should matter.
I am using MySQL, Doctrine 2.2 in Symfony 2.1.6.
You'll need to register MD5 as a custom DQL function:
# app/config/config.yml
doctrine:
orm:
# ...
entity_managers:
default:
# ...
dql:
string_functions:
MD5: Acme\HelloBundle\DQL\MD5Function
For more info, see: http://symfony.com/doc/2.0/cookbook/doctrine/custom_dql_functions.html

Resources