Doctrine尝试访问先前在迁移中删除的列

时间:2011-09-13 12:57:31

标签: symfony1 doctrine migration symfony-1.4 doctrine-1.2

我已经通过将一对多关系转换为Sample模型和Collector模型之间的多对多关系来更改Symfony应用程序的模式。 Sample模型有一个collector_id外键,现在有一个带有两个外键的中间模型(SampleCollectors):sample_idcollector_id

Sample:
  columns:
    id:  { type: integer, primary: true, autoincrement: true }
    ...
    collector_id:  ... # This column is removed
    ...
  relations:
    ...
    Collectors:  { foreignAlias: Collectors, class: Collector, local: sample_id, foreign: collector_id, refClass: SampleCollectors }

Collector:
  columns:
    id:       { type: integer, primary: true, autoincrement: true }
    ...
  relations:
    Samples:  { foreignAlias: Samples, class: Sample, local: collector_id, foreign: sample_id, refClass: SampleCollectors }


SampleCollectors:
  columns:
    sample_id:    { type: integer, primary: true }
    collector_id: { type: integer, primary: true }
  relations:
    Sample:       { onDelete: cascade }

我已经像上面那样编辑了schema.yml file并执行了以下任务:

  1. php symfony doc:generate-migrations-diff
  2. php symfony doc:build --all-classes
  3. 已经重建了每个涉及的模型/表单/过滤器并创建了两个迁移类。

    由于数据库已经处于生产模式,我已经调整了迁移类,以将一对多关系(Samplecollector_id)的外键移动到many-to - 中间表(SampleCollectorscollector_id):

    public function preUp() {
      $sampleTable = Doctrine_Core::getTable('Sample');
      // I do this because collector_id has disappeared after model regeneration.
      $sampleTable->setColumn('collector_id', 'integer', null, array('type' => 'integer'));
      $this->samples = $sampleTable->findAll()->toArray();
    }
    
    public function up() {
      // ...
      $this->removeColumn('sample', 'collector_id');
    
      // ...
      $this->createTable('sample_collectors', array(...), array(
        'type' => 'INNODB',
        'primary' => array(0 => 'sample_id', 1 => 'collector_id'),
         ...));
    }
    
    public function postUp() {
      foreach ( $this->samples as $sample ) {
        $sampleCollector = new SampleCollectors();
        $sampleCollector->setSampleId($sample['id']);
        $sampleCollector->setCollectorId($sample['collector_id']);
        $sampleCollector->trySave();
      }
    }
    

    我已经迁移了数据库,一切都很顺利。

    但是现在我正在更新控制器,视图等,Doctrine仍然尝试从Sample中检索collector_id列,而不是浏览多对多关系。

    由于Sample模型不再存储对它的引用,为什么Doctrine坚持这一点?我检查了BaseSample类,但没有hasColumn()方法设置列collector_id。顺便说一句,我也清除了缓存。

    谢谢!

1 个答案:

答案 0 :(得分:1)

我终于找到了问题。我已将Doctrine配置为使用ProjectConfiguration类中的APC缓存系统:

public function configureDoctrine(Doctrine_Manager $manager) {
  $manager->setAttribute(Doctrine::ATTR_QUERY_CACHE, new Doctrine_Cache_Apc());
}

执行php symfony cache:clear时不会清除此缓存,因为它不是Symfony,而是PHP和Web服务器。

您可以重启网络服务器或使用apc_clear_cache(),如question regarding APC cache所述。