· 7 years ago · Feb 13, 2019, 09:02 AM
1public function relations()
2{
3
4 return array(
5 'data' => array(self::HAS_ONE, 'Data', 'id'),
6 );
7}
8
9public function actionAdd_Record()
10{
11 $users = new Users();
12 $data = new Data();
13
14
15 if (isset($_POST['Users']) && isset($_POST['Data'])) {
16
17 if(!empty($_POST['Users_password'])) $_POST['Users']['password']=md5($_POST['Users_password']);
18 $users->created_date=date('Y-m-d H:i:s');
19
20
21 CActiveForm::validate(array($users, $data));
22 $users->attributes = $_POST['Users'];
23 $data->attributes = $_POST['Data'];
24
25 $valid=$users->validate();
26 $valid=$data->validate() && $valid;
27
28
29 if($valid){
30 $users->save();
31 $data->save();
32
33 $this->redirect(
34 array('view_record',
35 'id'=> $users->id)
36 );
37 }
38 }
39
40 $this->render(
41 'add_record', array(
42 'users'=> $users,
43 'data'=>$data
44 )
45 );
46}
47
48CREATE TABLE IF NOT EXISTS `data` (
49 `id` int(11) NOT NULL AUTO_INCREMENT,
50 `investment_amount` float DEFAULT '0' COMMENT '投資é¡',
51 `withdrawals` float DEFAULT '0' COMMENT '引出é¡',
52 `investment_yield` float DEFAULT '0' COMMENT 'é‹ç”¨åˆ©å›žã‚Š',
53 `account_balance` float DEFAULT '0' COMMENT 'å£åº§æ®‹é«˜',
54 `status_account` enum('open','closed') DEFAULT 'open',
55 PRIMARY KEY (`id`)
56) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
57
58CREATE TABLE IF NOT EXISTS `users` (
59 `id` int(11) NOT NULL AUTO_INCREMENT,
60 `username` varchar(128) NOT NULL,
61 `password` varchar(128) NOT NULL,
62 `name` varchar(300) NOT NULL COMMENT 'æ°å',
63 `email` varchar(200) NOT NULL,
64 `user_type` enum('normal','admin') NOT NULL DEFAULT 'normal',
65 `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
66 PRIMARY KEY (`id`)
67) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
68
69CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`money_investment`.`data`, CONSTRAINT `FK_data_users` FOREIGN KEY (`id`) REFERENCES `users` (`id`)). The SQL statement executed was: INSERT INTO `data` (`investment_amount`, `withdrawals`, `investment_yield`, `account_balance`, `status_account`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4)
70
71if($valid){
72 $users->save();
73 $data->id=$users->id;
74 $data->save();
75
76
77 $this->redirect(
78 array('view_record',
79 'id'=> $users->id)
80 );
81 }