- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个上传文件,我想从那里显示数据库中的所有类别,但我一直收到 EntityType 错误,我不知道为什么以前工作。
这是错误:Cannot assign Doctrine\ORM\PersistentCollection to property App\Entity\Category::$posts of type Doctrine\Common\Collections\ArrayCollection
<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=CategoryRepository::class)
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private int $id;
/**
* @ORM\Column(type="string", length=255)
*/
private string $name;
/**
* @ORM\OneToMany(targetEntity=Post::class, mappedBy="Category_id")
*/
private ArrayCollection $Posts;
public function __construct()
{
$this->posts = 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 gePosts(): Collection
{
return $this->posts;
}
public function addPost(Post $post): self
{
if (!$this->posts->contains($post)) {
$this->$posts[] = $posts;
$post->setCategoryId($this);
}
return $this;
}
public function removePost(Post $post): self
{
if ($this->posts->removeElement($post)) {
// set the owning side to null (unless already changed)
if ($post->getCategoryId() === $this) {
$post->setCategoryId(null);
}
}
return $this;
}
public function __toString(): string
{
return (string) $this->getName();
}
}
我收到错误的上传表单
<?php
declare(strict_types=1);
namespace App\Form\Post;
use App\Entity\Category;
use App\Entity\Post;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\NotBlank;
class UploadType extends AbstractType
{
/**
* @param \Symfony\Component\Form\FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'constraints' => [
new NotBlank([
'message' => 'Please enter a valid Title. '
])
]
])
->add('category', EntityType::class, [
'mapped' => false,
'class' => Category::class,
'choice_label' => 'name',
'expanded' => true
])
->add('poster', FileType::class, [
'required' => false,
'constraints' => [
new File([
'maxSize' => '6000K',
'mimeTypes' => [
'image/x-png',
'image/jpeg',
],
'mimeTypesMessage' => 'Please upload a image file.'
])
]
])
->add('description', TextareaType::class, [
'constraints' => [
new NotBlank([
'message' => 'Please enter a valid Title. '
])
]
])
->add('mediainfo', TextareaType::class, [
'required' => false,
]);
}
/**
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
在 Post 实体中
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts")
*/
private Category $Category_id;
环境信息:
最佳答案
经过一番挖掘找到答案,将 ArrayCollection 更改为 Collection 解决了我的问题
change:
/**
* @ORM\OneToMany(targetEntity=Post::class, mappedBy="Category_id")
*/
private \Doctrine\Common\Collections\ArrayCollection $posts;
to:
/**
* @ORM\OneToMany(targetEntity=Post::class, mappedBy="Category_id")
*/
private \Doctrine\Common\Collections\Collection $posts;
关于php - 学说分配错误 : Cannot assign Doctrine\ORM\PersistentCollection to property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66695884/
我想知道存储库在 Doctrine 中意味着什么? 有人可以解释一下吗? 最佳答案 许多 ORM(对象关系映射器)使用的术语“存储库”, Doctrine 只是其中之一。 它意味着可以从数据存储库访问
我尝试使用 进行数据库查询 $position = $repository->findBy( array('id' => $profileId,'datum' => '10.07.2011')
几个月前,有一个命令可以将 Doctrine 1 架构文件转换为 Doctrine 2 实体,但是,该命令不再存在。 我刚刚开始使用 Symfony2 RC1。 有没有被删除的原因?我有一个 600
我有一个实体,称为 Stones,Stones 与 Attributes 有 ManyToMany 关系。 所以我查询实体以获取石头,然后将其水化以将其转换为数组。 $result = $t
我在 zend 框架 2 中使用了学说 2。要使用数据库表生成实体,使用的控制台命令是: php doctrine-module orm:convert-mapping --force --from-
我需要一个具体的代码示例,其中包含使用“多态关联”的 Doctrine 2。 让我澄清一下自己。我有一个名为 Contract 的实体,一个契约(Contract)可以有许多价格规则,这些价格规则可以
我知道条件过滤器尚不可用于查询(根据 Doctrine2 手册已知限制部分中的“26.1.4. Applying Filter Rules to any Query”),所以我想问问专家他们首选的解决
我在所有网络上搜索..但没有找到这个问题的回复。为了记录我的项目实体中的更改,我想使用 stof 条令扩展 - 可记录,但对我来说并不明显,实体的更改如何存储在数据库中?我可以定义我自己的历史表并在那
我使用两个类: namespace Test; use Doctrine\ORM\Mapping as ORM; /** *@Table() *@InheritanceType("Joined")
A previous question I asked与使用 Doctrine 和查询生成器时对结果集进行水化有关。我的问题是如何返回数组及其子集: 这是针对单个结果集的,答案很简单: $qb
我有两个实体 User和 Article多对多关系为 Article可以有很多作者。 class User { /** @var string */ public $name;
我有一个看起来像这样的查询: 我的用户实体具有如下所示的一对一关系: /** * @var UserProfile * * @ORM\OneToOne(targetEntity="UserPro
我最近正在阅读Doctrine 2's best practices并被此阻止: 25.3. Avoid composite keys Even though Doctrine fully suppo
如何在服务容器中使用 Doctrine? 该代码只会导致错误消息“ fatal error :调用未定义的方法 ...::get()”。 em = $em; } public func
向一条记录加载 Doctrine 的首选方式是什么? 我正在使用 $em = EntityManager::create($connectionOptions, $config); $dql = "s
我正在使用 Doctrine,并且想要记录所有生成的 SQL 查询。 我知道我可以使用 $q->getSqlQuery() 但我不想每次都手动执行此操作。 有没有办法自动完成? 最佳答案 如果您打开日
如何在 Doctrine 中找到分组最大值或包含最大值的行?在 SQL 中,我通常会使用自连接来执行此操作,如 here 中所述。 . 虽然可以在 Doctrine 中建立 self 关系,但有更好的
我在谷歌上浪费了数万亿个小时,但没有一个解决方案是好的。 我有这个查询生成器: $qb2=$this->createQueryBuilder('s') ->addSel
我有以下问题: 实体具有经典的创建/更新时间戳字段,应将其设置为 UTC 中的当前时间戳。 在实体上,我使用 columnDefinition 属性将它们设置为 "TIMESTAMP NOT NULL
我需要从多个相关表中执行 DQL 删除。 在 SQL 中,它是这样的: DELETE r1,r2 FROM ComRealty_objects r1, com_realty_objects_p
我是一名优秀的程序员,十分优秀!