SQL未插入Yii中具有关系的表 [英] SQL not inserting into table with relation in Yii

查看:75
本文介绍了SQL未插入Yii中具有关系的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个用户,并且所有值均未插入数据库中. systems_user表与方表有关系,因为party_id是sytems_user的主键.什么都没有插入.甚至没有错误,它只是返回到创建"页面.这是我的架构:

I'm trying to create a user and all the values are not inserting into the database. The systems_user table has a relation to a parties table as the party_id is the primary key of the sytems_user. Nothing is being inserted. Not even an error, it just goes back to the "create" page. Here is my schema:

--
-- 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`);

我哪里出问题了?为什么不插入?

Where have I gone wrong? Why is it not inserting?

编辑

SystemUser模型的规则:

Rules for SystemUser Model:

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

EDIT控制器actionCreate()

一些背景知识:从下面可以看到,if语句中的条件仅询问是否设置了SystemUsers,因为创建来自SystemUser表单.我的目标是获取system_user的party_id并将其插入"Parties"表中,该表与我正在使用的SystemUsers模型不同.到目前为止,当我运行此命令时,什么也没发生.

A little background: as you can see from below, the conditions inside the if statement is only asking if the SystemUsers is set because the create come from the SystemUser form. My goal is to get the party_id of the system_user and insert it into the Parties table which is a different model from the SystemUsers that i'm using. So far when I run this, nothing happens.

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

推荐答案

  1. Yii 不会在数据库中插入不安全的值,以前我遇到过此问题,如果您将所有属性设置为安全,则可以美好的.您的属性现在仅在search方案('on'=>'search')中是安全的.为了使所有属性在所有情况下均安全,请从两个模型规则中删除'on'=>'search'.

  1. Yii doesn't insert not-safe values in database, I had this problem before, if you make all attributes safe, you will be fine. your attributes now are safe only in search scenario ( 'on'=>'search' ) . for making all attributes safe in all scenarios, remove 'on'=>'search' from both model rules.

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

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

if you want to save $parties->id too, place it before save()

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

Remove redirect line, I think your save() method cannot show errors because of that.


更新:

那呢?

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

这篇关于SQL未插入Yii中具有关系的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆