这是我的用户控制器
public function action_create_student(){
$view = View::forge('admin/users/create_student');
if (Input::method() == 'POST')
{
$val = Model_User::validate('create');
if ($val->run())
{
$newuser = Model_User::forge(array(
'username'=> Input::post('username'),
'firstname' =>Input::post('firstname'),
'middlename'=> Input::post('middlename'),
'lastname'=> Input::post('lastname'),
'password'=> Auth::instance()->hash_password(Input::post('password')),
'phone_number'=> Input::post('phone_number'),
'group'=> Input::post('group'),
'email'=> Input::post('email'),
'course' =>Input::post('course'),
));
$useruser = Model_Student::forge(array(
'course' =>Input::post('course'),
'user_id' =>Input::post('user_id'),
));
$useruser->save();
$newuser->save();
我想添加用户,我希望user_id(外键)等于用户表中的id(主键)
这是我的用户模特
<?php
class Model_User extends \Orm\Model
{
protected static $_properties = array(
'id',
'username',
'password',
'firstname',
'middlename',
'lastname',
'phone_number',
'group',
'email',
'created_at',
'updated_at',
);
protected static $_has_one = array(
'student' => array(
'model_to' => 'Model_Student',
'key_from' => 'id',
'key_to' => 'user_id',
'cascade_delete' => true,
),
);
// public function get_full_name () {
// return $this->'firstname' . ' ' . $this->'lastname';
// static::method_exists(object, method_name());
// }
protected static $_observers = array(
'Orm\Observer_CreatedAt' => array(
'events' => array('before_insert'),
'mysql_timestamp' => false,
),
'Orm\Observer_UpdatedAt' => array(
'events' => array('before_save'),
'mysql_timestamp' => false,
),
);
public static function validate($factory)
{
$val = Validation::forge($factory);
$val->add_field('username', 'Username', 'required|max_length[50]');
$val->add_field('password', 'Password', 'required|max_length[255]');
$val->add_field('firstname', 'Firstname', 'required|max_length[50]');
$val->add_field('middlename', 'Middlename', 'required|max_length[255]');
$val->add_field('lastname', 'Lastname', 'required|max_length[255]');
$val->add_field('phone_number', 'Phone Number', 'required|valid_string[numeric]');
$val->add_field('group', 'Group', 'required|valid_string[numeric]');
$val->add_field('email', 'Email', 'required|valid_email|max_length[255]');
return $val;
}
}
这是我的学生模特
class Model_Student extends \Orm\Model
{
protected static $_properties = array(
'id',
'course',
'user_id',
'created_at',
'updated_at',
);
/**
* @var array belongs_to relationships
*/
protected static $_belongs_to = array(
'user' => array(
'model_to' => 'Model_User',
'key_from' => 'user_id',
'key_to' => 'id',
),
);
protected static $_observers = array(
'Orm\Observer_CreatedAt' => array(
'events' => array('before_insert'),
'mysql_timestamp' => false,
),
'Orm\Observer_UpdatedAt' => array(
'events' => array('before_save'),
'mysql_timestamp' => false,
),
);
public static function validate($factory)
{
$val = Validation::forge($factory);
$val->add_field('course', 'Course', 'required|max_length[50]');
$val->add_field('user_id', 'User_id', 'required|valid_string[numeric]');
return $val;
}
}
帮我摆脱这个需要帮助 谢谢:))
答案 0 :(得分:0)
在您的控制器中,您只需执行
$newuser->student = $useruser;
$newuser->save();
而不是
$newuser->save();
$useruser->save();
如果您正确设置了关系,那么ORM会在保存时自动为您填充密钥。基本代码示例可在文档中找到:http://fuelphp.com/docs/packages/orm/relations/has_one.html#/has_one
答案 1 :(得分:0)
现在我将回答我的问题:)
$newuser = Model_User::forge(array(
'username'=> Input::post('username'),
'firstname' =>Input::post('firstname'),
'middlename'=> Input::post('middlename'),
'lastname'=> Input::post('lastname'),
'password'=> Auth::instance()->hash_password(Input::post('password')),
'phone_number'=> Input::post('phone_number'),
'group'=> Input::post('group'),
'email'=> Input::post('email'),
'course' =>Input::post('course'),
));
$newuser->student = Model_Student::forge(array(
'course' =>Input::post('course'),
));
if($newuser->save()){
Session::set_flash('success', e('Added user'));
Response::redirect('admin/users');
}
您只需将该字段保存在学生中,但user_id除外, 当你使用orm加入它时它会自动保存。