我不明白我在API平臺上的錯誤。
DoctrineORMMappingManyToOne::__construct()。Argument #2 ($cascade) must be of type ?array, string given, called in /Users/charleslem/symfony/API-plateform-php-8/Api-plateform-php8-symfony5/vendor/doctrine/annotations/lib/Doctrine/Common/nnotations/DocParser.php on line 944.
什么引數錯誤的說法。我沒有指示。
這是我的代碼:
第一個物體:
namespace AppEntity。
use ApiPlatformCoreAnnotationApiResource;
use AppRepositoryPostRepository。
use DoctrineORMMapping as ORM;
使用 SymfonyComponent序列化器注解群組。
使用 SymfonyComponentValidatorConstraintsLength。
//read:collection pour lire une collection d'article。
//read:item correspond à la lecture d'un seul item/*ici je souhaite afficher les attributs title et slug quand je demande une collection d'article mais afficher
當我要求一個單獨的文章時,我還想顯示內容和日期。
*/
/**。
* @ORMEntity(repositoryClass=PostRepository::class)
*/
#[ApiResource()
normalizationContext:['group'=> ['read:collection']]。
itemOperations:[
'get'=> [
'normalization_context' => ['group'=> ['read: collection', 'read:item', 'read:Post']]。
'put' =>] [
'denormalization_context' => ['group' => ['put:Post'] ]。
],
],
],
)]
class Post
{
/**。
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
#[Groups(['read:collection'])]
private $id。
/**。
* @ORMColumn(type="string", length=255)
*/
#[]。
Groups(['read:collection','put:Post'])。
長度(min:5, max:255) 。
]
private $title。
/**。
* @ORMColumn(type="string", length=255)
*/
#[]。
Groups(['read:collection','put:Post'])。
長度(min:5, max:255) 。
]
private $slug;
/**。
* @ORMColumn(type="text")
*/
#[Groups(['read:item', 'put:Post'])]
private $content;
/**。
* @ORMColumn(type="datetime_immutable", nullable=true)
*/
#[Groups(['read:item', 'put:Post'])]
private $uptdatedAt;
/**.
* @ORMManyToOne(targetEntity=Category::class, inversedBy="post", cascade="persiste")
*/
#[Groups(['read:item', 'write:Post'])
]
private $category;
public function __construct()
{
$this->createdAt = new DateTime();
$this->uptdatedAt = new DateTime();
}
public function getId(): ?int
{
return $this->id。
}
public function getTitle(): ?string
{
return $this-> title。
}
public function setTitle(string $title) 。self
{
$this->title = $title;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug) 。self
{
$this->slug = $slug。
return $this;
}
public function getContent(): ?string?
{
return $this->content。
}
public function setContent(string $content) 。self
{
$this->content = $content;
return $this;
}
public function getUptdatedAt()
{
return $this->uptdatedAt;
}
public function setUptdatedAt(? DateTimeImmutable $uptdatedAt)。) self: self
{
$this->uptdatedAt = $uptdatedAt;
return $this;
}
public function getCategory(): ?category?
{
return $this-> category;
}
public function setCategory(? category $category) 。self: self.
{
$this->category = $category;
return $this;
}
而我的第二個物體 :
namespace AppEntity。
use ApiPlatformCoreAnnotationApiResource;
use AppRepositoryCategoryRepository;
use DoctrineCommonCollectionArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
使用 SymfonyComponent序列化器注解群組。
/**。
* @ApiResource()
* @ORMEntity(repositoryClass=CategoryRepository::class)
*/
class Category
{
/**。
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
#[Groups('read:Post')]
private $id;
/**。
* @ORMColumn(type="string", length=255)
*/
#[Groups('read:Post', 'write:Post')]
private $name;
/**。
* @ORMOneToMany(targetEntity=Post::class, mappedBy="category")
*/
private $posts;
public function __construct()
{
$this->post = new ArrayCollection()。
}
public function getId(): ?int
{
return $this->id。
}
public function getName(): ?string
{
return $this-> name;
}
public function setName(string $name) 。self
{
$this->name = $name;
return $this;
}
/**.
* @return Collection|Post[] 。
*/
public function getPosts()。Collection
{
return $this-> posts;
}
public function addPost(Post $post) 。self
{
if (!$this->post-> contains($post)) {
$this->post[] = $post;
$post->setCategory($this)。
}
return $this;
}
public function removePost(Post $post) : self
{
if ($this-> posts-> removeElement($post)) {
//將擁有的一方設定為null(除非已經改變)。
if ($post->getCategory() === $this) {
$post->setCategory(null) 。
}
}
return $this;
}
uj5u.com熱心網友回復:
在你的注釋中,你定義了一個ManyToOne關系,并試圖將cascade選項設定為一個字串值:
/**.
* @ORMManyToOne(targetEntity=Category::class, inversedBy="post", cascade="persiste")
*/
正如錯誤中明確指出的,它應該是一個陣列:
/**.
* @ORMManyToOne(targetEntity=Category::class, inversedBy="post", cascade={"persist"})
*/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/310775.html
標籤:
上一篇:顯示symfony的用戶串列
