简体   繁体   English

在Symfony2中使用带有外键的Doctrine2

[英]Using Doctrine2 with Foreign Keys in Symfony2

I have a MySQL database (that is later imported into Doctrine) set up that links 3 different tables by way of foreign keys. 我有一个MySQL数据库(以后导入到Doctrine中),该数据库通过外键链接3个不同的表。 The relationship is as follows: Many As go to one B, and many Bs go to one C. For the web page that I am trying to create, I need some of the related B's information on a page about A, while being categorized by C. 关系如下:“许多”转到一个B,许多B转到一个C。对于我要创建的网页,我需要在有关A的页面上获得一些与B相关的信息,同时将其分类为C。

Think of it like this: A is "dog_food", B is "company", and C is "company_category". 这样想:A是“ dog_food”,B是“ company”,C是“ company_category”。 That is, on a page displaying different kinds of dog food, I need to display information about the manufacturing company; 也就是说,在显示不同种类狗食的页面上,我需要显示有关制造公司的信息; I only display those dog foods based on how the user specifies what kind of company they want to buy from. 我仅根据用户指定他们要购买哪种公司的方式显示那些狗食。

Pulling information dynamically from A is trivial, since the repository is pulled and the rows exist as entities. 从A动态提取信息很简单,因为存储库已被提取并且行作为实体存在。 Say, {{ dog_food.price }} would call (if specified in a for loop, this is Twig code) a single instance's price. 假设{{ dog_food.price }}将调用(如果在for循环中指定,这是Twig代码)单个实例的价格。

I have read about the @OneToOne, @OneToMany, etc. annotations, but have not been able to find a way to easily utilize their effects inside of a Twig template. 我已经阅读了有关@ OneToOne,@ OneToMany等的批注,但是还没有找到在Twig模板中轻松利用其效果的方法。 The aggregate entities and repositories for all 3 tables exist as variables in the Controller. 所有3个表的聚合实体和存储库在Controller中都作为变量存在。 It should be mentioned that, continuing with this example, that there is a single companyID field in table B corresponding to multiple dog foods, and a single categoryID associated with multiple companies. 应该提到的是,继续该示例,表B中存在一个与多种狗粮相对应的单个companyID字段,并且与多个公司相关联的单个categoryID。

What happens if I want to list the company name above the price? 如果我想在价格上方列出公司名称,该怎么办? How do I access that information in Doctrine, and furthermore, in Twig? 如何在教义中以及在Twig中访问该信息?

I will translate what you've said above into code that I would effectively write if I was you: 如果您是我,我会将您上面所说的翻译成可以有效编写的代码:

So, I assume that, along with the mapping you defined , your company category entity is called 'Company_category', your dog food entity is called 'Dog_food'. 因此,我假设与您定义的映射一起,将您的公司类别实体称为“ Company_category”,将您的狗粮实体称为“ Dog_food”。 I would pass the id of the company_category to an action in my controller, then, I would retrieve all the companies that belong to that company_category, something like this: 我将company_category的ID传递给控制器​​中的一个动作,然后,我将检索属于该company_category的所有公司,如下所示:

public function xyzAction($id){
$companies=$this->getDoctrine()->getRepository('XYZYourBundle:Company')
                ->findBy(array('company_category'=>$id));
//Hold on I will just complete it
}

Then I would retrieve all the dog foods objects from my DB, that the company's exist in $companies, the result returned in the first line of code, to do this, I would first: 然后,我将从数据库中检索所有狗食对象,即该公司存在于$ companies中,结果在第一行代码中返回,为此,我将首先执行以下操作:

1-Define my own criteria: this would help you define a complex , powerful conditions and it's easy to use. 1-定义我自己的标准:这将帮助您定义复杂,强大的条件,并且易于使用。

2-Use my criteria to filter the result of the repository, this would be useful So let's update our action: 2-使用我的标准来过滤存储库的结果, 将很有用,所以让我们更新操作:

public function xyzAction($id){

   $companies=$this->getDoctrine()->getRepository('XYZYourBundle:Company')->findBy(array('company_category'=>$id));
   $criteria = new \Doctrine\Common\Collections\Criteria();
   $criteria->where($criteria->expr()->in('company',$companies)); 
   $dogfoods=$this->getDoctrine()->getRepository('XYZYourBundle:Dog_food')->matching($criteria)      
   //Hold on I will just complete it
   }

and finally render our dogfood objects to a twig template: 最后将我们的dogfood对象渲染到树枝模板:

public function xyzAction($id){

   $companies=$this->getDoctrine()->getRepository('XYZYourBundle:Company')->findBy(array('company_category'=>$id));
   $criteria = new \Doctrine\Common\Collections\Criteria();
   $criteria->where($criteria->expr()->in('company',$companies)); 
   $dogfoods=$this->getDoctrine()->getRepository('XYZYourBundle:Dog_food')->matching($criteria)      
   return $this->render('XYZYourBundle:test.html.twig', array(
    'dogfoods'=>$dogfoods));
   }

Now, let's place ouselves in a twig template, we're gonna iterate through our $dogfoods objects and we will display some information, assuming you defined the needed getters and setters; 现在,让我们将ouselves放在树枝模板中,我们将遍历$ dogfoods对象,并假设您定义了所需的getter和setters,将显示一些信息。

  {% for dogfood in dogfoods %}
     {{dogfood.price}}        
     {{dogfood.company}}
  {% endfor %}

Edit : you can get the company's name by either: 编辑 :您可以通过以下任一方式获取公司名称:

-implement a tostring method that returns the company's name and in twig use -实现一个tostring方法,该方法返回公司名称并在枝条中使用

{{dogfood.company}}

or 要么

-in twig, use -在树枝上使用

{{dogfood.company.name}}

Hope this helps you, if something doesn't work just tell me. 希望这对您有帮助,如果有些问题不起作用,请告诉我。

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

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