Im using ZF2 with DoctrineModule and DoctrineORMModule.
Problem: I got some Tablenames in my Database that are equal to MYSQL Commands
Doctrine generates Tablenames in SQL Statements without the "`"'s
SELECT ... FROM references
But I want it to be like this Format
SELECT ... FROM `references`
to be safe..
How to Fix it in Doctrine 1 to fix my Problem I had done this:
http://i.stack.imgur.com/UN5KB.png
(link: http://www.doctrine-project.org/documentation/manual/1_0/fr/configuration:naming-convention-attributes:table-name-format)
I do not find any Fix for this Problem in Doctrine2.
Thanks for helping.
In Doctrine2 there is no automatic identifier quoting. What you could do in this case is directly putting the quotes inside your mappings, like following:
#Table(name="`references`")
That should do the trick, but won't work with schemas and SQLite in 2.2.
Related
I'd like to make a MySQL query with doctrine2 QueryBuilder (Symfony 3.4 app) with a NOT BETWEEN statment.
The doctrine provide a ->expr()->between(..) but not ->expr()->notBetween(..)
Is there a way to negate the between with the query builder.
I don't want to use a native or a DQL query if possible.
Note: I think a possible solution is to use ->expr()->lt(..) and/or ->expr()->gt(..) but I want to know if notBetween is possible.
Thanks
Expected:
A NOT BETWEEN SQL statement with Doctrine2 QueryBuilder
After some attempts, I found a suitable solution for me:
The QueryBuilder provide a ->expr()->not(...), so in this case this is possible:
$qb->exp()->not($qb->between('field', 'from', 'to')
SQL result:
NOT (BETWEEN field from AND TO)
Okay, this may be silly, but i can't make CI to stop quoting my table. I use this code in my model :
$oracle->select('id',FALSE);
$oracle->from('ms_item');
That code will result Select statement as follow:
SELECT id FROM "ms_item"
The problem is, Oracle can't find the table when it have quotes or double quotes. The CI said that second parameter in select() will remove any quote generated by Query Builder, but i don't know how to remove the quote generated by from(), please help...
Thank you...
For note, i'm using Oracle 11g and oci8_11g.dll
Try writing the table name in upper case.
$oracle->from('MS_ITEM');
When a table is created, unless you quote it, it will be stored as an uppercase table name. When you are quoting it ( as CI wants to do), it's performing a case sensitive match
I created a table named order. (Yes, bad choice I see). If I make a #OneToMany relation I get a mysql syntax error because Doctrine does not write the name in "`". Can't believe it...
How to fix this?
I had to use:
#ORM\Table(name=" `order` ")
And yes, I had to include the spaces between the quotes and the backtics. Very strange behavior.
You should enable quote_identifier with doctrine
I'm using postgresql database.
I have table with column called "from" (I can't change it).
Sql insertion query generated by doctrine are incorrect, because column name "from" should be closed by quotation marks. How can I tell doctrine to do this?
I believe, there is fast and clean way to correct that.
Thanks in advance.
Maybe $conn->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);? From Docs -> Configuration -> Identifier quoting
My script is like this:
$query = Doctrine_Query::create ()
->select('count(p.product_id) as num_a')
->from ( 'ProductComments p' )
->groupBy('p.product_id')
->having('num_a =2 ');
And the generated sql is:
SELECT COUNT(i.product_id) AS i__0 FROM productcomments i GROUP BY i.product_id HAVING num_a=2
Thus I get an error when execute the sql.
I have two questions:
why is the alias of the table 'i' instead of 'p'?
why is the 'num_a' in having clause not replaced with 'i__0',how to fixed it?
Thanks for your suggestion...
I also had a problem with setting alias.
I had to set alias and then use "ORDER BY" with that alias.
Following solution worked for me:
$myQuery->addSelect('(<my select>) AS my_alias');
$myQuery->orderBy('my_alias');
In the result query looked like "...() AS p_0 ... ORDER BY p_0".
I hope it will help someone.
1: why is the alias of the table 'i'
instead of 'p'?
2: why is the 'num_a'
in having clause not replaced with
'i__0',how to fixed it?
Both questions are simply answered: Doctrine uses it's own aliases for the query. You do no need to know these aliases since they will not affect you nor will you need to work with it.
Even though Doctrine names the alias i__0 you can access the attribute with your custom alias, e.g. $yourObject->num_a will have the proper value, namely the result of count(p.product_id).
To see the output of your query is a useful debug feature, but relying on in inside your application is non-sense since these values are only used for the internal mechanisms of Doctrine.
This would not be valid SQL.
SQL standard state that SELECT will be logically executed after having. So You need to repeat aliased code in having.
Good advice. As long as You work with SQL consuming DBs stick as closely to SQL as possible.