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

アスタリスクで囲まれた行は、1 対 1 の関係でページの親ページを参照していると思われる問題を引き起こしている行です。

しかし、次のエラーが発生します。

[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;
}

これは doctrine: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);
}

タイプミスを修正...

  • Acmeacme(名前空間)の代わりに
  • PageParentpageParent(エンティティ クラス)の代わりに

また ...

エンティティが @Entity としてタグ付けされ、リポジトリにマップされていることを確認してください。

于 2013-09-18T14:56:30.730 に答える
1

答えは実際、私が思っていたよりも簡単でした。私が抱えていた問題は2つありました。

まず、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 のセッター (doctrine によって自動生成されたにもかかわらず) が間違った場所を指していることに気付きました。エンティティ全体を参照しているはずの 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 に答える