在Yii的Ar模型中,什么是Mysql“重复密钥更新”的等效方法

时间:2013-10-03 11:10:56

标签: php mysql yii

我正在尝试使用Yii的AR模型在表格中插入数据。我想知道Yii的AR中MySQL的ON DUPLICATE KEY UPDATE命令的等效方法是什么。 以下是从巨大的xml提要中插入数据的方法示例。

public function insertData($name, $category, $brand, $price_unit, $weight_unit, $providers, $review_text, $flavor, $imageurl) {

    $this->productModel = new Products;
    $this->brandModel = new Brand;

    $this->brandModel->name = $brand;


    $this->brandModel->save();

    $this->productModel->name = $name;
    $this->productModel->category = $category;
    $this->productModel->brand = $brand;
    $this->productModel->weight_unit = $weight_unit;
    $this->productModel->price_unit = $price_unit;
    $this->productModel->flavors = $flavor;
    $this->productModel->providers = $providers;
    $this->productModel->review_text = $review_text;
    $this->productModel->image_path = $imageurl;


    $this->productModel->save();

}

正如您所看到的,我正在使用save()进行简单插入。但是我想实现mysql的ON DUPLICATE KEY命令。

以下是相关表格:

+-------------+---------------+------+-----+---------+----------------+
| Field       | Type          | Null | Key | Default | Extra          |
+-------------+---------------+------+-----+---------+----------------+
| id          | int(11)       | NO   | PRI | NULL    | auto_increment |
| name        | varchar(100)  | YES  | MUL | NULL    |                |
| category    | varchar(50)   | YES  | MUL | NULL    |                |
| brand       | varchar(50)   | NO   | MUL | NULL    |                |
| weight      | decimal(10,2) | YES  |     | NULL    |                |
| weight_unit | varchar(30)   | YES  |     | NULL    |                |
| price       | decimal(15,2) | YES  |     | NULL    |                |
| price_unit  | varchar(30)   | YES  |     | NULL    |                |
| flavors     | varchar(45)   | YES  |     | NULL    |                |
| providers   | varchar(45)   | YES  | MUL | NULL    |                |
| review_text | text          | YES  |     | NULL    |                |
| image_path  | varchar(100)  | YES  |     | NULL    |                |
+-------------+---------------+------+-----+---------+----------------+

因此,id是主键,name上有一个索引,可以更快地查找。类别和品牌是外键。

我想用Yii的ar复制的查询将是这样的::

INSERT INTO tbl_products (name, category, brand, weight_unit, price_unit, flavors, providers, review_text, image_path) values ('.$this->parseXML[0] .', '. 
$this->parseXML[1] .',' 
.$this->parseXML[2] .','
. $this->parseXML[3] .','
. $this->parseXML[4] .','
. $this->parseXML[5] .','
.$this->parseXML[6] .','
.$this->parseXML[7] .','
.$this->parseXML[8].') 
ON DUPLICATE KEY UPDATE 
name = '.$this->parseXML[1].',
category = '.$this->parseXML[2].',
brand = '.$this->parseXML[3].'

那么,使用Yii的AR来实现这一点的有效方法是什么。我尽量避免使用蛮力方法。

先谢谢。我真的很感激我能得到的任何帮助。

感谢, Maxx

1 个答案:

答案 0 :(得分:1)

您可以在模型中定义唯一的验证规则。然后在违反此规则时捕获并使用条件块处理数据,以防遇到重复值...

在模型规则中

array('id', 'unique','message'=>'duplicate value entered'),

在控制器中:

$this->brandModel->validate(); 
.......
.......

if ($this->brandModel->getError('id')==='duplicate value entered')
{
....
....
}