Merge is creating new record for children
Merge is creating not working for children @OneToMany
I am using Php Doctrine and I am using @OnToMany
mapping with cascade
all
. I have a parent class SalesOrder
and child class SalesOrderDetails
.
Case 1 : Save
- When I save new record sales order along with sales order details. It is working as expected.
Case 2 : Update
- Here is the issue, I am merging theSales Order
which is fine however its inserting new records for its childrenSalesOrderDetail
instead of updating it. Ideally it should it applymerge
but for children as well but its not.
As of now, I am getting the Sales Order Details by id from DB then change the properties of it. Ideally that should not be the case, mean if we set the id to unmanned object, it should update instead of creating new records.
Note:
1. Merge is working with parent object if it has the id value.
2. I am not adding new item here, I am just updating the existing recorded through merge.
SalesOrder.php
/**
* @Entity @Table(name="sales_orders")
* */
class SalesOrder extends BaseEntity {
/**
* @OneToMany(targetEntity="SalesOrderDetail",cascade="all", mappedBy="salesOrder" )
*/
protected $itemSet;
function __construct() {
$this->itemSet = new ArrayCollection();
}
}
SalesOrderDetail.php
/**
* @Entity @Table(name="sales_order_details")
* */
class SalesOrderDetail extends BaseEntity {
/** @Id @Column(type="integer") @GeneratedValue * */
protected $id;
/**
* @ManyToOne(targetEntity="SalesOrder")
* @JoinColumn(name="order_no", referencedColumnName="order_no")
*/
protected $salesOrder;
}
Debug Mode screen
If I use cascade={"merge"}
I am getting different error if I am using Cascades merge
Type: DoctrineORMORMInvalidArgumentException Message: Multiple
non-persisted new entities were found through the given association
graph: * A new entity was found through the relationship
'ZiletechDatabaseEntitySalesOrder#itemSet' that was not configured
to cascade persist operations for entity:
ZiletechDatabaseEntitySalesOrderDetail@0000000052218380000000007058b4a6.
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"}). If you
cannot find out which entity causes the problem implement
'ZiletechDatabaseEntitySalesOrderDetail#__toString()' to get a
clue. * A new entity was found through the relationship
'ZiletechDatabaseEntitySalesOrder#itemSet' that was not configured
to cascade persist operations for entity:
ZiletechDatabaseEntitySalesOrderDetail@0000000052218071000000007058b4a6.
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"}). If you
cannot find out which entity causes the problem implement
'ZiletechDatabaseEntitySalesOrderDetail#__toString()' to get a
clue.
php doctrine-orm doctrine
add a comment |
Merge is creating not working for children @OneToMany
I am using Php Doctrine and I am using @OnToMany
mapping with cascade
all
. I have a parent class SalesOrder
and child class SalesOrderDetails
.
Case 1 : Save
- When I save new record sales order along with sales order details. It is working as expected.
Case 2 : Update
- Here is the issue, I am merging theSales Order
which is fine however its inserting new records for its childrenSalesOrderDetail
instead of updating it. Ideally it should it applymerge
but for children as well but its not.
As of now, I am getting the Sales Order Details by id from DB then change the properties of it. Ideally that should not be the case, mean if we set the id to unmanned object, it should update instead of creating new records.
Note:
1. Merge is working with parent object if it has the id value.
2. I am not adding new item here, I am just updating the existing recorded through merge.
SalesOrder.php
/**
* @Entity @Table(name="sales_orders")
* */
class SalesOrder extends BaseEntity {
/**
* @OneToMany(targetEntity="SalesOrderDetail",cascade="all", mappedBy="salesOrder" )
*/
protected $itemSet;
function __construct() {
$this->itemSet = new ArrayCollection();
}
}
SalesOrderDetail.php
/**
* @Entity @Table(name="sales_order_details")
* */
class SalesOrderDetail extends BaseEntity {
/** @Id @Column(type="integer") @GeneratedValue * */
protected $id;
/**
* @ManyToOne(targetEntity="SalesOrder")
* @JoinColumn(name="order_no", referencedColumnName="order_no")
*/
protected $salesOrder;
}
Debug Mode screen
If I use cascade={"merge"}
I am getting different error if I am using Cascades merge
Type: DoctrineORMORMInvalidArgumentException Message: Multiple
non-persisted new entities were found through the given association
graph: * A new entity was found through the relationship
'ZiletechDatabaseEntitySalesOrder#itemSet' that was not configured
to cascade persist operations for entity:
ZiletechDatabaseEntitySalesOrderDetail@0000000052218380000000007058b4a6.
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"}). If you
cannot find out which entity causes the problem implement
'ZiletechDatabaseEntitySalesOrderDetail#__toString()' to get a
clue. * A new entity was found through the relationship
'ZiletechDatabaseEntitySalesOrder#itemSet' that was not configured
to cascade persist operations for entity:
ZiletechDatabaseEntitySalesOrderDetail@0000000052218071000000007058b4a6.
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"}). If you
cannot find out which entity causes the problem implement
'ZiletechDatabaseEntitySalesOrderDetail#__toString()' to get a
clue.
php doctrine-orm doctrine
Do you really need that merge? It sounds weird for updating entities when flush will be enough (unless you're merging Sales into EM before applying any changes)
– malarzm
Nov 17 '18 at 22:24
The entire data I get from the front-end and create the entities out of it so its not managed object which can be saved on flush automatically.
– Sunil Singh
Nov 18 '18 at 5:37
add a comment |
Merge is creating not working for children @OneToMany
I am using Php Doctrine and I am using @OnToMany
mapping with cascade
all
. I have a parent class SalesOrder
and child class SalesOrderDetails
.
Case 1 : Save
- When I save new record sales order along with sales order details. It is working as expected.
Case 2 : Update
- Here is the issue, I am merging theSales Order
which is fine however its inserting new records for its childrenSalesOrderDetail
instead of updating it. Ideally it should it applymerge
but for children as well but its not.
As of now, I am getting the Sales Order Details by id from DB then change the properties of it. Ideally that should not be the case, mean if we set the id to unmanned object, it should update instead of creating new records.
Note:
1. Merge is working with parent object if it has the id value.
2. I am not adding new item here, I am just updating the existing recorded through merge.
SalesOrder.php
/**
* @Entity @Table(name="sales_orders")
* */
class SalesOrder extends BaseEntity {
/**
* @OneToMany(targetEntity="SalesOrderDetail",cascade="all", mappedBy="salesOrder" )
*/
protected $itemSet;
function __construct() {
$this->itemSet = new ArrayCollection();
}
}
SalesOrderDetail.php
/**
* @Entity @Table(name="sales_order_details")
* */
class SalesOrderDetail extends BaseEntity {
/** @Id @Column(type="integer") @GeneratedValue * */
protected $id;
/**
* @ManyToOne(targetEntity="SalesOrder")
* @JoinColumn(name="order_no", referencedColumnName="order_no")
*/
protected $salesOrder;
}
Debug Mode screen
If I use cascade={"merge"}
I am getting different error if I am using Cascades merge
Type: DoctrineORMORMInvalidArgumentException Message: Multiple
non-persisted new entities were found through the given association
graph: * A new entity was found through the relationship
'ZiletechDatabaseEntitySalesOrder#itemSet' that was not configured
to cascade persist operations for entity:
ZiletechDatabaseEntitySalesOrderDetail@0000000052218380000000007058b4a6.
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"}). If you
cannot find out which entity causes the problem implement
'ZiletechDatabaseEntitySalesOrderDetail#__toString()' to get a
clue. * A new entity was found through the relationship
'ZiletechDatabaseEntitySalesOrder#itemSet' that was not configured
to cascade persist operations for entity:
ZiletechDatabaseEntitySalesOrderDetail@0000000052218071000000007058b4a6.
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"}). If you
cannot find out which entity causes the problem implement
'ZiletechDatabaseEntitySalesOrderDetail#__toString()' to get a
clue.
php doctrine-orm doctrine
Merge is creating not working for children @OneToMany
I am using Php Doctrine and I am using @OnToMany
mapping with cascade
all
. I have a parent class SalesOrder
and child class SalesOrderDetails
.
Case 1 : Save
- When I save new record sales order along with sales order details. It is working as expected.
Case 2 : Update
- Here is the issue, I am merging theSales Order
which is fine however its inserting new records for its childrenSalesOrderDetail
instead of updating it. Ideally it should it applymerge
but for children as well but its not.
As of now, I am getting the Sales Order Details by id from DB then change the properties of it. Ideally that should not be the case, mean if we set the id to unmanned object, it should update instead of creating new records.
Note:
1. Merge is working with parent object if it has the id value.
2. I am not adding new item here, I am just updating the existing recorded through merge.
SalesOrder.php
/**
* @Entity @Table(name="sales_orders")
* */
class SalesOrder extends BaseEntity {
/**
* @OneToMany(targetEntity="SalesOrderDetail",cascade="all", mappedBy="salesOrder" )
*/
protected $itemSet;
function __construct() {
$this->itemSet = new ArrayCollection();
}
}
SalesOrderDetail.php
/**
* @Entity @Table(name="sales_order_details")
* */
class SalesOrderDetail extends BaseEntity {
/** @Id @Column(type="integer") @GeneratedValue * */
protected $id;
/**
* @ManyToOne(targetEntity="SalesOrder")
* @JoinColumn(name="order_no", referencedColumnName="order_no")
*/
protected $salesOrder;
}
Debug Mode screen
If I use cascade={"merge"}
I am getting different error if I am using Cascades merge
Type: DoctrineORMORMInvalidArgumentException Message: Multiple
non-persisted new entities were found through the given association
graph: * A new entity was found through the relationship
'ZiletechDatabaseEntitySalesOrder#itemSet' that was not configured
to cascade persist operations for entity:
ZiletechDatabaseEntitySalesOrderDetail@0000000052218380000000007058b4a6.
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"}). If you
cannot find out which entity causes the problem implement
'ZiletechDatabaseEntitySalesOrderDetail#__toString()' to get a
clue. * A new entity was found through the relationship
'ZiletechDatabaseEntitySalesOrder#itemSet' that was not configured
to cascade persist operations for entity:
ZiletechDatabaseEntitySalesOrderDetail@0000000052218071000000007058b4a6.
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"}). If you
cannot find out which entity causes the problem implement
'ZiletechDatabaseEntitySalesOrderDetail#__toString()' to get a
clue.
php doctrine-orm doctrine
php doctrine-orm doctrine
edited Nov 19 '18 at 12:16
Sunil Singh
asked Nov 17 '18 at 13:40
Sunil SinghSunil Singh
6,2322626
6,2322626
Do you really need that merge? It sounds weird for updating entities when flush will be enough (unless you're merging Sales into EM before applying any changes)
– malarzm
Nov 17 '18 at 22:24
The entire data I get from the front-end and create the entities out of it so its not managed object which can be saved on flush automatically.
– Sunil Singh
Nov 18 '18 at 5:37
add a comment |
Do you really need that merge? It sounds weird for updating entities when flush will be enough (unless you're merging Sales into EM before applying any changes)
– malarzm
Nov 17 '18 at 22:24
The entire data I get from the front-end and create the entities out of it so its not managed object which can be saved on flush automatically.
– Sunil Singh
Nov 18 '18 at 5:37
Do you really need that merge? It sounds weird for updating entities when flush will be enough (unless you're merging Sales into EM before applying any changes)
– malarzm
Nov 17 '18 at 22:24
Do you really need that merge? It sounds weird for updating entities when flush will be enough (unless you're merging Sales into EM before applying any changes)
– malarzm
Nov 17 '18 at 22:24
The entire data I get from the front-end and create the entities out of it so its not managed object which can be saved on flush automatically.
– Sunil Singh
Nov 18 '18 at 5:37
The entire data I get from the front-end and create the entities out of it so its not managed object which can be saved on flush automatically.
– Sunil Singh
Nov 18 '18 at 5:37
add a comment |
1 Answer
1
active
oldest
votes
You have a mistake in your mapping, cascade
needs an array
/**
* @OneToMany(targetEntity="SalesOrderDetail", cascade={"all"}, mappedBy="salesOrder" )
*/
protected $itemSet;
If I usearray
instead ofArrayCollection
, I am getting the errorClass '' does not exist
. Detailed error is added in answer. To avoid this error I usedArrayCollection
.
– Sunil Singh
Nov 18 '18 at 13:00
I meant thatcascade
needs an array, so you should have thiscascade={"all"}
instead ofcascade="all"
in your@OneToMany
mapping
– malarzm
Nov 19 '18 at 9:15
Unfortunately it didn't help. Getting same error.
– Sunil Singh
Nov 19 '18 at 12:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351773%2fmerge-is-creating-new-record-for-children%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have a mistake in your mapping, cascade
needs an array
/**
* @OneToMany(targetEntity="SalesOrderDetail", cascade={"all"}, mappedBy="salesOrder" )
*/
protected $itemSet;
If I usearray
instead ofArrayCollection
, I am getting the errorClass '' does not exist
. Detailed error is added in answer. To avoid this error I usedArrayCollection
.
– Sunil Singh
Nov 18 '18 at 13:00
I meant thatcascade
needs an array, so you should have thiscascade={"all"}
instead ofcascade="all"
in your@OneToMany
mapping
– malarzm
Nov 19 '18 at 9:15
Unfortunately it didn't help. Getting same error.
– Sunil Singh
Nov 19 '18 at 12:11
add a comment |
You have a mistake in your mapping, cascade
needs an array
/**
* @OneToMany(targetEntity="SalesOrderDetail", cascade={"all"}, mappedBy="salesOrder" )
*/
protected $itemSet;
If I usearray
instead ofArrayCollection
, I am getting the errorClass '' does not exist
. Detailed error is added in answer. To avoid this error I usedArrayCollection
.
– Sunil Singh
Nov 18 '18 at 13:00
I meant thatcascade
needs an array, so you should have thiscascade={"all"}
instead ofcascade="all"
in your@OneToMany
mapping
– malarzm
Nov 19 '18 at 9:15
Unfortunately it didn't help. Getting same error.
– Sunil Singh
Nov 19 '18 at 12:11
add a comment |
You have a mistake in your mapping, cascade
needs an array
/**
* @OneToMany(targetEntity="SalesOrderDetail", cascade={"all"}, mappedBy="salesOrder" )
*/
protected $itemSet;
You have a mistake in your mapping, cascade
needs an array
/**
* @OneToMany(targetEntity="SalesOrderDetail", cascade={"all"}, mappedBy="salesOrder" )
*/
protected $itemSet;
answered Nov 18 '18 at 12:23
malarzmmalarzm
2,11021117
2,11021117
If I usearray
instead ofArrayCollection
, I am getting the errorClass '' does not exist
. Detailed error is added in answer. To avoid this error I usedArrayCollection
.
– Sunil Singh
Nov 18 '18 at 13:00
I meant thatcascade
needs an array, so you should have thiscascade={"all"}
instead ofcascade="all"
in your@OneToMany
mapping
– malarzm
Nov 19 '18 at 9:15
Unfortunately it didn't help. Getting same error.
– Sunil Singh
Nov 19 '18 at 12:11
add a comment |
If I usearray
instead ofArrayCollection
, I am getting the errorClass '' does not exist
. Detailed error is added in answer. To avoid this error I usedArrayCollection
.
– Sunil Singh
Nov 18 '18 at 13:00
I meant thatcascade
needs an array, so you should have thiscascade={"all"}
instead ofcascade="all"
in your@OneToMany
mapping
– malarzm
Nov 19 '18 at 9:15
Unfortunately it didn't help. Getting same error.
– Sunil Singh
Nov 19 '18 at 12:11
If I use
array
instead of ArrayCollection
, I am getting the error Class '' does not exist
. Detailed error is added in answer. To avoid this error I used ArrayCollection
.– Sunil Singh
Nov 18 '18 at 13:00
If I use
array
instead of ArrayCollection
, I am getting the error Class '' does not exist
. Detailed error is added in answer. To avoid this error I used ArrayCollection
.– Sunil Singh
Nov 18 '18 at 13:00
I meant that
cascade
needs an array, so you should have this cascade={"all"}
instead of cascade="all"
in your @OneToMany
mapping– malarzm
Nov 19 '18 at 9:15
I meant that
cascade
needs an array, so you should have this cascade={"all"}
instead of cascade="all"
in your @OneToMany
mapping– malarzm
Nov 19 '18 at 9:15
Unfortunately it didn't help. Getting same error.
– Sunil Singh
Nov 19 '18 at 12:11
Unfortunately it didn't help. Getting same error.
– Sunil Singh
Nov 19 '18 at 12:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351773%2fmerge-is-creating-new-record-for-children%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you really need that merge? It sounds weird for updating entities when flush will be enough (unless you're merging Sales into EM before applying any changes)
– malarzm
Nov 17 '18 at 22:24
The entire data I get from the front-end and create the entities out of it so its not managed object which can be saved on flush automatically.
– Sunil Singh
Nov 18 '18 at 5:37