doushan2311 2013-11-10 22:31
浏览 30

Symfony2删除记录实体问题

I am having an issue with Symfony2 when it comes to removing a record from a table.

I have a table that stores the following: purchaseOrders, purchaseOrderItems, and warehouses

Purchase Order Entity "purchaseOrders"

 id|name   |
 -----------
 1 |Test   |

Warehouses Entity "warehouses"

 id|name             |
 ---------------------
 1 |Test Warehouse   |
 2 |Test Warehouse 2 |

Purchase Order Table Items Entity "purchaseOrderItems"

 id|name        |purchaseOrder_id|warehouse_id
 --------------------------------
 1 |Test Item 1 |1               |1
 2 |Test Item 2 |1               |1
 3 |Test Item 3 |1               |2

The purchaseOrder_id is a many to one relationship linked to a table called "purchaseOrders". The warehouse_id is a many to one relationship linked to a table called "warehouses".

My view.html.twig page is kinda laid out like so:

 PurchaseOrder
 --------------------------------------
 | | Item         | Warehouse
 --------------------------------------
 |1| Test Item 1  | Test Warehouse
 |2| Test Item 2  | Test Warehouse
 |3| Test Item 3  | Test Warehouse 2

If I remove record #1 from the "warehouses" table I get an error in this view.html.twig template

  An exception has been thrown during the rendering of a template ("Entity was not found.") in /Applications/XAMPP/xamppfiles/htdocs/symfonydev/src/WIC/PurchaseOrderBundle/Resources/views/PurchaseOrder/view.html.twig at line 320.

The issue is because when the twig template is trying to access the warehouse "name" it cant find it in the object to render it...

I have tried the following conditional statements and nothing works, I still get that same error:

 {% if purchaseOrderItems.warehouses.name != null %}
      {{ purchaseOrderItems.warehouses.name }}
 {% endif %}

 {% if purchaseOrderItems.warehouses.name is defined %}
      {{ purchaseOrderItems.warehouses.name }}
 {% endif %}

 {% if purchaseOrderItems.warehouses.name is not null %}
      {{ purchaseOrderItems.warehouses.name }}
 {% endif %}

What I need to know is this:

When I remove a "warehouse" how can i make it so that the "purchaseOrderItems" relationship to the "warehouse" doesn't break or puts Null or 0 in the value.

The issue has to be that the purchaseOrderItems cant make the mapping to the warehouse because the record has been deleted.

Please help or advise what I need to do to keep this from happening.

Thanks so much!

  • 写回答

2条回答 默认 最新

  • dongshi1914 2013-11-11 03:00
    关注

    First mistake, twig error, you should write like this

    {% if purchaseOrderItems.warehouses %}
      {{ purchaseOrderItems.warehouses.name }}
    {% endif %}
    

    Then your question:

    I don't find a way that doctrine can do it automatically.

    So just set the warehouse in the purchaseOrder to null when you remove one warehouse. After all, this is a bad Idea, you'll mess up you data.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?