由于数据库中的关系,插入到数据库中不起作用 [英] Insert into Database not working due to Relations in Database
问题描述
我一直在尝试在用户注册时插入数据库,但我总是收到 SQL[23000] 错误,我意识到在我的数据库中,有一个与不同表的关系,这就是为什么我遇到了一个错误.我习惯于通过 Gii 创建模型和 crud,但这是我第一次因为表之间的关系而遇到错误.我认为问题在于我需要能够插入到两个模型中,但我不完全确定我应该如何做到这一点.
I've been trying to make an insert into the database whenever a user register, but I always got an SQL[23000] error and I realized that inside my database, there was a relationship to a different table and that is why I was getting an error. I'm used to creating a model and crud through Gii but this is the first time where I encountered an error because of relationships between tables. I think the problem is that I need to be able to insert into two models and I'm not completely sure how I should do that.
首先,我将展示我的架构:
First things first, I'll show my schema:
CREATE TABLE IF NOT EXISTS `system_users` (
`party_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(200) NOT NULL,
`password` varchar(255) NOT NULL,
`date_last_login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`status` varchar(50) NOT NULL DEFAULT 'Pending for Approval',
`date_created` datetime NOT NULL,
`date_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_role` varchar(255) NOT NULL,
`isLogin` int(1) NOT NULL,
PRIMARY KEY (`party_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=219 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `system_users`
--
ALTER TABLE `system_users`
ADD CONSTRAINT `system_users_ibfk_1` FOREIGN KEY (`party_id`) REFERENCES `parties` (`id`);
---------------------------------------
CREATE TABLE IF NOT EXISTS `parties` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`party_type_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `party_type_id` (`party_type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=200 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `parties`
--
ALTER TABLE `parties`
ADD CONSTRAINT `parties_ibfk_1` FOREIGN KEY (`party_type_id`) REFERENCES `party_types` (`id`);
在此之后,我使用 Gii 生成了一个模型,并将其命名为 SystemUsers.php,我还将 crud 生成到视图下的 systemUsers 中.
After this, I generated a model using Gii and I called it SystemUsers.php and I also generated the crud into the systemUsers under view.
现在的问题是,每次我选择创建"时,它都会向我抛出一个错误,即它无法以某种方式找到当事人 ID.
Now problem is, every time I select "Create," it throws me an error that it cannot somehow find the parties id.
以防万一,这是我的模型 SystemUsers.php 的代码:
Just in case, here is the code of my model SystemUsers.php:
<?php
class SystemUsers extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return SystemUsers the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'system_users';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password, date_last_login, date_created, user_role, isLogin', 'required'),
array('isLogin', 'numerical', 'integerOnly'=>true),
array('username', 'length', 'max'=>200),
array('password, user_role', 'length', 'max'=>255),
array('status', 'length', 'max'=>50),
array('date_modified', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('party_id, username, password, date_last_login, status, date_created, date_modified, user_role, isLogin', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'party_id' => array(self::BELONGS_TO, 'system_users', 'party_id'),
'party_id' => array(self::HAS_ONE, 'parties', 'id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'party_id' => 'Party',
'username' => 'Username',
'password' => 'Password',
'date_last_login' => 'Date Last Login',
'status' => 'Status',
'date_created' => 'Date Created',
'date_modified' => 'Date Modified',
'user_role' => 'User Role',
'isLogin' => 'Is Login',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('party_id',$this->party_id,true);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('date_last_login',$this->date_last_login,true);
$criteria->compare('status',$this->status,true);
$criteria->compare('date_created',$this->date_created,true);
$criteria->compare('date_modified',$this->date_modified,true);
$criteria->compare('user_role',$this->user_role,true);
$criteria->compare('isLogin',$this->isLogin);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
推荐答案
因为你有外键约束,那么你在父表中有条目
as u have foreign key Constraints then you have have entry in parent table
system_users -> depends on parties
and
parties -> depends on party_types
因此要在 system_user 中插入记录,您必须在 Parites 中具有记录,并且与在 party 中插入记录类似,您必须具有记录在party_types
so to insert the record in system_user you must have a record in Parites and similarly to insert the record in parties you must have a record in party_types
因此首先在 party_type 中插入记录,然后在 Parties 中为该 party_type 创建记录,然后为该 party_id 在 中创建记录>system_user
so first insert the record in party_type and for that party_type create record in Parties and then for that party_id create the record in system_user
这篇关于由于数据库中的关系,插入到数据库中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!