简体   繁体   English

一对多关系在Symfony2应用程序中未显示多个条目

[英]One-to-Many Doctrine relationship not displaying more than one entry in Symfony2 Application

Background 背景

I am running a Symfony2 application for a client that tracks AEDs (defibrillators), their maintenance records, and persons responsible for their maintenance. 我正在为一个客户端运行Symfony2应用程序,该应用程序跟踪AED(除颤器),其维护记录以及负责维护的人员。 I'm in a beta launch and have run into an issue with a one-to-many bidirectional relationship achieved with Doctrine. 我正在测试版发布中,并且遇到了与Doctrine实现的一对多双向关系的问题。

The project was a conversion from a previous schema on a custom-developed framework. 该项目是对自定义开发框架上的先前架构的转换。 This means I used 这意味着我曾经

Schema 架构

The relationship structure is a "top-down approach" where the AEDs are organized by the following: 关系结构是一种“自上而下的方法”,其中AED的组织方式如下:

Municipalities
    one-to-many-> Organizations
        one-to-many-> Locations
            one-to-many-> AEDs
                one-to-many-> Monthly Check Logs
                one-to-many-> Maintenance Records
            one-to-many-> Persons
                one-to-many-> Contact Entries

(This is simplified but should give you enough context for the problem I'm having.) (这已简化,但应该为您遇到的问题提供足够的背景信息。)

The Problem 问题

I am showing a "User Profile" where all of their associated contact entries are listed in a table. 我正在显示一个“用户配置文件”,其中所有相关的联系人条目都在表中列出。 The problem is when a new entry is added, only (the newest) one shows up in the table. 问题是当添加新条目时,表中仅显示(最新)条目。 The others are still stored in the database but hidden for some reason. 其他的仍然存储在数据库中,但是由于某种原因被隐藏了。

The desired 所需

Debugging Steps 调试步骤

I cloned the production database into my staging environment and replicated the problem. 我将生产数据库克隆到暂存环境中,然后复制了问题。

I tried forcing the fetch mode to EAGER and still had the same problem. 我尝试将获取模式强制设置为EAGER ,但仍然遇到相同的问题。

I checked the length of person.contacts and it outputted a value of 1. 我检查了person.contacts的长度,结果为1。

I tried taking the runnable eager query directly into the database (as follows) and had a result of multiple entries (greater than 1): 我尝试将可运行的渴望查询直接带入数据库(如下所示),并导致多个条目(大于1)的结果:

SELECT 
  t0.table_id AS table_id1, 
  t0.ref_id AS ref_id2, 
  t0.person_id AS person_id3, 
  t0.contact_type AS contact_type4, 
  t0.contact_place AS contact_place5, 
  t0.contact AS contact6, 
  t0.contact_id AS contact_id7, 
  t0.person_id AS person_id8 
FROM 
  supContact t0 
WHERE 
  t0.person_id = 1490

I'm not sure how to proceed with debugging from here. 我不确定如何从此处进行调试。 The desired functionality is to have multiple entries show up of course. 所需的功能当然是要显示多个条目。

The Code 编码

Controller Snippet 控制器片段

public function showAction($personId = 0, $organizationId = 0)
    {
        $person = $this->getDoctrine()
                        ->getRepository('AEDTracker:Genperson')
                        ->find($personId);

        return $this->render('AEDTracker:Personnel:show.html.twig', array(
            'person' => $person,
            'organizationId' => $organizationId,
        ));
    }

Twig Template Snippet 树枝模板片段

    {% if person.contacts is empty %}
    <tr><td colspan="100">No entries</td></tr>
    {% else %}
      {% for item in person.contacts %}
        <tr class="contact_edit" module="contact" name="{{ item.contactId }}">
           <td></td>
           <td>{{ item.contactType }}</td>
           <td>{{ item.contactPlace }}</td>
           <td>{{ item.contact }}</td>
           <td><a href="{{ path('personnelEditContact', { 'contactId': item.contactId, 'organizationId': organizationId }) }}">Edit</a></td>
        </tr>
       {% endfor %}
     {% endif %}

Genperson.orm.xml Genperson.orm.xml

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="AED\Tracker\Entity\Genperson" table="genPerson">
    <id name="personId" type="integer" column="person_id">
      <generator strategy="IDENTITY"/>
    </id>

    <!-- Skipped unimportant fields -->

    <one-to-many field="contacts" target-entity="Supcontact" mapped-by="person" fetch="EAGER" />
  </entity>
</doctrine-mapping>

Supcontact.orm.xml Supcontact.orm.xml

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="AED\Tracker\Entity\Supcontact" table="supContact">
    <id name="contactId" type="integer" column="contact_id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="personId" type="integer" column="person_id" nullable="true" />

    <!-- Removed unimportant fields -->

    <many-to-one field="person" target-entity="Genperson" inversed-by="contacts">
        <join-column name="person_id" referenced-column-name="person_id" />
        <cascade>
            <cascade-persist />
            <cascade-merge />
        </cascade>
    </many-to-one>
  </entity>
</doctrine-mapping>

Any help would be greatly appreciated. 任何帮助将不胜感激。

Like a silly person, I didn't try something obvious for this fix. 像一个愚蠢的人一样,我没有为此尝试尝试一些显而易见的事情。

There must be a bug between Symfony 2.3 and 2.4, because updating fixed this issue entirely. Symfony 2.3和2.4之间必须存在一个错误,因为更新完全解决了此问题。

For those that may ever stumble on this, to update you must download the new composer.json file located here into the root folder of the project, then run 对于那些可能会偶然发现的人,要进行更新,必须将此处的新composer.json文件下载到项目的根文件夹中,然后运行

php composer.phar update

All dependencies will be updated, including the Symfony core and Doctrine library. 所有依赖项将被更新,包括Symfony核心和Doctrine库。 Make sure you test before pushing this to your production environment. 在将其推向生产环境之前,请确保进行测试。

However, oddly enough I had to keep the fetch="EAGER" parameter in my ORM XML files to keep the functionality working. 但是,奇怪的是,我必须在我的ORM XML文件中保留fetch="EAGER"参数,以使该功能正常工作。 This may not be a complete solution and someone else may have some further insight. 这可能不是一个完整的解决方案,并且其他人可能会有进一步的了解。 Ideally I'd like the application to continue using lazy loading. 理想情况下,我希望应用程序继续使用延迟加载。

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

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