Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate:doctrine:entity doesn't quote reserved words #1802

Closed
jalopezcar opened this issue Jul 25, 2011 · 13 comments
Closed

generate:doctrine:entity doesn't quote reserved words #1802

jalopezcar opened this issue Jul 25, 2011 · 13 comments

Comments

@jalopezcar
Copy link

The generate:doctrine:entity command doesn't quote reserved words.

Example, both 'group' and 'add' are mysql keywords.

$> app/console generate:doctrine:entity
..
The Entity shortcut name: ExampleBundle:Group
..
Configuration format (yml, xml, php, or annotation) [annotation]:
..
New field name (press to stop adding fields): add
Field type [string]: string
Field length [255]: 255

The Entity Group is correctly created but fields are not quoted.
/**
* @var string $add
*
* @Orm\Column(name="add", type="string", length=255)
*/
private $add;

so fails generating the shema

$> app/console doctrine:schema:update --dump-sql
CREATE TABLE Group (id INT AUTO_INCREMENT NOT NULL, add VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;

$> app/console doctrine:schema:update --force
Updating database schema...

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group (id INT AUTO_INCREMENT NOT NULL, add VARCHAR(255) NOT NULL, PRIMARY KEY(id' at line 1

I suposse the generated entity could be this,

/**
*

  • @Orm\Table(name="Group") <-- quoted
  • @Orm\Entity
    /
    /
    *
    • @var string $add
      *
    • @Orm\Column(name="add", type="string", length=255) <-- quoted
      */
      private $add;

CREATE TABLE Group (id INT AUTO_INCREMENT NOT NULL, add VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB

@stof
Copy link
Member

stof commented Jul 25, 2011

The Doctrine EntityGenerator does not quote them (which is why they are not quoted in the command as it uses it). And I think it is intended as the Doctrine doc explicitly says that using quoted reserved keywords is a bad practice.

@dlsniper
Copy link
Contributor

Also, see this, #1707 (and I think a few more other issue threads here). Just do a search for the 'problem'

@fabpot @weaverryan should this be a big notice either on manual or Symfony2 readme: Doctrine doesn't auto-escape reserved SQL keywords, for further reference check Doctrine2 manual here: http://www.doctrine-project.org/docs/orm/2.0/en/reference/basic-mapping.html#quoting-reserved-words ?

@fabpot
Copy link
Member

fabpot commented Jul 26, 2011

@dlsniper: This is really a Doctrine "issue", not a Symfony one and it is already explained in the Doctrine documentation.

@weaverryan: what do you think?

@fabpot fabpot closed this as completed Jul 26, 2011
@dlsniper
Copy link
Contributor

I know it's a Doctrine issue but since Symfony in relying on Doctrine and this issue keeps reappearing why not?
I've did a PR here about it: symfony/symfony-docs#579

@weaverryan
Copy link
Member

I've merged it into the docs. We are getting a little bit crowded with notes in this area, but it seems like it's probably a good addition.

symfony/symfony-docs@770127b

Thanks!

@rjmunro
Copy link

rjmunro commented Aug 20, 2014

There is a Doctrine DBAL issue about this here: http://www.doctrine-project.org/jira/browse/DBAL-40

@mindplay-dk
Copy link

Wow. As though words like "order", "limit" and "user" weren't totally ordinary in all manner of domains.

So now the technology has to influence our domain language.

This is messed-up, guys... (and a documentation fix is not a fix, it's a work-around.)

@alexgurrola
Copy link

@mindplay-dk, I've raised the issue with Doctrine, so, hopefully it goes somewhere.

doctrine/orm#5874

@valerebron
Copy link

valerebron commented Aug 20, 2019

Finaly in 2019 with symfony 4, I still get the unquoted reserved words for DELETE query ... on user.
Seriously, who want to call his table _user ?

@fabpot
Copy link
Member

fabpot commented Aug 20, 2019

@valerebron That's not part of Symfony, but Doctrine, please report your issue there.

@valerebron
Copy link

That's true fabpot.

@valerebron
Copy link

valerebron commented Aug 20, 2019

Can we use different names for entity and sql table name ?

@michaelKaefer
Copy link
Contributor

This is not a problem anymore. If you use for example bin/console make:entity Group then your entity is created with a table name using backticks:

/**
 * ...
 * @ORM\Table(name="`group`")
 */
class Group { /* ... */ }

If you use backticks the generated SQL also uses it:

SELECT DISTINCT g0_.id AS id_0 FROM `group` g0_ LIMIT 20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants