1

問題を解決しようとするあらゆる試みを無視する問題に遭遇しました。それはおそらく簡単です、私はこれで完全に疲れ果てました:)

基本的に、ユーザーがモジュール (Facebook ID、略歴、テキスト入力) とアセット (画像ロゴ、PDF など) をページ オブジェクトに追加できるようにしたいと考えています。

モジュールとアセットからページへの一対一の関係を設定しました。

モジュールは期待どおりに機能しますが、アセットはまったく機能しません。PageController がそのエンティティから getAsset() を呼び出すと、null になります。アセットを繰り返し処理するまで、エラーは発生しません。

また、PageController には、次の名前空間宣言があります。

use Linkme\SiteBundle\Entity\Module;
use Linkme\SiteBundle\Entity\Asset;

Module 宣言を削除するとエラーが発生しますが、Asset 行を削除しても何も変わりません。したがって、私は関係が作成されていないと考えています。

私が走れば

app/console doctrine:schema:create --dump-sql

次に、とりわけ次の行を取得します。

ALTER TABLE Asset ADD CONSTRAINT FK_C36E75589D3B65E3 FOREIGN KEY (pageId) REFERENCES Page(id);

これにより、スキーマが正しいと思います。アセットがページに関連していることを少なくとも理解しています

タイプミスがあると感じ始めているか、明らかな何かが完全に欠けていると感じ始めています-トラブルシューティングやその他の提案に関する支援があれば大歓迎です!

app/console --version
Symfony version 2.0.1 - app/dev/debug

Page.php

/*
 * @ORM\OneToMany(targetEntity="Asset", mappedBy="pageId", cascade={"persist", "remove"})
 * @ORM\OrderBy({"type" = "ASC"})
 * @ORM\OrderBy({"id" = "ASC"})
 * 
 * @var ArrayCollection $assets
 */
protected $assets;

/**
 * @ORM\OneToMany(targetEntity="Module", mappedBy="pageId", cascade={"persist", "remove"})
 * @ORM\OrderBy({"type" = "ASC"})
 * @ORM\OrderBy({"id" = "ASC"})
 *
 * @var ArrayCollection $modules
 */
protected $modules;

/**
 * Set assets
 * @param Asset $assets
 */
public function setAssets(Asset $assets = null)
{
    $this->assets = $assets;
}

/**
 * Get assets
 *
 * @return Asset
 */
public function getAssets()
{
    echo '<br />Asset is '.gettype($this->assets); //   outut:  Asset is NULL
    return $this->assets;
}

/**
 * Set modules
 * @param Module $modules
 */
public function setModules(Module $modules = null)
{
    $this->modules = $modules;
}

/**
 * Get modules
 * @return Module
 */
public function getModules()
{
    echo '<br />Module is '.gettype($this->assets); //   output:  Module is object
    return $this->modules;
}

Asset.php

/**
 * @var integer $pageId
 *
 * @ORM\ManyToOne(targetEntity="Page", inversedBy="assets")
 * @ORM\JoinColumn(name="pageId", referencedColumnName="id")
 */
protected $pageId;

Module.php

/**
 * @var integer $pageId
 *
 * @ORM\ManyToOne(targetEntity="Page", inversedBy="modules")
 * @ORM\JoinColumn(name="pageId", referencedColumnName="id")
 */
protected $pageId;

PageController.php

use Linkme\SiteBundle\Entity\Module;
use Linkme\SiteBundle\Entity\Asset;

/**
 * Add modules  and assets to a page
 *
 * @Route("/{id}/wizardmodule", name="page_wizardmodule")
 * @Template()
 */
public function wizardmoduleAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $page = $em->getRepository('LinkmeSiteBundle:Page')->find($id);
    $modules = $page->getModules();
    $assets = $page->getAssets();        

depmod

[symfony]
    git=http://github.com/symfony/symfony.git
    version=v2.0.1

[twig]
    git=http://github.com/fabpot/Twig.git
    version=v1.1.2

[monolog]
    git=http://github.com/Seldaek/monolog.git
    version=1.0.1

[doctrine-common]
    git=http://github.com/doctrine/common.git
    version=2.1.1

[doctrine-dbal]
    git=http://github.com/doctrine/dbal.git
    version=2.1.1

[doctrine]
    git=http://github.com/doctrine/doctrine2.git
    version=2.1.1

[swiftmailer]
    git=http://github.com/swiftmailer/swiftmailer.git
    version=v4.1.1

[assetic]
    git=http://github.com/kriswallsmith/assetic.git
    version=v1.0.1

[twig-extensions]
    git=http://github.com/fabpot/Twig-extensions.git

[metadata]
    git=http://github.com/schmittjoh/metadata.git
    version=1.0.0

[SensioFrameworkExtraBundle]
    git=http://github.com/sensio/SensioFrameworkExtraBundle.git
    target=/bundles/Sensio/Bundle/FrameworkExtraBundle
    version=v2.0.1

[JMSSecurityExtraBundle]
    git=http://github.com/schmittjoh/JMSSecurityExtraBundle.git
    target=/bundles/JMS/SecurityExtraBundle
    version=v2.0.1

[SensioDistributionBundle]
    git=http://github.com/sensio/SensioDistributionBundle.git
    target=/bundles/Sensio/Bundle/DistributionBundle
    version=v2.0.1

[SensioGeneratorBundle]
    git=http://github.com/sensio/SensioGeneratorBundle.git
    target=/bundles/Sensio/Bundle/GeneratorBundle
    version=v2.0.1

[AsseticBundle]
    git=http://github.com/symfony/AsseticBundle.git
    target=/bundles/Symfony/Bundle/AsseticBundle
    version=v1.0.0
4

3 に答える 3

2

わかった!そして私が予測したように、それは信じられないほど迷惑なPEBKACでした...。

注釈が読み取られていなかったため、関係が作成されていませんでした。注釈のコメントボックスに*がなかったためです..... ddddoooohhhhh!

Page.php

前:

/*      <==========================   there is only one * here. It needs to be two: **
 * @ORM\OneToMany(targetEntity="Asset", mappedBy="pageId", cascade={"persist", "remove"})
 * @ORM\OrderBy({"type" = "ASC"})
 * @ORM\OrderBy({"id" = "ASC"})
 *
 * @var ArrayCollection $assets
 */
protected $assets;

後:

/**      <==========================   like this.
 * @ORM\OneToMany(targetEntity="Asset", mappedBy="pageId", cascade={"persist", "remove"})
 * @ORM\OrderBy({"type" = "ASC"})
 * @ORM\OrderBy({"id" = "ASC"})
 *
 * @var ArrayCollection $assets
 */
protected $assets;

この問題を手伝ってくれたすべての人に心から感謝します。

于 2012-06-01T22:19:07.423 に答える
0

これを確認してください: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional

コレクションをページ コンストラクターに正しく初期化していますか?

于 2012-05-30T06:52:04.470 に答える
0

これは、Page コンストラクターで $assets コレクションを初期化していないためです。

public function __construct(){
    $this->assets = new ArrayCollection();
}

それから doctrine:generate:entities コマンドを実行していないと思います。マップされたフィールドごとに get、set、および add メソッドを作成することで、あなたの生活が少し簡素化されます。あなたの場合、それは

public function addModules(Module $modules)
{
    $this->modules[] = $modules;
}

実際には $modules を $this->modules に割り当てるだけであることに注意してください。これは間違っています。これはスカラーではなく配列です。

また、すべてのモジュールによって参照されるページへの参照を追加するには、別の命令を追加する必要があります。

public function addModules(Module $modules)
{
    $this->modules[] = $modules;
    $modules->setPage($this);
}

私は自分のコードでこれを実行しましたが、うまくいきました。あなたにもうまくいくかどうか教えてください。

Linuxatico

于 2012-05-30T11:07:46.010 に答える