0

我正在尝试将以下固定装置添加到我的数据库中:

$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);

由星号包围的行是导致问题的行,它应该以一对一的关系引用页面父页面。

但我收到以下错误:

[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    

错误引用的函数在此设置器上:

/**
 * 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;
}

这是使用教义从该实体自动生成的:generate:entities:

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

上面引用的“targetEntity”是容器类的名称。我不确定这是否正确,但我不知道还应该设置什么。

4

2 回答 2

1

您必须将\acme\StyleGuideBundle\Entity\pageParent对象设置为setPageParent(). 然后,您必须检索父页面,

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

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

修正错别字...

  • Acme而不是acme(命名空间)
  • PageParent而不是pageParent(实体类)

还 ...

确保您的实体被标记为 @Entity 并映射到存储库。

于 2013-09-18T14:56:30.730 回答
1

答案实际上比我预期的要简单。我遇到的问题是双重的。

首先,我在没有完全理解 Doctrine 不能那样工作的情况下使用 ID。它不需要查看 ID。它需要引用实体本身。所以我改变了这个:

$pageContent10->setPageParent(10);

对此(虽然显然首先在另一个对象上设置引用 - 注意:我必须重新排序固定装置以确保在再次调用它之前设置引用):

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

这解决了 1/2 的问题,但随后夹具又出现了另一个错误:

  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

我花了一段时间才解决这个问题,但我意识到 PageParent 的设置器(即使它是由教义自动生成的)指向了错误的位置。它引用了 PageParent 本身,它应该引用整个实体。所以这只是一个改变这个的例子:

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

对此:

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

瞧!有效!

于 2013-09-19T11:55:35.247 回答