【问题标题】:SQL not inserting into table with relation in Yii在 Yii 中,SQL 没有插入到有关系的表中
【发布时间】:2014-01-29 06:04:24
【问题描述】:

我正在尝试创建一个用户,但所有值都没有插入到数据库中。 systems_user 表与party 表有关系,因为party_id 是sytems_user 的主键。没有插入任何内容。甚至没有错误,它只是回到“创建”页面。这是我的架构:

--
-- Table structure for table `system_users`
--

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=221 ;

--
-- Constraints for table `system_users`
--
ALTER TABLE `system_users`
  ADD CONSTRAINT `system_users_ibfk_1` FOREIGN KEY (`party_id`) REFERENCES `parties` (`id`);

-------------------------------------
-- Table structure for table `parties`
--

 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 table `parties`
--
ALTER TABLE `parties`
  ADD CONSTRAINT `parties_ibfk_1` FOREIGN KEY (`party_type_id`) REFERENCES `party_types`    (`id`);

我哪里做错了?为什么不插入?

编辑

SystemUser 模型规则:

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'),
    );
}

各方规则:

 public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('party_type_id', 'required'),
        array('party_type_id', 'length', 'max'=>10),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, party_type_id', 'safe', 'on'=>'search'),
    );
}

编辑控制器 actionCreate()

一点背景: 从下面可以看出,if 语句中的条件只是询问是否设置了 SystemUsers,因为创建来自 SystemUser 表单。我的目标是获取 system_user 的 party_id 并将其插入到与我正在使用的 SystemUsers 不同的模型的 Party 表中。到目前为止,当我运行它时,什么都没有发生。

public function actionCreate()
{
    $parties = new Parties;
    $model= new SystemUsers;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

     if (isset($_POST['SystemUsers']))
            {
                $parties->attributes = (HOW can I put the party_id from $model here?);
                $model->attributes = $_POST['SystemUsers'];
                $valid = true; 
                $valid &= $parties->validate(); 
                $valid &= $model->validate(); 

                if($valid)
                    {
                        $parties->id = $model->getPrimaryKey();
                        $parties->save(); /* First save parties. */

                        $model->save(); 
                    }
            } 

    $this->render('create',array(
    'model'=>$model,
    'parties'=>$parties, 
    ));

【问题讨论】:

  • 发布你模型的规则方法
  • party_id 是系统用户的自动增量和 PK,下面您将其称为外键...??这是矛盾的。更改您的 PK 名称,然后将您的 party_id 列引用为外键
  • @AlirezaFallah 我已根据您的要求编辑了我的答案。
  • 为什么要先保存派对?
  • 我先保存了,因为是外来物品。我只在模型保存了从模型中检索到的 pk 后才保存模型。

标签: php mysql sql yii


【解决方案1】:
  1. Yii 不会在数据库中插入不安全的值,我之前遇到过这个问题,如果你让所有属性安全,你会美好的。您的属性现在仅在 search 场景 ('on'=>'search') 中是安全的。为了使所有属性在所有情况下都安全,请从两个模型规则中删除 'on'=>'search'

  2. 您将$parties->id = $customers->getPrimaryKey(); 放在 $parties->save();之后。

    如果你也想保存$parties->id,请将其放在save()之前

  3. 删除redirect 行,我认为您的save() 方法因此无法显示错误。


更新:

这个呢?

if (isset($_POST['SystemUsers'])) {
    $model->attributes = $_POST['SystemUsers'];
    if($model->save()){  // save() does validation in itself
        $parties->id = $model->getPrimaryKey();
        $parties->save();
    }
}

【讨论】:

  • 这没有做任何事情。我实际上希望我的控制器有问题。
  • 您是否也删除了 SystemUser 模型中的 'on'=>'search'
  • 把它也拿出来了。我不明白这将如何解决我的问题。这些不是搜索规则吗?我不是要进行搜索,而是要进行“创建”。我并不是说我不欣赏你的回答,但你能解释一下你告诉我删除'on'=>'search'的原因吗?
  • 是的,您必须确保属性安全,在所有情况下,删除该部分都会这样做
  • 好的,我看到你的解释了。我会尝试改变一些东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-29
相关资源
最近更新 更多