<bdo id='kOdiC'></bdo><ul id='kOdiC'></ul>

  1. <legend id='kOdiC'><style id='kOdiC'><dir id='kOdiC'><q id='kOdiC'></q></dir></style></legend>

      <tfoot id='kOdiC'></tfoot>

      <small id='kOdiC'></small><noframes id='kOdiC'>

      <i id='kOdiC'><tr id='kOdiC'><dt id='kOdiC'><q id='kOdiC'><span id='kOdiC'><b id='kOdiC'><form id='kOdiC'><ins id='kOdiC'></ins><ul id='kOdiC'></ul><sub id='kOdiC'></sub></form><legend id='kOdiC'></legend><bdo id='kOdiC'><pre id='kOdiC'><center id='kOdiC'></center></pre></bdo></b><th id='kOdiC'></th></span></q></dt></tr></i><div id='kOdiC'><tfoot id='kOdiC'></tfoot><dl id='kOdiC'><fieldset id='kOdiC'></fieldset></dl></div>
    1. Yii INSERT ... 重复更新

      Yii INSERT ... ON DUPLICATE UPDATE(Yii INSERT ... 重复更新)

      1. <i id='hPhNL'><tr id='hPhNL'><dt id='hPhNL'><q id='hPhNL'><span id='hPhNL'><b id='hPhNL'><form id='hPhNL'><ins id='hPhNL'></ins><ul id='hPhNL'></ul><sub id='hPhNL'></sub></form><legend id='hPhNL'></legend><bdo id='hPhNL'><pre id='hPhNL'><center id='hPhNL'></center></pre></bdo></b><th id='hPhNL'></th></span></q></dt></tr></i><div id='hPhNL'><tfoot id='hPhNL'></tfoot><dl id='hPhNL'><fieldset id='hPhNL'></fieldset></dl></div>
          <tbody id='hPhNL'></tbody>
        • <legend id='hPhNL'><style id='hPhNL'><dir id='hPhNL'><q id='hPhNL'></q></dir></style></legend><tfoot id='hPhNL'></tfoot>
            <bdo id='hPhNL'></bdo><ul id='hPhNL'></ul>

              <small id='hPhNL'></small><noframes id='hPhNL'>

                本文介绍了Yii INSERT ... 重复更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在做一个 Yii 项目.如何使用 MySQL 的 ON DUPLICATE 功能(http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html ) 在 Yii 模型上执行 save() 时?

                I am working on a Yii project. How can I use the ON DUPLICATE feature of MySQL ( http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html ) when doing a save() on a Yii model?

                我的MySQL如下:

                CREATE TABLE `ck_space_calendar_cache` (
                  `space_id` int(11) NOT NULL,
                  `day` date NOT NULL,
                  `available` tinyint(1) unsigned NOT NULL DEFAULT '0',
                  `price` decimal(12,2) DEFAULT NULL,
                  `offer` varchar(45) DEFAULT NULL,
                  `presale_date` date DEFAULT NULL,
                  `presale_price` decimal(12,2) DEFAULT NULL,
                  `value_x` int(11) DEFAULT NULL,
                  `value_y` int(11) DEFAULT NULL,
                  PRIMARY KEY (`space_id`,`day`),
                  KEY `space` (`space_id`),
                  CONSTRAINT `space` FOREIGN KEY (`space_id`) REFERENCES `ck_space` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
                

                我的PHP如下:

                $cache = new SpaceCalendarCache();
                $cache->attributes = $day; //Some array with attributes              
                $cache->save();
                

                如果我的主键 (sapce_id,day) 中存在重复项,我不希望它抱怨,我只希望它使用最新数据进行更新.

                If there is a duplicate in my primary key (sapce_id,day), I don't want it to complain, I just want it to update with the latest data.

                我知道如何在原始 SQL 中做到这一点,我只是想知道是否有一种干净的 Yii 方法来做到这一点.

                I know how to do it in raw SQL, I was just wondering if there is a clean Yii way to do it.

                推荐答案

                我在 beforeValidate() 中检查了是否存在重复项.如果有,我设置 $this->setIsNewRecord(false);

                I overrode beforeValidate() where I checked if a duplicate exists. If one does, I set $this->setIsNewRecord(false);

                似乎有效.不确定它的性能如何.

                Seems to work. Not sure how performant it is.

                这篇关于Yii INSERT ... 重复更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                <legend id='m31tY'><style id='m31tY'><dir id='m31tY'><q id='m31tY'></q></dir></style></legend>

                <small id='m31tY'></small><noframes id='m31tY'>

                  <tbody id='m31tY'></tbody>

                1. <tfoot id='m31tY'></tfoot>
                  • <bdo id='m31tY'></bdo><ul id='m31tY'></ul>
                        <i id='m31tY'><tr id='m31tY'><dt id='m31tY'><q id='m31tY'><span id='m31tY'><b id='m31tY'><form id='m31tY'><ins id='m31tY'></ins><ul id='m31tY'></ul><sub id='m31tY'></sub></form><legend id='m31tY'></legend><bdo id='m31tY'><pre id='m31tY'><center id='m31tY'></center></pre></bdo></b><th id='m31tY'></th></span></q></dt></tr></i><div id='m31tY'><tfoot id='m31tY'></tfoot><dl id='m31tY'><fieldset id='m31tY'></fieldset></dl></div>