Doctrine中使用代理对象而不是实体对象

3

我正在使用Doctrine和Symfony,遇到了一个问题。代替AppBundle\Entity\Item,我得到了`Proxies__CG__\AppBundle\Entity\Item`,其中所有数据都为null。

$test = $em->getRepository(Entity\Item::class)->find(159);
var_dump($test); exit;

我需要做出哪些改变?

object(Proxies\__CG__\AppBundle\Entity\Item)[5348]
  public '__initializer__' => 
    object(Closure)[5237]
  public '__cloner__' => 
    object(Closure)[5802]
  public '__isInitialized__' => boolean false
  private 'id' (AppBundle\Entity\Item) => int 159
  private 'name' (AppBundle\Entity\Item) => null
  private 'slug' (AppBundle\Entity\Item) => null
  private 'description' (AppBundle\Entity\Item) => null
  private 'price' (AppBundle\Entity\Item) => null
  private 'isFixedPrice' (AppBundle\Entity\Item) => null
  private 'isExchangable' (AppBundle\Entity\Item) => null
  private 'isDeliverable' (AppBundle\Entity\Item) => null
  private 'totalViewsCount' (AppBundle\Entity\Item) => null
  private 'condition' (AppBundle\Entity\Item) => null
  private 'soldAt' (AppBundle\Entity\Item) => null
  private 'category' (AppBundle\Entity\Item) => null
  private 'user' (AppBundle\Entity\Item) => null
  private 'attributes' (AppBundle\Entity\Item) => null
  private 'pics' (AppBundle\Entity\Item) => null
  protected 'createdAt' => null
  protected 'updatedAt' => null
  protected 'deletedAt' => null

我需要获取$test->getItem()->getSlug()。在数据库中存在这个id的Item。


Symfony采用延迟加载,这意味着只有在您实际使用属于“Entity\Item”的属性时,代理对象才会返回实际对象的属性。 - Jakumi
@Jakumi关于懒加载的说法是正确的,但你基本上是在测试$item->getItem()...还是打错字了? - Cerad
@Cerad 抱歉,打错了。我想获取 $test->getSlug()。在数据库中,我有值“test”。var_dump($test->getSlug()); exit;“error_message”:“找不到ID为id(159)的'AppBundle\\Entity\\Item'类型实体" - Vladymyr Hrychenko
@Jakumi 当然,我确定我已经连接到正确的数据库。截图。当我尝试获取旧行中的任何数据时,都会出现错误。但是如果我创建一个新行,则该错误不存在。 - Vladymyr Hrychenko
@Jakumi 也许是更改数据库后的某些缓存(我昨天从转储中获得了数据库)。我该如何关闭惰性加载? - Vladymyr Hrychenko
显示剩余3条评论
1个回答

0
我找到了解决方法。之前的开发者使用了@Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=true), 所以数据库中有些行有 deleted_at 的值。这就是他们没有返回任何数据的原因。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接