Can't change model builder options

2020-02-29 06:56发布

问题:

I'm trying to get symfony to use a custom class called jsDoctrineRecord instead of sfDoctrineRecord for its models. Here's the code for the overriding class:

<?php
abstract class jsDoctrineRecord extends sfDoctrineRecord
{
  public function foo()
  {
    echo 'foo';exit;
  }
}

Here's what I have in config/ProjectConfiguration.class.php, per the instructions here:

<?php

require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->enablePlugins('sfDoctrinePlugin');
    $this->enablePlugins('sfDoctrineGuardPlugin');
    $this->enablePlugins('jsDoctrineSchemaOverriderPlugin');
  }

  public function configureDoctrine(Doctrine_Manager $manager)
  {
    $options = array('baseClassName' => 'jsDoctrineRecord');
    sfConfig::set('doctrine_model_builder_options', $options);
  }
}

Unfortunately, this doesn't work. My models continue to inherit from sfDoctrineRecord instead of jsDoctrineRecord. The foo() method is not recognized. I still have the problem when I clear my cache.

I'm pretty sure I'm following the instructions right, so what could be going wrong?

回答1:

You need to rebuild the model so that the Base record classes extend your new record class. Run doctrine:build-model.



回答2:

Im not sure whay thats not working as its still there for BC, but after looking at the sfDoctrinePlugin it looks like the proper way to handle this is with a symfony event listener (see lines 83 - 89 of SF_LIB_DIR/plugins/sfDoctrinePlugin/config/sfDoctrinePluginConfiguration.class.php):

in projectConfiguration:

public function setup()
{
   $this->enablePlugins('sfDoctrinePlugin');
   $this->enablePlugins('sfDoctrineGuardPlugin');
   $this->enablePlugins('jsDoctrineSchemaOverriderPlugin');

   $this->dispatcher->connect(
     'doctrine.filter_model_builder_options', 
     array($this, 'configureDoctrineBuildOptions')
   );
}

public function configureDoctrineBuildOptions(sfEvent $event, $options)
{
   $options['baseClassName'] = 'jsDoctrineRecord';

   return $options;
}

Give that a shot and see if it makes a difference.