Symfony 1.4 / doctrine schema.xml多对多关系不能正常工作?

时间:2011-08-05 08:00:44

标签: symfony1 doctrine symfony-1.4 doctrine-1.2

我这里有一点令人不安的问题! 使用带有Doctrine的symfony 1.4! 事实上,我有一个“多对多”的关系(见下面的代码),但我没有正确的结果!

     Monitor:
  actAs:
    Timestampable: ~
  columns:
    label: {type: string(45)}
    url: {type: string(80)}
    frequency: {type: integer}
    timeout: {type: integer}
    method: {type: enum, values: [GET, POST]}
    parameters: {type: string(255)}
  relations:
    Server:
      foreignAlias: Servers
      refClass: Benchmark
      local: monitor_id
      foreign: server_id

Server:
  actAs:
    Timestampable: ~
  columns:
    name: string(255)
    ip: string(255)
  relations:
     Monitor:
      foreignAlias: Monitors
      refClass: Benchmark
      local: server_id
      foreign: monitor_id

Benchmark:
  actAs:
    Timestampable: ~
  columns:
    monitor_id: { type: integer, primary: true }
    server_id: { type: integer, primary: true }
    connexionTime: {type: string(45)}
    executionTime: {type: string(45)}
    responseTime: {type: string(45)}
    responseCode: {type: string(45)}
    responseMessage: {type: string(45)}
  relations:
    Monitor:
      local: monitor_id
      foreign: id
      foreignAlias: Monitors
    Server:
      local: server_id
      foreign: id
      foreignAlias: Servers

1-当在添加服务器(或监视器)界面中时,会出现一个显示的监视器(或服务器)列表(但我可以添加服务器(或监视器)而不选择它。

2-在添加基准测试界面时,我没有监视器和服务器列表来选择它们!当我提交我不工作(它不应该!)。我收到这个错误:

500 | Internal Server Error | Doctrine_Connection_Mysql_Exception
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`sfmonitoring`.`benchmark`, CONSTRAINT `benchmark_monitor_id_monitor_id` FOREIGN KEY (`monitor_id`) REFERENCES `monitor` (`id`))

我在 BaseBenchmarkForm

中有这段代码
    abstract class BaseBenchmarkForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'monitor_id'      => new sfWidgetFormInputHidden(),
      'server_id'       => new sfWidgetFormInputHidden(),
      'connexionTime'   => new sfWidgetFormInputText(),
      'executionTime'   => new sfWidgetFormInputText(),
      'responseTime'    => new sfWidgetFormInputText(),
      'responseCode'    => new sfWidgetFormInputText(),
      'responseMessage' => new sfWidgetFormInputText(),
      'created_at'      => new sfWidgetFormDateTime(),
      'updated_at'      => new sfWidgetFormDateTime(),
    ));

    $this->setValidators(array(
      'monitor_id'      => new sfValidatorChoice(array('choices' => array($this->getObject()->get('monitor_id')), 'empty_value' => $this->getObject()->get('monitor_id'), 'required' => false)),
      'server_id'       => new sfValidatorChoice(array('choices' => array($this->getObject()->get('server_id')), 'empty_value' => $this->getObject()->get('server_id'), 'required' => false)),
      'connexionTime'   => new sfValidatorString(array('max_length' => 45, 'required' => false)),
      'executionTime'   => new sfValidatorString(array('max_length' => 45, 'required' => false)),

任何想法的人?????? Pleaaaaaase真的被封锁了!

################################################ V2

感谢您的帮助,这对我来说真的很有意义! 我做了这些转换:

 Monitor:
      tableName: monitor
      actAs:
        Timestampable: ~
      columns:
        label: {type: string(45)}
        url: {type: string(80)}
        frequency: {type: integer}
        timeout: {type: integer}
        method: {type: enum, values: [GET, POST]}
        parameters: {type: string(255)}

    Benchmark:
      actAs:
        Timestampable: ~
      columns:
        monitor_id: { type: integer, primary: true }
        server_id: { type: integer, primary: true }
        connexionTime: {type: string(45)}
        executionTime: {type: string(45)}
        responseTime: {type: string(45)}
        responseCode: {type: string(45)}
        responseMessage: {type: string(45)}
      relations:
        Monitor: { onDelete: CASCADE, local: monitor_id, foreign: id, foreignAlias: Monitors }
        Server: { onDelete: CASCADE, local: server_id, foreign: id, foreignAlias: Servers }

    Server:
      actAs:
        Timestampable: ~
      columns:
        name: string(255)
        ip: string(255)  

但是在基准“新”界面中,我仍然没有获得服务器和监视器列表!

2 个答案:

答案 0 :(得分:2)

好的,拿两个:你得到的(SQL)错误意味着你要保存的Benchmark有一个无效的monitor_id。所以它要么是空的,要么是指不存在的监视器。

这可能是因为您没有选择监视器/服务器,因为它们在表单上是不可见的(它们呈现为sfWidgetFormInputHidden)。 要显示这两者的<select>,您必须转到BenchmarkForm(覆盖BaseBenchmarkForm,并覆盖configure()方法。这样的事情:

public function configure() {
  parent::configure();
  $this->widgetSchema['monitor_id'] = sfWidgetFormDoctrineChoice(array('model' => 'Monitor'));
  $this->widgetSchema['server_id'] = sfWidgetFormDoctrineChoice(array('model' => 'Server'));

  $this->validatorSchema['monitor_id'] = sfValidatorDoctrineChoice(array('model' => 'Monitor'));
  $this->validatorSchema['server_id'] = sfValidatorDoctrineChoice(array('model' => 'Server'));

}

答案 1 :(得分:0)

refClass关系中的Server定义只能指向包含两个字段的模型:每个拥有方的一个字段(在这种情况下为Server和{{1} }})。通过这种方式创建“多对多”关系,您可以调用Monitor,并自动使用此$server->Monitors创建refClass(而不是Monitor)的集合。 (作为用户,您没有看到refClass)。

如果你想在这个“耦合类”Benchmark中拥有更多数据,就像你一样,你将不得不与之分离关系。

只需删除服务器中的关系,您就可以完成。