Symfony4:フォーム内のDoctrineOneToMany関係がエラーをスローします 'クラス "Y"のプロパティ "X"のアクセスタイプを判別できませんでした'

mihyar farhat

多くのコレクションを持つ親フォームがありますが、コレクションの数を制限したいので、結果の数を8に制限する基準を作成しました。これは表示には問題なく機能しますが、編集中は機能しません。

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

「クラス「App \ Entity \ Parent」のプロパティ「Collections」のアクセスタイプを判別できませんでした:プロパティ「Collections」もメソッド「addCollections()」/「removeCollections()」、「setCollections()」のいずれもありません。 「Collections()」、「__ set()」、または「__call()」が存在し、クラス「App \ Entity \ Parent」にパブリックアクセス権があります。

これはおそらく、デフォルトの代わりにを$this->Collections->matching($criteria)返すためArrayCollectionですPersistentCollection

ここから先に進む方法がわかりません。

誰かが私にいくつかの提案や参考資料を教えてもらえますか?

/**
* @ORM\OneToMany(targetEntity="App\Entity\Collection", mappedBy="parent", cascade={"persist"}, orphanRemoval=true)
* @ORM\OrderBy({"createdOn" = "DESC"})
*/
private $Collections;

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

public function getCollections(): Collection
{
   $criteria = Criteria::create()
       ->setMaxResults(8);
   return $this->Collections->matching($criteria);
}
ニコライメリー

プロパティ$collectionsprivate(ではなくpublic)であり、そのためのsetter-methodもないため、この例外が発生します。

このように、フォームコンポーネント$collectionsは外部からの値を設定できません

setCollections次のようにメソッドを追加します。

/** @var Collection */
private $collections;

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

public function setCollections(?Collection $collections)
{
    foreach ($collections as $collection) {
        $collection->setParent($this);
    }
    $this->collections = $collections;
}

次に'by_reference' => falsecollectionsフィールドのフォームタイプにオプション設定します

あなたはおそらく、あなたの名前を変更するCollectionような、より意味のあるものに実体をCarか、Pictureおよび使用$carsまたは$picturesあまりにも、。:)

さらに、プロパティ名と大文字と小文字の不一致を再確認してください。

  • protected $collections ->小文字
  • __construct方法:コレクションS - >小文字、末尾に「s」を追加します。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ