###実現したいこと
FuelPHPで管理者が新規会員を登録する機能を作っています。
has_one関係にあるusersテーブルとprofileテーブルに同時にInsertしたい。
FuelPHP1.8
データベースにはMySQLを使用しています。
###テーブル
usersテーブル
sql
1CREATE TABLE IF NOT EXISTS `users` ( 2 `id` int(11) unsigned NOT NULL PRIMARY KEY, 3 `username` varchar(30) NOT NULL, 4 `password` varchar(255), 5 `email` varchar(255) NOT NULL, 6 `created_at` int(11) NOT NULL, 7 `updated_at` int(11) NOT NULL DEFAULT 0 8) ENGINE=InnoDB DEFAULT CHARSET=utf8;
profileテーブル
sql
1CREATE TABLE IF NOT EXISTS `profile` ( 2 `profile_users_id` int(11) unsigned NOT NULL PRIMARY KEY, 3 `profile_name` varchar(20) NOT NULL, 4 `profile_address` varchar(100), 5 `profile_created_at` int(11) NOT NULL, 6 `profile_updated_at` int(11) NOT NULL DEFAULT 0 7) ENGINE=InnoDB DEFAULT CHARSET=utf8;
###モデル
APPPATH/classes/model/user.php
php
1class Model_User extends \Orm\Model 2{ 3 protected static $_table_name = 'users'; 4 5 protected static $_primary_key = array('id'); 6 7 protected static $_properties = array( 8 'id', 9 'username', 10 'password', 11 'email', 12 'created_at', 13 'updated_at', 14 ); 15 16 protected static $_observers = array( 17 'Orm\Observer_CreatedAt' => array( 18 'events' => array('before_insert'), 19 'mysql_timestamp' => false, 20 ), 21 'Orm\Observer_UpdatedAt' => array( 22 'events' => array('before_update'), 23 'mysql_timestamp' => false, 24 ), 25 ); 26 27 protected static $_has_one = array( 28 'profile' => array( 29 'key_from' => 'id', 30 'model_to' => 'Model_Profile', 31 'key_to' => 'profile_users_id', 32 'cascade_save' => true, 33 'cascade_delete' => false, 34 ) 35 ); 36}
APPPATH/classes/model/profile.php
php
1class Model_Profile extends \Orm\Model 2{ 3 protected static $_table_name = 'profile'; 4 5 protected static $_primary_key = array('profile_users_id'); 6 7 protected static $_properties = array( 8 'profile_users_id', 9 'profile_name', 10 'profile_address', 11 'profile_created_at', 12 'profile_updated_at', 13 ); 14 15 protected static $_observers = array( 16 'Orm\Observer_CreatedAt' => array( 17 'events' => array('before_insert'), 18 'mysql_timestamp' => false, 19 'property' => 'profile_created_at', 20 'overwrite' => true, 21 ), 22 'Orm\Observer_UpdatedAt' => array( 23 'events' => array('before_update'), 24 'mysql_timestamp' => false, 25 'property' => 'profile_updated_at', 26 ), 27 ); 28 29 protected static $_belong_to = array( 30 'users' => array( 31 'key_from' => 'profile_users_id', 32 'model_to' => 'Model_User', 33 'key_to' => 'id', 34 'cascade_save' => false, 35 'cascade_delete' => false, 36 ) 37 ); 38}
###コントローラのアクション
php
1public function action_create() 2{ 3 if (Input::method() == 'POST') 4 { 5 $user = Model_User::forge(array( 6 'username' => Input::post('username'), 7 'password' => base64_encode(hash_pbkdf2( 8 'sha256', 9 Input::post('password'), 10 \Config::get('auth.salt'), 11 \Config::get('auth.iterations', 10000), 12 32, 13 true 14 )), 15 'email' => Input::post('email'), 16 'created_at' => \Date::forge()->get_timestamp(), 17 'updated_at' => 0, 18 )); 19 20 $user->profile = Model_Profile::forge(array( 21 'profile_name' => Input::post('profile_name'), 22 'profile_address' => Input::post('profile_address'), 23 'profile_updated_at' => 0, 24 )); 25 26 if ($user and $user->save()) 27 { 28 // 登録成功 29 Session::set_flash('success', '新しい会員『'. $user->profile->profile_name .'』を追加しました。'); 30 Response::redirect('admin/member/index'); 31 } 32 } 33 34 $this->template->title = "新規会員登録"; 35 $this->template->content = View::forge('admin/member/create'); 36} 37
※バリデーションに関する記述は省略しています。
###困っていること
これで、usersテーブルとprofileテーブルのそれぞれにInsert文が実行されると思ったのですが、
usersテーブルにしかInsertされませんでした。
ここまでは以下のドキュメントを参考にしました。
FuelPHP日本語ドキュメント > ORM > リレーション:Has One
http://fuelphp.jp/docs/1.8/packages/orm/relations/has_one.html
試しに、コントローラの以下部分、
php
1if ($user and $user->save())
ここを次のようにしてみたら、profileテーブルにもInsertできましたが、
profile_users_idが正しく(意図した通りに)セットされませんでした。
php
1if ($user and $user->save() and $user->profile->save())
2つのテーブルにInsertする方法をご教授いただけますと幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。