简体   繁体   English

原则覆盖关系访问器

[英]Doctrine override relation accessor

Say you have the following relations (altered for simplicity): 假设您具有以下关系(为简单起见已更改):

Dog:
  columns:
    id:         { type: integer, notnull: true, primary: true }
    name:       { type: string,  notnull: true }

AwardType:
  columns:
    id:         { type: integer, notnull: true, primary: true }
    name:       { type: string,  notnull: true }

Award:
  columns:
    dog_id:     { type: integer, notnull: true, primary: true }
    award_id:   { type: integer, notnull: true, primary: true }
    is_granted: { type: boolean, notnull: true, default: false }
  relations:
    Dog:
      local:        dog_id
      foreign:      id
      type:         one
      foreignType:  many
      foreignAlias: Awards
    AwardType:
      local:        award_id
      foreign:      id
      type:         one
      foreignType:  many
      foreignAlias: Awards

With the above setup I can add a new dog and manually add Awards to that dog. 通过上述设置,我可以添加一条新狗并手动向该狗添加奖励。 I can also edit awards already granted to a dog. 我还可以编辑已经授予狗的奖励。

When I call $myDog->getAwards() I want the set to include all granted awards (Award.is_granted==true && Award.dog_id==$myDog()->getId()) pluss all awards that have not been granted to that dog yet. 当我调用$ myDog-> getAwards()时,我希望集合包含所有授予的奖励(Award.is_granted == true && Award.dog_id == $ myDog()-> getId())加上所有未授予的奖励到那只狗了。

Is there an option that can be set in a model somewhere to make this happen? 是否可以在某个地方的模型中设置某个选项来实现此目的? If not, what would be the best way to achieve it? 如果没有,那么实现这一目标的最佳方法是什么?

I'm using Symfony 1.4 and the bundled Doctrine 1.2 ORM. 我正在使用Symfony 1.4和捆绑的Doctrine 1.2 ORM。

[ Edit 1 ] [编辑1]
I realize I didn't explain the whole award thing properly, so I'll try to expand on it. 我意识到我没有正确解释整个奖项,因此我将尝试扩大范围。 Say you have the following AwardTypes: 假设您有以下奖励类型:

  • 1: Bone for not molesting postman 1:不骚扰邮递员的骨头
  • 2: Collar for not biting Miss Molly 2:衣领不咬莫莉小姐
  • 3: Bigger bone for chasing milkman (who we all know is evil) 3:追逐送牛奶者的骨头更大(我们都知道那是邪恶的)

Dog1 is already registered and has been granted AwardTypes 1 and 3. When editing that Dog the form should display AwardTypes 1 and 3 with a checked checkbox and AwardType 2 with an unchecked checkbox. Dog1已经注册,并被授予AwardType 1和3。在编辑Dog时,表单应显示带有选中复选框的AwardTypes 1和3,以及带有未选中复选框的AwardType 2。 This works great if Dog 1 has one entry in the Award table for each of the AwardTypes. 如果Dog 1在每个AwardTypes的Award表中有一个条目,则该方法非常有用。 Two with is_granted == true and one is_granted == false. 两个is_granted == true,一个is_granted == false。 So far so good. 到现在为止还挺好。

When the user enters a new Dog the form should display all the AwardTypes, but with no checked checkboxes. 当用户输入新的Dog时,表单应显示所有AwardType,但没有选中的复选框。 When saving the new Dog a total of 3 rows should appear in the Awards table with the is_granted-flag set according to the checked-state of the checkboxes. 当保存新的Dog时,在Awards表中应该总共显示3行,并根据复选框的选中状态设置is_granted标志。

I know I can get all AwardTypes and check that against the AwardTypes a Dog has been granted already (showing all unchecked for new dogs, granted checked + not yet granted unchecked for existing dogs). 我知道我可以获取所有AwardTypes并对照AwardTypes来检查是否已授予Dog(显示了所有未检查的新狗,已授予已检查的狗+未授予未检查的现有狗)。 What I am asking here is wether or not Doctrine has some magic that will give me compound sets as described above. 我在这里要问的是,教义是否具有某种神奇的魔力,可以像上面描述的那样为我提供复合设置。

Ok you have a problem modeling your data. 好的,您在建模数据时遇到问题。 It should probably look like this: 它可能看起来应该像这样:

Dog:
  columns:
    id:         { type: integer, notnull: true, primary: true }
    name:       { type: string,  notnull: true, unique: true }
  relations:
    Awards:
      class: Award
      foreignType: many
      type: many
      refClass: DogAward
      local: dog_id
      foreign: award_id

Award:
  columns:
    id:         { type: integer, notnull: true, primary: true }
    name:       { type: string,  notnull: true, unique: true }

DogAward:
  columns:
    dog_id:     { type: integer, notnull: true, primary: true }
    award_id:   { type: integer, notnull: true, primary: true }

I have used a many-to-many relations, and added some constraint that I think might be useful in your case. 我使用了多对多关系,并添加了一些我认为可能对您有用的约束。 Symfony should generate a form looking like what you want, tell us if it is not the case. Symfony应该生成一个看起来像您想要的表格,如果不是这样,请告诉我们。

You can overwrite the method getAwards() method in lib/model/Dog.class.php 您可以在lib / model / Dog.class.php中覆盖方法getAwards()方法

for example 例如

public function getAwards(){
  return $query = $this->getTable()
    ->createQuery('a')
    ->where('a.dog_id = ?', $this->getId())
    ->addWhere('a.is_granted = ?', true);
    ->execute();
}

Please note, thats just a mockup, I didn't test it right now 请注意,那只是一个样机,我目前尚未对其进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM