<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 ... 重复更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                How to parse django style template tags(如何解析 django 样式模板标签)
                What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                php in background exec() function(php 后台 exec() 函数)
                <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>