php 多对一关系持久化操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15586495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:31:16  来源:igfitidea点击:

Doctrine many to one relationship persist operation

phpmysqldoctrine-orm

提问by sbi

I have a story table and user table. The column userid in story table is a foreign key which refers the id in user table.

我有一个故事表和用户表。story表中的userid列是一个外键,引用user表中的id。

I have set the relationship is that a user may have many stories which is stored in story table. I have created the entities of both table.

我设置的关系是用户可能有许多存储在故事表中的故事。我已经创建了两个表的实体。

But if try to persist operation only to story table it is asking the details for new user entry.

但是,如果尝试仅将操作持久化到故事表,则会询问新用户条目的详细信息。

My objective is to add a new story with existing userId.

我的目标是用现有的userId.

Am posting the error here:

我在这里发布错误:

A new entity was found through the relationship 'Story#_userId' that was not configured to cascade persist operations for entity: User@0000000038960c50000000008ea93852. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={\"persist\"}).

通过关系“Story#_userId”找到了一个新实体,该实体未配置为对实体进行级联持久化操作:User@0000000038960c50000000008ea93852。解决这个问题:要么在这个未知实体上显式调用 EntityManager#persist() 要么配置级联在映射中持久化这个关联,例如@ManyToOne(..,cascade={\"persist\"})。

I set ManyToOne relationship in Storyentity:

我在Story实体中设置多对一关系:

/**
 * @ManyToOne(targetEntity="User", inversedBy = "_story" )
 * @JoinColumns({
 *     @JoinColumn(name="user_id", referencedColumnName="id")
 * })
 */

private $_userId;  

I checked the database schema and it shows relationship is set correctly. So I have done the story insertion process.

我检查了数据库架构,它显示关系设置正确。所以我已经完成了故事插入过程。

$user = new User();
$user->setUserId($id);
$story = new Story();
$story->setContent("....");
$story->setUserid($user);
$this->em->persist($story);
$this->em->flush();

回答by K. Norbert

You are probably persisting the story entity but not the user. If you have something like this:

您可能正在持久化故事实体而不是用户。如果你有这样的事情:

$story = new Story();
$user = new User();
$story->setUser($user);
$em->persist($story);
$em->flush();

This will result in a fatal error, since you are persisting one entity, but through its relations, Doctrine finds another new entity. You have two options:

这将导致致命错误,因为您正在持久化一个实体,但通过其关系,Doctrine 找到了另一个新实体。您有两个选择:

Call persist on both entities:

在两个实体上调用持久化:

$story = new Story();
$user = new User();
$story->setUser($user);
$em->persist($story);
$em->persist($user);
$em->flush();

Or, set up cascading persist for the Story entity. Eg. if you are using annotation mapping, you would do something like

或者,为 Story 实体设置级联持久化。例如。如果您使用注释映射,您会执行类似的操作

/**
 * @ManyToOne(targetEntity="User", inversedBy="stories", cascade={"persist"})
 */
private $author;

The chapter 8. Working with associationsdetails this.

8使用关联对此进行详细说明。

回答by BartBiczBo?y

K. Norbert's answer hits the spot, but there is something that might be unclear. At least it was unclear for me, who came to doctrine from SQL.

K. Norbert 的回答恰到好处,但有些事情可能不清楚。至少对我来说不清楚,我是从 SQL 开始学说的。

The thing is doctrine seem to remember which objects are already persisted. Whenever it finds new(not persisted yet) objects related to entity you want to persist - it yells with 'A new entity was found through the relationship ...' error. When you want to save new object (only one entity, without persisting related entities) you just need to make sure related entities are persisted already. So:

事情是学说似乎记得哪些对象已经被持久化了。每当它发现与您想要持久化的实体相关的(尚未持久化)对象时,它就会大喊“通过关系找到了新实体......”错误。当你想保存新对象(只有一个实体,不持久化相关实体)时,你只需要确保相关实体已经持久化。所以:

$story = new Story();
$entityManager->find('User', $id);
$story->setUser($user);
$em->persist($story);
$em->flush();

does the trick. Because doctrine knows the user is persisted already and it doesn't need to do it anymore. Doctrine knows because we got the user from the database.

诀窍。因为学说知道用户已经被持久化了,它不再需要这样做了。Doctrine 知道,因为我们从数据库中获取了用户。

回答by ophintor

I'm struggling to understand what you need, so I'm not sure if this will respond to your question.

我正在努力了解您的需求,所以我不确定这是否会回答您的问题。

As the story.user_id id a foreign key of user.id (which is a primary key), this can't be empty/null. So if, let's say, you have a php session, you should probably store your user id (or user name) in your session variables, and when you create a new record in the story table, use the user id from your session to populate the story.user_id attribute.

由于 story.user_id id 是 user.id 的外键(主键),所以不能为空/空。因此,假设您有一个 php 会话,您可能应该将您的用户 ID(或用户名)存储在会话变量中,并且当您在故事表中创建新记录时,使用会话中的用户 ID 进行填充story.user_id 属性。

Hope it helps.

希望能帮助到你。

回答by Athlan

I would be helpful if you attach your entites.

如果您附上您的实体,我会有所帮助。

But I suppose that it is simmilar problem to mine. You cannot define the @Id and @ManyToOne together in one field. You have to use separate fields for an @Id and for relation @ManyToOne.

但我想这与我的问题相似。您不能在一个字段中同时定义 @Id 和 @ManyToOne。您必须为@Id 和关系@ManyToOne 使用单独的字段。

If you have a new (not persisted yet) User entity, you have to persist User which have cascade={"persist"} or cascade={"all"} in field correspond to Story entity. You can store the Story entity only if User already exists and it is attached to Doctrine.

如果您有一个新的(尚未持久化的)用户实体,则必须在与 Story 实体对应的字段中持久化具有级联 ={"persist"} 或级联 ={"all"} 的用户。只有当 User 已经存在并且它附加到 Doctrine 时,您才能存储 Story 实体。

Hope it help.

希望有帮助。

回答by plain jane

Dont you think you should set Userrather than Useridfor story

你不认为你应该设置User而不是Useridstory

$user = new User();
$user->setUserId($id);
$story = new Story();
$story->setContent("....");
$story->setUser($user);   //change here
$this->em->persist($story);
$this->em->flush();