FuelPHP+PostgreSQLの組み合わせにてModel_Crudを使用した場合に登録時に登録IDを取得する方法をメモ。
以前に書いた記事の続編となります。
FuelPHP+PostgresqlでInsertIdを取得する方法
以前と同じようにCoreを拡張して対応します。
拡張対象のクラス
- Database_PDO_Connection 以前修正済みのため対象外
- Database_Query 以前修正済みのため対象外
- DB
- Model_Crud
それぞれのクラスを作成します。
APPPATH .’classes/core/db.php’
\Fuel\Core\DBクラスより、insertメソッドをコピーし拡張を施す。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class DB extends \Fuel\Core\DB { /** * Create a new [Database_Query_Builder_Insert]. * * // INSERT INTO users (id, username) * $query = DB::insert('users', array('id', 'username')); * * @param string table to insert into * @param array list of column names or array($column, $alias) or object * @param string $seq_name default NULL * @return Database_Query_Builder_Insert */ public static function insert($table = NULL, array $columns = NULL, $seq_name = NULL) { $query = new \Database_Query_Builder_Insert($table, $columns); // シーケンス名がnullでない場合は設定 if (!is_null($seq_name)) { $query->seq_name = $seq_name; } return $query; } } |
APPPATH .’classes/core/model/crud.php’
\Fuel\Core\Model_Crudクラスより、saveメソッドをコピーし拡張を施す。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php /** * Part of the Fuel framework. * * @package Fuel * @version 1.7 * @author Fuel Development Team * @license MIT License * @copyright 2010 - 2015 Fuel Development Team * @link http://fuelphp.com */ class Model_Crud extends Fuel\Core\Model_Crud { /** * Saves the object to the database by either creating a new record * or updating an existing record. Sets the default values if set. * * @param bool $validate whether to validate the input * @return mixed Rows affected and or insert ID */ public function save($validate = true) { if ($this->frozen()) { throw new \Exception('Cannot modify a frozen row.'); } ・・・・ if ($this->is_new()) { if(isset(static::$_created_at)) { if(isset(static::$_mysql_timestamp) and static::$_mysql_timestamp === true) { $vars[static::$_created_at] = \Date::forge()->format('mysql'); } else { $vars[static::$_created_at] = \Date::forge()->get_timestamp(); } } // 修正箇所 if ($this->_seq_name) { $query = \DB::insert(static::$_table_name, NULL, $this->_seq_name) ->set($vars); } else { $query = \DB::insert(static::$_table_name) ->set($vars); } // ここまで $this->pre_save($query); $result = $query->execute(static::get_connection(true)); ・・・・ return $this->post_save($result); } ・・・・ return $this->post_update($result); } // シーケンス保持フィールドを作成 protected $_seq_name = null; /** * シーケンス名を設定 * * @param string|null $seq_name シーケンス名 */ public function set_seq($seq_name = null) { $this->_seq_name = $seq_name; } } |
後はAPPPATH . ‘bootstrap.php’に拡張したCoreを読み込ませる設定を追加
1 2 3 4 5 6 7 |
Autoloader::add_classes(array( // Add classes you want to override here // Example: 'View' => APPPATH.'classes/view.php', 'DB' => APPPATH.'classes/core/db.php', 'Model_Crud' => APPPATH.'classes/core/model/crud.php', )); |
これで完了。
簡単な使い方はこれ
1 2 3 4 5 6 7 |
$data = Model_Users::forge(); $data->username = 'test'; $data->password = 'xxxxxxxxx'; $data->set_seq('users_id_seq'); $aa = $data->save(); // $aa = Array ( [0] => 5 [1] => 1 ) // $aa = [id, record] |
■ちなみに、Simpleauthのcreate_userで登録したユーザIDを取得したい場合は下記の方法がよさそうです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class DB extends \Fuel\Core\DB { /** * Create a new [Database_Query_Builder_Insert]. * * // INSERT INTO users (id, username) * $query = DB::insert('users', array('id', 'username')); * * @param string table to insert into * @param array list of column names or array($column, $alias) or object * @param string $seq_name default NULL * @return Database_Query_Builder_Insert */ public static function insert($table = NULL, array $columns = NULL, $seq_name = NULL) { $query = new \Database_Query_Builder_Insert($table, $columns); //ここを追記 if ($table == Config::get('simpleauth.table_name')) { $query->seq_name = 'users_id_seq'; } // シーケンス名がnullでない場合は設定 if (!is_null($seq_name)) { $query->seq_name = $seq_name; } return $query; } } |
Comments are closed