drsdvwsvo78320812 2014-09-08 13:07
浏览 46
已采纳

仅在sonata管理视图中加载ManyToMany属性

i'm using sonata admin for managing entities in my symfony application. the entities have some many to many properties, and i want to administrate them in sonata. therefore i added this to the entity:

/**
 * @ORM\ManyToMany(targetEntity="Product", inversedBy="farmer", cascade={"persist"})
 * @ORM\JoinTable(name="farmerproduct",
 *     joinColumns={
 *     @ORM\JoinColumn(name="farmer_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="product_id", referencedColumnName="id")
 *   }
 * )
 */
private $products;

The problem now is that in my application, I'm fetching lots of these farmers (the entity) and this ends up in a PHP Fatal error: Allowed memory size of 134217728 bytes exhausted

The reason is, I'm sending the results to the web application through JSON.

How can I use the products only in sonata admin only? I have already programmed some lazy loading style fetching of the products in a next step. So i don't need it in my webapplication.

Any ideas?

  • 写回答

2条回答 默认 最新

  • duanlanqian9974 2014-09-08 23:53
    关注

    Okay the problem was easily solved

    First Way to do is with the setIgnored method of the serializer: http://symfony.com/doc/current/components/serializer.html#ignoring-attributes-when-serializing

    If this does not work (as it didn't work for me), second way is:

    Second way: Create a new entity object only for administration and add the manytomany in that entity, reconfigure your sonata admin config in admin.yml to work with the new entity which is mapped to the original entity in the database. Use your old entity for webapp.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?