我正在尝试在处理数据迁移的 CLI 应用程序中使用 Doctrine。事情进展顺利,直到我遇到一个主键没有标识增量的表/实体。嗯,主键实际上是三个字段的组合。表规范(MySql)是:
CREATE TABLE `xcart_variants` (
`variantid` int(11) NOT NULL DEFAULT '0' COMMENT 'Use XCVariantsChange::addVariant to add a row;AUTO_INCREMENT is removed for performance purpose',
`productid` int(11) NOT NULL DEFAULT '0',
`avail` int(11) NOT NULL DEFAULT '0',
`weight` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT 'Use func_decimal_empty',
`productcode` varchar(32) NOT NULL DEFAULT '0' COMMENT 'Cannot be unique as used by different providers and may be duplicated in XCVariantsChange::repairIntegrity()',
`list_price` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT 'Can be disabled using config[PRoduct_Options][PO_use_list_price_variants].Use func_decimal_empty',
`def` char(1) NOT NULL DEFAULT '',
`is_product_row` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'For performance;For replace left join to inner join;Must duplicate product data;Only one such row is allowed per productid;def is 0 for the row',
PRIMARY KEY (`productid`,`variantid`,`is_product_row`),
UNIQUE KEY `pp` (`productcode`,`productid`),
KEY `pi` (`productid`,`is_product_row`),
KEY `vi` (`variantid`,`is_product_row`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
引用的方法在表锁内做这样的事情:
$arr['variantid'] = func_query_first_cell("SELECT MAX(variantid)+1 FROM $sql_tbl[variants]");
$res = db_query("INSERT INTO $sql_tbl[variants]
(variantid,productid,
avail,weight,
productcode, list_price,
def,is_product_row)
VALUES ($arr[variantid], '$arr[productid]',
$arr[avail]', '$arr[weight]',
'$arr[productcode]', '$arr[list_price]',
'$arr[def]', '$arr[is_product_row]')");
问题是我在 UnitOfWork 中还有其他插入要做,并且需要连接外键关系。看起来 Doctrine 支持带有自定义映射的直接 SQL,但文档说这实际上只适用于 SELECT 语句。
处理这种情况的最佳方法是什么?