Php 唯一密钥对的推进验证

Php 唯一密钥对的推进验证,php,orm,propel,propel2,Php,Orm,Propel,Propel2,在我的数据库中,user\u group表中的UNIQUE\u键由两列组成,user\u id和user\u group\u id。这是推进模式中的情况: <unique name="UNIQUE_KEY"> <unique-column name="user_id"/> <unique-column name="user_group_id"/> </unique> 如果它是关于一列的,则可以如

在我的数据库中,user\u group表中的UNIQUE\u键由两列组成,
user\u id
user\u group\u id
。这是推进模式中的情况:

<unique name="UNIQUE_KEY">
            <unique-column name="user_id"/>
            <unique-column name="user_group_id"/>
</unique>

如果它是关于一列的,则可以如下设置验证行为:

<behavior name="validate">
     <parameter name="rule1"
     value="{column: column_name, validator: Unique, options {message:Your validation message here.}}"/>
</behavior>


所以我想知道的是如何为密钥对
user\u id
user\u group\u id
设置唯一验证。是否有可能传递列名数组??任何建议都是可以接受的。谢谢。

此功能尚未提供,但您有几个选项:

1。自己处理副本:

// First check for a duplicate
$duplicates = TableAQuery::create()
  ->filterByKey1($key1)
  ->filterByKey2($key2)
  ->count();

if ($duplicates>0) {
    throw new \PropelException('Row already exists');
}
// No duplicate, add the new row
 try{
     $obj->save();
 } catch (PropelException $e) {
     if (stripos($e->getMessage(), ' duplicate key ') !== false) {
         throw new \PropelException('Row already exists');
     }
 }
2。通过
try catch

// First check for a duplicate
$duplicates = TableAQuery::create()
  ->filterByKey1($key1)
  ->filterByKey2($key2)
  ->count();

if ($duplicates>0) {
    throw new \PropelException('Row already exists');
}
// No duplicate, add the new row
 try{
     $obj->save();
 } catch (PropelException $e) {
     if (stripos($e->getMessage(), ' duplicate key ') !== false) {
         throw new \PropelException('Row already exists');
     }
 }

如果性能是一个问题,选项2可能不会那么痛苦,因为它可以为您节省一个查询,但如果没有将其配置为忽略重复插入,则可能会增加自动增量(特别是在MySQL上)。

此功能还不可用,但您有几个选项:

1。自己处理副本:

// First check for a duplicate
$duplicates = TableAQuery::create()
  ->filterByKey1($key1)
  ->filterByKey2($key2)
  ->count();

if ($duplicates>0) {
    throw new \PropelException('Row already exists');
}
// No duplicate, add the new row
 try{
     $obj->save();
 } catch (PropelException $e) {
     if (stripos($e->getMessage(), ' duplicate key ') !== false) {
         throw new \PropelException('Row already exists');
     }
 }
2。通过
try catch

// First check for a duplicate
$duplicates = TableAQuery::create()
  ->filterByKey1($key1)
  ->filterByKey2($key2)
  ->count();

if ($duplicates>0) {
    throw new \PropelException('Row already exists');
}
// No duplicate, add the new row
 try{
     $obj->save();
 } catch (PropelException $e) {
     if (stripos($e->getMessage(), ' duplicate key ') !== false) {
         throw new \PropelException('Row already exists');
     }
 }
如果性能是一个问题,那么选项2可能不会那么痛苦,因为它会为您节省一个查询,但如果没有将其配置为忽略重复插入,则可能会增加自动增量(特别是在MySQL上)