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

[NFR] Escaping system to generated SQL according to database system #104

Closed
bungcip opened this issue Sep 22, 2012 · 10 comments
Closed

[NFR] Escaping system to generated SQL according to database system #104

bungcip opened this issue Sep 22, 2012 · 10 comments
Labels
new feature request Planned Feature or New Feature Request
Milestone

Comments

@bungcip
Copy link

bungcip commented Sep 22, 2012

phalcon cannot save model when using table named "user" in postgresql.

$user = new User();
$user->username = "foo";
$user->pasword = "bar";
$user->save(); /// error...

causing error:
PhalconException: ERROR: column "username" does not exist LINE 1: SELECT COUNT() AS rowcount FROM user WHERE username = 'foo' ^ when executing SELECT COUNT() AS rowcount FROM user WHERE username = 'foo'

This is because phalcon generated sql not using quoted version. The right query is

SELECT COUNT(*) AS rowcount FROM "user" WHERE username = 'foo'

sql script:

CREATE TABLE "user"
(
  username character varying(32) NOT NULL,
  password character(40) NOT NULL,
  CONSTRAINT users_pkey PRIMARY KEY (username )
)

User.php

<?php

use Phalcon\Mvc\Model;

class User extends Model {
}
@phalcon
Copy link
Collaborator

phalcon commented Sep 22, 2012

Maybe this could work:

class User extends Model {

   public function getSource(){
            return '"user"';
   }

}

@daviddoran
Copy link

Looks like your source name is double-quoted.

@bungcip
Copy link
Author

bungcip commented Sep 22, 2012

@phalcon
nope. still wrong.

PhalconException: Table ""user"" doesn't exist on database when dumping meta-data for User

@nesbert
Copy link

nesbert commented Sep 22, 2012

Try backticks... since "user" is reserved word in MySQL I believe.

HTH

class User extends Model {

   public function getSource() {
            return '`user`';
   }

}

@bungcip
Copy link
Author

bungcip commented Sep 22, 2012

@nesbert:
postgresql use double quote (") and "user" is not reversed word in MySQL. you can check in this url

http://www.petefreitag.com/tools/sql_reserved_words_checker/?word=user

@nesbert
Copy link

nesbert commented Sep 22, 2012

@bungcip "postgresql" my bad missed that in the title... thanks for the link btw :)

@phalcon
Copy link
Collaborator

phalcon commented Sep 23, 2012

we need to implement a full scaping system for columns and tables in Phalcon to fix this, that will take a couple of weeks.

@ghost ghost assigned phalcon Sep 23, 2012
@gyde
Copy link

gyde commented Sep 25, 2012

Yes, we have the exact same problem with all the keywords when using Pdo\Mysql
For now we just use a namespace_keyword convention when needed, and try to avoid keywords in general, but a full scaping system will be appriciated.

@divgit
Copy link

divgit commented Nov 12, 2012

I have the same error.
PhalconException: Table "users" doesn't exist on database when dumping meta-data for Users

When i create object use model and try to select data use static methods than i catch an error.
Error in Postgres.

Example:
$usersTable = new Users();
Users::find(); // return error
$usersTable::find(); // also error

But if i dont create Users instance all work good.

My decision for Postgres:
Create 2 models on the same table.
First use for saving (updating), second for selecting.

Very nasty bug

@phalcon phalcon closed this as completed Nov 15, 2012
@phalcon
Copy link
Collaborator

phalcon commented Nov 15, 2012

From 0.7.0 a full escaping system for columns/tables/schemas is implemented in the ORM:

For example, the following PHQL statement:

SELECT name, type FROM Robots ORDER BY name

is transformed to:

SELECT `robots`.`name` AS `name`, `robots`.`type` AS `type` 
FROM `phalcon_test`.`robots` ORDER BY `robots`.`name`

@phalcon phalcon removed their assignment Mar 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature request Planned Feature or New Feature Request
Projects
None yet
Development

No branches or pull requests

5 participants