Question

I am trying to add the following fixtures to my database:

$pageContent1 = new pageContent();
$pageContent1->addPageSector($this->getReference('ps1'));
$pageContent1->addPageSector($this->getReference('ps2'));
$pageContent1->setPageTypes($this->getReference('pt2'));
*$pageContent1->setPageParent(10);*
$pageContent1->setPageName('Paragraphs');
$pageContent1->setRichText('<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>');
$manager->persist($pageContent1);

The line surrounded by asterisks is the line which is causing the problem it is supposed to be referencing the pages parent page in a one-to-one relationhip.

But I am getting the following error:

[Symfony\Component\Debug\Exception\ContextErrorException]                    
  Catchable Fatal Error: Argument 1 passed to acme\StyleGuideBundle\Entity  
  \pageContent::setPageParent() must be an instance of acme\StyleGuideBund  
  le\Entity\pageParent, integer given, called in /Volumes/Projects/Style Guid  
  e/site/acmeGuidelines/src/acme/StyleGuideBundle/DataFixtures/ORM/Page  
  sContentFixtures.php on line 21 and defined in /Volumes/Projects/Style Guid  
  e/site/acmeGuidelines/src/acme/StyleGuideBundle/Entity/pageContent.ph  
  p line 300    

The function the error references is on this setter:

/**
 * Set pageParent
 *
 * @param \acme\StyleGuideBundle\Entity\pageParent $pageParent The parent page of this page
 *
 * @return pageContent
 */
public function setPageParent(\acme\StyleGuideBundle\Entity\pageParent $pageParent = null)
{
    $this->pageParent = $pageParent;

    return $this;
}

and this was automatically generated from this entity using doctrine:generate:entities:

/**
 * @ORM\OneToOne(targetEntity="pageContent")
 * @ORM\JoinColumn(name="pageParent", referencedColumnName="id")
 */
    private $pageParent;

The 'targetEntity' referenced above is the name of the container class. I wasn't sure if that was correct but I didn't know what else it should be set to.

Was it helpful?

Solution 2

The answer was actually simpler than I expected it to be. The problem I was having was twofold.

Firstly I was using an ID without fully understanding that Doctrine doesn't work that way. It doesn't need to see the ID. It needs to reference the entity itself. So I changed this:

$pageContent10->setPageParent(10);

to this (whilst obviously setting the reference on the other object first - Note: I had to re-order the fixtures to make sure I was setting the reference before I called it again):

$pageContent10->setPageParent($this->getReference('pc1'));

That fixed 1/2 the problem but then the fixture was coming back with another error:

  Catchable Fatal Error: Argument 1 passed to acme\StyleGuideBundle\Entity  
  \PageContent::setPageParent() must be an instance of acme\StyleGuideBund  
  le\Entity\PageParent, instance of acme\StyleGuideBundle\Entity\PageConte  
  nt given, called in /Volumes/Projects/Style Guide/site/AcmeGuidelines/sr  
  c/acme/StyleGuideBundle/DataFixtures/ORM/PagesContentFixtures.php on lin  
  e 114 and defined in /Volumes/Projects/Style Guide/site/AcmeGuidelines/s  
  rc/acme/StyleGuideBundle/Entity/pageContent.php line 300

It took me a while to work it out but I realised that the setter for PageParent (even though it was auto-generated by doctrine) was pointing to the wrong place. It was referencing PageParent itself, where it should have been referencing the entire entity. So that was simply a case of changing this:

public function setPageParent(\acme\StyleGuideBundle\Entity\PageParent $PageParent = null)

To this:

public function setPageParent(\acme\StyleGuideBundle\Entity\PageContent $PageParent = null)

And voila! It worked!

OTHER TIPS

You've to set an \acme\StyleGuideBundle\Entity\pageParent object to setPageParent(). Then, you've to retrieve the parent page,

$parentPage = $this->getDoctrine()->getRepository('AcmeStyleGuideBundle:PageParent')->find(10);

if ($parentPage) {
    $pageContent1->setPageParent($parentPage);
}

Fix typos ...

  • Acme instead of acme (namespace)
  • PageParent instead of pageParent (entity class)

Also ...

Make sure your entity is tagged as @Entity and mapped with a repository.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top