从yml禁用相关对象id的自动索引

时间:2011-01-06 10:41:06

标签: symfony1 doctrine symfony-1.4 phpdoc

Doctrine自动在用于定义对象关系的列上创建索引

例如

user:id,name

消息:id,sender_id,receiver_id,message

如果我以消息具有一个发件人并且具有一个Receiver的方式定义消息和用户之间的关系,则当我从模型生成sql时,doctrine将自动索引sender_id和receiver_id字段。我想在发送方上禁用索引,因为我手动创建了包含sender_id和receiver id的索引。如何禁用自动生成的索引?

1 个答案:

答案 0 :(得分:1)

您好我假设您使用的是MySQL,并查看了Doctrine / Export / Mysql.php 我发现了这个:

// build indexes for all foreign key fields (needed in MySQL!!)
if (isset($options['foreignKeys'])) {
      foreach ($options['foreignKeys'] as $fk) {
          $local = $fk['local'];
          $found = false;
          if (isset($options['indexes'])) {
             foreach ($options['indexes'] as $definition) {
                 if (is_string($definition['fields'])) {
                     // Check if index already exists on the column                            
                   $found = $found || ($local == $definition['fields']);    
                 } else if (in_array($local, $definition['fields']) && count($definition['fields']) === 1) {
                    // Index already exists on the column
                    $found = true;
                 }   
            }   
        }   
        if (isset($options['primary']) && !empty($options['primary']) &&
                in_array($local, $options['primary'])) {
             // field is part of the PK and therefore already indexed
             $found = true;
        }   

        if ( ! $found) {
            if (is_array($local)) {
             foreach($local as $localidx) {
                 $options['indexes'][$localidx] = array('fields' => array($localidx => array()));
             }   
        } else {
              $options['indexes'][$local] = array('fields' => array($local => array()));    
             }   
         }   
     }   
 }   

如果我理解正确,要禁用索引应该是主键的一部分。