Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
PostgreSQL
  • hi
    this is my code for insert data to database :

    $rec = Model_Name::forge();
    $rec->name = 'abcdfeh';
    return $rec->save() ? true : false;

    data save but return false!


  • update :
    i think this bug for PostgreSQL normally not return insert id[?!] and find this : http://stackoverflow.com/a/2944481
    $this->_connection->lastInsertId([table]_[column]_seq)
    http://stackoverflow.com/a/10492645
  • HarroHarro
    Accepted Answer
    There is no specific Postgresl driver for Fuel, and the PDO driver is probably too generic on occasion. I assume you use the PDO driver?

    Having said that, the PDO driver does use lastInsertId(), see /database/pdo/connection.php, line #307, so in this case something else may be wrong?
  • hi Harro,
    PDO driver is problem and not work with update and delete with limit and insert,
    i write pgsql driver (like as write mssql driver for windows) and work fine for me.

    pgsql have tree function for escape data : pg_escape_string, pg_escape_literal, pg_escape_bytea, pg_escape_identifier
    I'm confused to select the proper function?
    thanks
  • HarroHarro
    Accepted Answer
    I have no Postgresql experience, so I don't know that the difference is.

    From what I can see

    pg_escape_identifier is for identifiers (table names, column names, etc, the backtick in MySQL)
    pg_escape_literal is for strings (as in insert values, etc)

    bytea is like literal but for bytea columns, and literal should be used instead of escape.

    Would you like to contribute your drivers to Fuel? It would be great if we have better support for other platforms.
  • thanks,i send both mssql(windows sqlsrver2012 and up) and pgsql (linux) driver to you.

  • Great! I'll add your name to the release notes for the next 1.8 release.
  • hi Harro,
    how can i send you my psql driver files
    Orm (model.php, query.php)
    DB (core\classes\db.php, core\classes\database\connection.php)
    psql (folder)
  • HarroHarro
    Accepted Answer
    With 1.9/dev there is no need to modify any of these files.

    You can create a /fuel/core/classes/database/postgresql directory, and in there a connection.php for stuff you need to overload from the base class. If you need custom builder code, you can create builder classes in /fuel/core/classes/database/postgresql/builder, and again overload what you need.

    You can also create a /fuel/core/classes/database/postgresql/schema.php, and assign it to the _schema property in your custom connection.php's constructor, so you can use dbutil methods with Postgres.

    The database platform used should be completely transparent for the underlying layers, be it DB or ORM.
  • done!
    but in orm/classes/query in build_query function $query it must \Fuel\Core\Database_Query_Builder_Where object and i create postgresql/build/where.php (for field convert type without DB::expr())
    and for now i use (/*\Fuel\Core\Database_Query_Builder_Where*/) $query object and work fine!
    one more question : how i send auto increment field to insert (postgresql/build/insert.php ) for return insert id with RETURNING id (reason i change other file for send return key in option of get, get_one, execute,...);
    thanks!
  • HarroHarro
    Accepted Answer
    \Fuel\Core\Database_Query_Builder_Where should not be a problem, as your postgres/build/where should extend this class, and type hinting works for extended classes. Same for the other query classes, they should all extend the base class.

    To emulate AUTOINCREMENT in Postgresql, see http://stackoverflow.com/questions/787722/postgresql-autoincrement.

    As Postgresql doesn't have AUTOINCREMENT, it doesn't have a set mechanism to get it's value returned after an INSERT. There are three possible options:

    1. After the INSERT, run "SELECT LASTVAL();" to get the last generated sequence number.

    2. Alter the INSERT by adding "RETURNING <pk>"; to it. This does require you to know what the PK column name (or names for a compound key) is, which isn't easy to find out in the DB class.

    3. After the INSERT, run "SELECT CURRVAL(<sequence-name>);". Downside is obviously that you need to know the sequence name for that table, which isn't really possible as well.

    So I think 1 is safest. Your driver need to extend Database_Connection::query(), so you can replace "$this->_connection->lastInsertId()," by the result of the LASTVAL query.
  • thanks Harro, I use " ;SELECT LASTVAL();" after insert query and it work.thanks again!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion