symfony / doctrine:无法使用对象而不刷新 [英] symfony/doctrine: cannot use object without refreshing

查看:116
本文介绍了symfony / doctrine:无法使用对象而不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是第一次遇到这个问题。我想创建一个教条对象并传递它,而不必刷新它。

It's the first time I run into this problem. I want to create a doctrine object and pass it along without having to flush it.

创建后,我可以在对象中显示一些值,但是我可以' t访问嵌套对象:

Right after it's creation, I can display some value in the object, but I can't access nested object:

    $em->persist($filter);
    print_r($filter->getDescription() . "\n");
    print_r(count($filter->getAssetClasses()));
    die;

我得到:


过滤器描述 - 0

filter description -- 0

(我应该有19 assetClass)

(I should have 19 assetClass)

如果我刷新$ filter,我仍然有同样的问题(为什么哦为什么!)
解决方案是刷新它:

If I flush $filter, i still have the same issue (why oh why !) The solution is to refresh it:

    $em->persist($filter);
    $em->flush();
    $em->refresh($filter);
    print_r($filter->getDescription() . " -- ");
    print_r(count($filter->getAssetClasses()));
    die;

我得到:


过滤器说明 - 19

filter description -- 19

不幸的是,无法刷新而不刷新。

unfortunately, you can't refresh without flushing.

在我的实体上,我得到以下内容:

On my entities, I've got the following:

在过滤器中:

public function __construct()
{
    $this->filterAssetClasses = new ArrayCollection();
    $this->assetClasses = new ArrayCollection();
}

/**
 * @var Collection
 *
 * @ORM\OneToMany(targetEntity="FilterAssetClass",                         mappedBy="filterAssetClasses", cascade={"persist"})
 */
private $filterAssetClasses;

public function addFilterAssetClass(\App\CoreBundle\Entity\FilterAssetClass $filterAssetClass)
{
    $this->filterAssetClasses[] = $filterAssetClass;
    $filterAssetClass->setFilter($this);

    return $this;
}

在FilterAssetClass类中:

in class FilterAssetClass:

/**
 * @var Filter
 *
 * @ORM\ManyToOne(targetEntity="App\CoreBundle\Entity\Filter", inversedBy="filterAssetClasses")
 */
private $filter;

/**
 * @var Filter
 *
 * @ORM\ManyToOne(targetEntity="AssetClass")
 */
private $assetClass;

public function setFilter(\App\CoreBundle\Entity\Filter $filter)
{
    $this->filter = $filter;

    return $this;
}

有人写了实体的代码,我有点丢失。我不是一个学说专家,所以如果有人可以指出我的方向很好,那将会很棒。

Someone else did write the code for the entities, and i'm a bit lost. I'm not a Doctrine expert, so if someone could point me in the good direction, that would be awesome.

Julien

推荐答案

你的问题与doctrine无关,而是持续/刷新/刷新序列;你所描述的问题只是一个坏的代码的症状。如其他人所建议的那样,您不应该依靠数据库来获取数据模型。你应该能够完全得到你的完全没有使用数据库;数据库仅在完成数据时才存储数据。

Your problem is not related to doctrine nor the persist/flush/refresh sequence; the problem you describe is only a symptom of bad code. As others have suggested, you should not be relying on the database to get at your data model. You should be able to get what you are after entirely without using the database; the database only stores the data when you are done with it.

您的过滤器类应包含一些管理此类的代码:

Your Filter class should include some code that manages this:

// Filter
public function __contsruct()
{
    $this->filterAssetClasses = new ArrayCollection();
}

/**
 * @ORM\OneToMany(targetEntity="FilterAssetClass", mappedBy="filterAssetClasses", cascade={"persist"})
 */
private $filterAssetClasses;


public function addFilterAssetClass(FilterAssetClass $class) 
{
    // assuming you don't want duplicates...
    if ($this->filterAssetClasses->contains($class) {
        return;
    }

    $this->filterAssetClasses[] = $class;

    // you also need to set the owning side of this relationship 
    //    for later persistence in the db
    // Of course you'll need to create the referenced function in your 
    //    FilterAssetClass entity

    $class->addFilter($this);
}

您可能已经有所有这些,但是您没有显示足够的代码知道。请注意,您应该可以有一个函数 setFilterAssetClass ()在您的过滤器实体。

You may have all of this already, but you didn't show enough of your code to know. Note that you should probably not have a function setFilterAssetClass() in your Filter entity.

这篇关于symfony / doctrine:无法使用对象而不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆