Type error with ArrayCollection / OneToMany relationship in Symfony 3.4
For the past couple of days I have been trying to create a bidirectionnal ManyToOne-OneToMany relationship in Symfony 3.4
I have two entities. One is Contribution and the other is Source. A Contribution can have several sources. So the relationship should be
Contribution – ManyToOne – Source – OneToMany – Contribution
But I keep getting the following error during $em→flush();
in my controller:
Type error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given, called in /var/www/html/Edebate/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605
I do not have any set method related to the Array Collection in my Entity Contribution as I could see in other posts here:
Type error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given
Symfony-Catchable Fatal Error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given
And the annotations are ok as mentionned here:
Doctrine OneToMany relationship error
Any help would be appreciate ! :)
Here is my Entity Contribution
use DoctrineCommonCollectionsArrayCollection;
//annotations
abstract class Contribution
{
/**
* @ORMOneToMany(targetEntity="ShakerDebateBundleEntitySource", mappedBy="parent")
*/
protected $sources;
//Other attributes and methods
public function __construct() {
$this->sources = new ArrayCollection();
}
/**
* Add source
*
* @param ShakerDebateBundleEntitySource $source
*
* @return Contribution
*/
public function addSource(ShakerDebateBundleEntitySource $source)
{
$this->sources = $source;
return $this;
}
/**
* Remove source
*
* @param ShakerDebateBundleEntitySource $source
*/
public function removeSource(ShakerDebateBundleEntitySource $source)
{
$this->sources->removeElement($source);
}
/**
* Get sources
*
* @return DoctrineCommonCollectionsCollection
*/
public function getSources()
{
return $this->sources;
}
}
And this is in my Entity Source:
/**
* @ORMManyToOne(targetEntity="ShakerDebateBundleEntityContribution", inversedBy="sources")
*/
protected $parent;
/**
* Set parent
*
* @param ShakerDebateBundleEntityContribution $parent
*
* @return Contribution
*/
public function setParent(ShakerDebateBundleEntityContribution $parent = null)
{
$this->parent = $parent;
$parent->addSource($this);
return $this;
}
/**
* Get parent
*
* @return ShakerJRQBundleEntityContribution
*/
public function getParent()
{
return $this->parent;
}
And in my Controller, the problem arises with flush:
$formsourcebook->handleRequest($request);
$contributionid=$formsourcebook->get('ContributionId')->getData();
if ($formsourcebook->isValid()) {
$topicargtarget=$this->getContribution($contributionid);
$sourcebook->setUser($user);
$sourcebook->setContribution($topicargtarget);
$em->persist($sourcebook);
$em->flush();
}
symfony doctrine-orm arraycollection
add a comment |
For the past couple of days I have been trying to create a bidirectionnal ManyToOne-OneToMany relationship in Symfony 3.4
I have two entities. One is Contribution and the other is Source. A Contribution can have several sources. So the relationship should be
Contribution – ManyToOne – Source – OneToMany – Contribution
But I keep getting the following error during $em→flush();
in my controller:
Type error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given, called in /var/www/html/Edebate/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605
I do not have any set method related to the Array Collection in my Entity Contribution as I could see in other posts here:
Type error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given
Symfony-Catchable Fatal Error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given
And the annotations are ok as mentionned here:
Doctrine OneToMany relationship error
Any help would be appreciate ! :)
Here is my Entity Contribution
use DoctrineCommonCollectionsArrayCollection;
//annotations
abstract class Contribution
{
/**
* @ORMOneToMany(targetEntity="ShakerDebateBundleEntitySource", mappedBy="parent")
*/
protected $sources;
//Other attributes and methods
public function __construct() {
$this->sources = new ArrayCollection();
}
/**
* Add source
*
* @param ShakerDebateBundleEntitySource $source
*
* @return Contribution
*/
public function addSource(ShakerDebateBundleEntitySource $source)
{
$this->sources = $source;
return $this;
}
/**
* Remove source
*
* @param ShakerDebateBundleEntitySource $source
*/
public function removeSource(ShakerDebateBundleEntitySource $source)
{
$this->sources->removeElement($source);
}
/**
* Get sources
*
* @return DoctrineCommonCollectionsCollection
*/
public function getSources()
{
return $this->sources;
}
}
And this is in my Entity Source:
/**
* @ORMManyToOne(targetEntity="ShakerDebateBundleEntityContribution", inversedBy="sources")
*/
protected $parent;
/**
* Set parent
*
* @param ShakerDebateBundleEntityContribution $parent
*
* @return Contribution
*/
public function setParent(ShakerDebateBundleEntityContribution $parent = null)
{
$this->parent = $parent;
$parent->addSource($this);
return $this;
}
/**
* Get parent
*
* @return ShakerJRQBundleEntityContribution
*/
public function getParent()
{
return $this->parent;
}
And in my Controller, the problem arises with flush:
$formsourcebook->handleRequest($request);
$contributionid=$formsourcebook->get('ContributionId')->getData();
if ($formsourcebook->isValid()) {
$topicargtarget=$this->getContribution($contributionid);
$sourcebook->setUser($user);
$sourcebook->setContribution($topicargtarget);
$em->persist($sourcebook);
$em->flush();
}
symfony doctrine-orm arraycollection
Has anybody any idea what the error means ? I'm still stuck with this problem and it seems to be a very general error log.
– Shaker81
Nov 27 '18 at 21:30
add a comment |
For the past couple of days I have been trying to create a bidirectionnal ManyToOne-OneToMany relationship in Symfony 3.4
I have two entities. One is Contribution and the other is Source. A Contribution can have several sources. So the relationship should be
Contribution – ManyToOne – Source – OneToMany – Contribution
But I keep getting the following error during $em→flush();
in my controller:
Type error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given, called in /var/www/html/Edebate/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605
I do not have any set method related to the Array Collection in my Entity Contribution as I could see in other posts here:
Type error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given
Symfony-Catchable Fatal Error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given
And the annotations are ok as mentionned here:
Doctrine OneToMany relationship error
Any help would be appreciate ! :)
Here is my Entity Contribution
use DoctrineCommonCollectionsArrayCollection;
//annotations
abstract class Contribution
{
/**
* @ORMOneToMany(targetEntity="ShakerDebateBundleEntitySource", mappedBy="parent")
*/
protected $sources;
//Other attributes and methods
public function __construct() {
$this->sources = new ArrayCollection();
}
/**
* Add source
*
* @param ShakerDebateBundleEntitySource $source
*
* @return Contribution
*/
public function addSource(ShakerDebateBundleEntitySource $source)
{
$this->sources = $source;
return $this;
}
/**
* Remove source
*
* @param ShakerDebateBundleEntitySource $source
*/
public function removeSource(ShakerDebateBundleEntitySource $source)
{
$this->sources->removeElement($source);
}
/**
* Get sources
*
* @return DoctrineCommonCollectionsCollection
*/
public function getSources()
{
return $this->sources;
}
}
And this is in my Entity Source:
/**
* @ORMManyToOne(targetEntity="ShakerDebateBundleEntityContribution", inversedBy="sources")
*/
protected $parent;
/**
* Set parent
*
* @param ShakerDebateBundleEntityContribution $parent
*
* @return Contribution
*/
public function setParent(ShakerDebateBundleEntityContribution $parent = null)
{
$this->parent = $parent;
$parent->addSource($this);
return $this;
}
/**
* Get parent
*
* @return ShakerJRQBundleEntityContribution
*/
public function getParent()
{
return $this->parent;
}
And in my Controller, the problem arises with flush:
$formsourcebook->handleRequest($request);
$contributionid=$formsourcebook->get('ContributionId')->getData();
if ($formsourcebook->isValid()) {
$topicargtarget=$this->getContribution($contributionid);
$sourcebook->setUser($user);
$sourcebook->setContribution($topicargtarget);
$em->persist($sourcebook);
$em->flush();
}
symfony doctrine-orm arraycollection
For the past couple of days I have been trying to create a bidirectionnal ManyToOne-OneToMany relationship in Symfony 3.4
I have two entities. One is Contribution and the other is Source. A Contribution can have several sources. So the relationship should be
Contribution – ManyToOne – Source – OneToMany – Contribution
But I keep getting the following error during $em→flush();
in my controller:
Type error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given, called in /var/www/html/Edebate/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605
I do not have any set method related to the Array Collection in my Entity Contribution as I could see in other posts here:
Type error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given
Symfony-Catchable Fatal Error: Argument 1 passed to DoctrineCommonCollectionsArrayCollection::__construct() must be of the type array, object given
And the annotations are ok as mentionned here:
Doctrine OneToMany relationship error
Any help would be appreciate ! :)
Here is my Entity Contribution
use DoctrineCommonCollectionsArrayCollection;
//annotations
abstract class Contribution
{
/**
* @ORMOneToMany(targetEntity="ShakerDebateBundleEntitySource", mappedBy="parent")
*/
protected $sources;
//Other attributes and methods
public function __construct() {
$this->sources = new ArrayCollection();
}
/**
* Add source
*
* @param ShakerDebateBundleEntitySource $source
*
* @return Contribution
*/
public function addSource(ShakerDebateBundleEntitySource $source)
{
$this->sources = $source;
return $this;
}
/**
* Remove source
*
* @param ShakerDebateBundleEntitySource $source
*/
public function removeSource(ShakerDebateBundleEntitySource $source)
{
$this->sources->removeElement($source);
}
/**
* Get sources
*
* @return DoctrineCommonCollectionsCollection
*/
public function getSources()
{
return $this->sources;
}
}
And this is in my Entity Source:
/**
* @ORMManyToOne(targetEntity="ShakerDebateBundleEntityContribution", inversedBy="sources")
*/
protected $parent;
/**
* Set parent
*
* @param ShakerDebateBundleEntityContribution $parent
*
* @return Contribution
*/
public function setParent(ShakerDebateBundleEntityContribution $parent = null)
{
$this->parent = $parent;
$parent->addSource($this);
return $this;
}
/**
* Get parent
*
* @return ShakerJRQBundleEntityContribution
*/
public function getParent()
{
return $this->parent;
}
And in my Controller, the problem arises with flush:
$formsourcebook->handleRequest($request);
$contributionid=$formsourcebook->get('ContributionId')->getData();
if ($formsourcebook->isValid()) {
$topicargtarget=$this->getContribution($contributionid);
$sourcebook->setUser($user);
$sourcebook->setContribution($topicargtarget);
$em->persist($sourcebook);
$em->flush();
}
symfony doctrine-orm arraycollection
symfony doctrine-orm arraycollection
edited Dec 3 '18 at 14:36
Shaker81
asked Nov 19 '18 at 19:37
Shaker81Shaker81
186
186
Has anybody any idea what the error means ? I'm still stuck with this problem and it seems to be a very general error log.
– Shaker81
Nov 27 '18 at 21:30
add a comment |
Has anybody any idea what the error means ? I'm still stuck with this problem and it seems to be a very general error log.
– Shaker81
Nov 27 '18 at 21:30
Has anybody any idea what the error means ? I'm still stuck with this problem and it seems to be a very general error log.
– Shaker81
Nov 27 '18 at 21:30
Has anybody any idea what the error means ? I'm still stuck with this problem and it seems to be a very general error log.
– Shaker81
Nov 27 '18 at 21:30
add a comment |
1 Answer
1
active
oldest
votes
I don't know your question very well. However, did you try with this syntax in the Source entity?
private $parent;
// ...
public function __construct() {
$this->parent = new ArrayCollection();
// or new DoctrineCommonCollectionsArrayCollection();
}
I think you're forgetting the constructor in the class.
Yes I have a constructor but in the Entity "Contribution" for the attribute "Sources" as one contribution can have several sources. It did not appear in my first post so I edited it.
– Shaker81
Nov 20 '18 at 13:48
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%2f53381494%2ftype-error-with-arraycollection-onetomany-relationship-in-symfony-3-4%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
I don't know your question very well. However, did you try with this syntax in the Source entity?
private $parent;
// ...
public function __construct() {
$this->parent = new ArrayCollection();
// or new DoctrineCommonCollectionsArrayCollection();
}
I think you're forgetting the constructor in the class.
Yes I have a constructor but in the Entity "Contribution" for the attribute "Sources" as one contribution can have several sources. It did not appear in my first post so I edited it.
– Shaker81
Nov 20 '18 at 13:48
add a comment |
I don't know your question very well. However, did you try with this syntax in the Source entity?
private $parent;
// ...
public function __construct() {
$this->parent = new ArrayCollection();
// or new DoctrineCommonCollectionsArrayCollection();
}
I think you're forgetting the constructor in the class.
Yes I have a constructor but in the Entity "Contribution" for the attribute "Sources" as one contribution can have several sources. It did not appear in my first post so I edited it.
– Shaker81
Nov 20 '18 at 13:48
add a comment |
I don't know your question very well. However, did you try with this syntax in the Source entity?
private $parent;
// ...
public function __construct() {
$this->parent = new ArrayCollection();
// or new DoctrineCommonCollectionsArrayCollection();
}
I think you're forgetting the constructor in the class.
I don't know your question very well. However, did you try with this syntax in the Source entity?
private $parent;
// ...
public function __construct() {
$this->parent = new ArrayCollection();
// or new DoctrineCommonCollectionsArrayCollection();
}
I think you're forgetting the constructor in the class.
answered Nov 19 '18 at 20:37


SimoBrazzSimoBrazz
265
265
Yes I have a constructor but in the Entity "Contribution" for the attribute "Sources" as one contribution can have several sources. It did not appear in my first post so I edited it.
– Shaker81
Nov 20 '18 at 13:48
add a comment |
Yes I have a constructor but in the Entity "Contribution" for the attribute "Sources" as one contribution can have several sources. It did not appear in my first post so I edited it.
– Shaker81
Nov 20 '18 at 13:48
Yes I have a constructor but in the Entity "Contribution" for the attribute "Sources" as one contribution can have several sources. It did not appear in my first post so I edited it.
– Shaker81
Nov 20 '18 at 13:48
Yes I have a constructor but in the Entity "Contribution" for the attribute "Sources" as one contribution can have several sources. It did not appear in my first post so I edited it.
– Shaker81
Nov 20 '18 at 13:48
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53381494%2ftype-error-with-arraycollection-onetomany-relationship-in-symfony-3-4%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
Has anybody any idea what the error means ? I'm still stuck with this problem and it seems to be a very general error log.
– Shaker81
Nov 27 '18 at 21:30