多对多嵌入式表单

时间:2011-08-19 13:33:29

标签: forms symfony1 doctrine

我有三个简单的表(种族,法官,运动员),其中列是id和name。

还有其他两个表格(race_judge和race_athlete),我在Advanced Forms中解释,通过嵌入式表格将比赛连接到许多裁判,并在前端应用程序中与许多运动员竞赛;这两个表有一些额外的字段(比如关于种族和运动员比赛号码的评判记录),可以用嵌入的形式填写。

现在我想为最后一个表“race_judge_athlete”建立一个嵌入式表格,参加比赛的每位法官都可以对参加比赛的每位运动员进行投票,我希望以单一形式填写这些数字。< / p>

我如何使用symfony + doctrine表单?

更新:当我插入一个新种族(通过RaceForm)判断并且运动员会自动添加到race_judge和race_athlete(如果从选择小部件中选择),这要归功于运动员和法官模型中的关系,但这不会发生在race_judge_athlete上。


这是schema.yml:


Race:
  columns:
    name: string(255)
Athlete:
  columns:
    name: string(255)
  relations:
    RaceAthlete:
      foreignAlias: RaceAthlete
      class: Race
      refClass: RaceAthlete
      local: athlete_id
      foreign: race_id 
Judge:
  columns:
    name: string(255)
  relations:
    RaceJudge:
      foreignAlias: RaceJudge
      class: Race
      refClass: RaceJudge
      local: judge_id
      foreign: race_id

RaceJudge:
  columns:
    race_id: integer
    judge_id: integer
    judge_notes: string
  relations:
    Race:
      class: Race
      local: race_id
    Judge:
      class: Judge
      local: judge_id
RaceAthlete:
  columns:
    race_id: integer
    athlete_id: integer
    athlete_number: integer
  relations:
    Race:
      class: Race
      local: race_id
    Athlete:
      class: Athlete
      local: athlete_id

RaceJudgeAthlete:
  columns:
    race_id: integer
    judge_id: integer
    athlete_id: integer
    score: double
  relations:
    Race:
      class: Race
      local: race_id
    Judge:
      class: Judge
      local: judge_id
    Athlete:
      class: Athlete
      local: athlete_id

2 个答案:

答案 0 :(得分:1)

我已经整理了一个有效的快速黑客,但它和地狱一样难看! 我仍在寻找一个干净的实现,但现在这样做。

在RaceForm.class.php中,我以这种方式覆盖doSave函数: 我得到RaceForm中的doSave保存的RaceJudge和RaceAthlete集合并手动创建缺失的集合,删除实际的RaceJudgeAthlete集合并保存在清空的RaceJudgeAthlete集合中手动创建的集合。

以下是代码:



    protected function doSave($con = null)
    {
        $this->saveRaceJudgeList($con);
        $this->saveRaceAthleteList($con);

        parent::doSave($con);
        $this->saveRaceJudgeAthlete();
    }

    public function saveRaceJudgeAthlete()
    {
        $race = $this->getObject();
        $RaceJudgeAthlete_collection = new Doctrine_Collection('RaceJudgeAthlete');
        $i = 0;
        foreach ($race->RaceJudge as $judge)
        {
            foreach ($race->RaceAthlete as $athlete)
            {
                $RaceJudgeAthlete_collection[$i]->setRaceId($race->getId());
                $RaceJudgeAthlete_collection[$i]->setJudgeId($judge->judge_id);
                $RaceJudgeAthlete_collection[$i]->setAthleteId($athlete->athlete_id);

                $i++;
            }
        }

        $race->RaceJudgeAthlete->delete();
        $race->RaceJudgeAthlete = $RaceJudgeAthlete_collection;
        $race->RaceJudgeAthlete->save();
    }

答案 1 :(得分:0)

你尝试过这种方式吗?我有一个类似的案例,它适用于我。

Race:
  columns:
  name: string(255)

Athlete:
  columns:
    name: string(255)
  relations:
    Races:
      class: Race
      refClass: RaceAthlete
      foreignAlias: RaceAthleteList
      local: athlete_id
      foreign: race_id 

Judge:
  columns:
    name: string(255)
  relations:
    Races:
      class: Race
      refClass: RaceJudge
      foreignAlias: RaceJudgeList
      local: judge_id
      foreign: race_id

RaceJudge:
  columns:
    race_id: integer
    judge_id: integer
    judge_notes: string
  relations:
    Race:
      class: Race
      local: race_id
    Judge:
      class: Judge
      local: judge_id

RaceAthlete:
  columns:
    race_id: integer
    athlete_id: integer
    athlete_number: integer
  relations:
    Race:
      class: Race
      local: race_id
    Athlete:
      class: Athlete
      local: athlete_id

RaceJudgeAthlete:
  columns:
    race_id: integer
    judge_id: integer
    athlete_id: integer
    score: double
  relations:
    Race:
      local: race_id
      foreign: id
    Judge:
      local: judge_id
      foreign: id
    Athlete:
      local: athlete_id
      foreign: id

HTH