doctrine 相关问题

Doctrine Project是一个开源库和工具的集合,用于处理用PHP编写的数据库抽象和对象关系映射。

在 Symfony 5 迁移类中访问实体管理器?

我在表中添加了新闻关系,并且在 postUp() 方法中我想更新所有线程。 我如何访问迁移类中的 EntityManager 以获取我的所有线程,更新...

回答 1 投票 0

未定义的方法 getDoctrine

我是 Symfony 6 的初学者,我被阻止了,因为我收到一条错误消息:“Undefined method getDoctrine” with Intelephense 这是我的代码: #[路线('/recettes', 名称: 'app_recettes'...

回答 4 投票 0

Symfony 和 Doctrine 迁移

这就是我被困住的地方: 我正在调用一个使用最新版本执行学说迁移的 api。 它运行一个命令。 当我手动执行命令时,这里是输出(THE JOB IS DO...

回答 1 投票 0

如何替换typo3已弃用的注释@validate Boolean(is=true)?

我必须将 Typo3 v9 项目升级到 Typo3 v10。 像以前一样的注释已被弃用。我知道如何用 @TYPO3\CMS\Extbase\Annotation\Vali 替换 @validate NotEmpty 之类的东西...

回答 1 投票 0

Symfony 学说错误“DoctrineMigrationsBundle 需要启用 DoctrineBundle。”

我创建了一个新的 Symfony 项目,并且不断收到“DoctrineMigrationsBundle 需要启用 DoctrineBundle”。错误并且无法摆脱它。看来我是唯一的一个...

回答 2 投票 0

Doctrine 迁移不会为某个特定实体生成迁移版本

我正在使用 symfony,并且我正在尝试使用以下命令生成新的迁移版本: 应用程序/控制台学说:迁移:diff 它正在生成新的迁移文件,但有一个表...

回答 2 投票 0

向实体添加关系会导致奇怪的 ORM 映射错误

在 Symfony Api 平台应用程序中,使用 Maker Bundle 将关系字段“Owner”添加到“Community”实体导致所有“Get”api 调用返回相同的错误。 ...

回答 2 投票 0

使用 `urlencode` 的 Symfony 4 Flex 数据库连接

我正在使用 Symfony 4 + Flex 开始一个新项目。此时我正在尝试将我的新应用程序连接到 MySQL 数据库。 我正在关注 Symfony 文档,并且我已经将原则添加到我的

回答 2 投票 0

为什么我在 symfony 中遇到虚拟属性错误?

我有这个实体 我有这个实体 <?php namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; #[ApiResource( normalizationContext: ["groups" => ["contact", "contact_read"]], denormalizationContext: ["groups" => ["contact", "contact_write"]] )] #[ORM\Entity] class Contact { // ... #[ORM\OneToMany(targetEntity: Email::class, mappedBy: "contact")] #[Groups(["contact"])] private $emails; // ... public function __construct() { $this->emails = new ArrayCollection(); } // ... public function getEmails() { return $this->emails; } // ... } 我想添加虚拟财产 #[Groups(["contact"])] public function getPrimaryEmail() { $criteria = Criteria::create()->where(Criteria::expr()->eq("is_primary", true)); $matches = $this->emails->matching($criteria); return ($matches->count()) ? $matches->first()->getAccount() : null; } 并且它有效......但如果方法的名称是getEmail(),则无效 #[Groups(["contact"])] public function getEmail() { $criteria = Criteria::create()->where(Criteria::expr()->eq("is_primary", true)); $matches = $this->emails->matching($criteria); return ($matches->count()) ? $matches->first()->getAccount() : null; } 在这种情况下我会收到此错误 多对关系的意外不可迭代值 这是为什么呢?有没有办法让方法保持getEmail()? 我正在使用 API Platform 3.2.10 和 symfony 6.4.1 完全错误: { "@id": "/errors/400", "@type": "hydra:Error", "title": "An error occurred", "detail": "Unexpected non-iterable value for to-many relation.", "status": 400, "type": "/errors/400", "trace": [ { "file": "vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php", "line": 192, "function": "getAttributeValue", "class": "ApiPlatform\\Serializer\\AbstractItemNormalizer", "type": "->" }, { "file": "vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php", "line": 171, "function": "normalize", "class": "Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer", "type": "->" }, { "file": "vendor/api-platform/core/src/JsonLd/Serializer/ItemNormalizer.php", "line": 111, "function": "normalize", "class": "ApiPlatform\\Serializer\\AbstractItemNormalizer", "type": "->" }, { "file": "vendor/symfony/serializer/Debug/TraceableNormalizer.php", "line": 58, "function": "normalize", "class": "ApiPlatform\\JsonLd\\Serializer\\ItemNormalizer", "type": "->" }, { "file": "vendor/symfony/serializer/Serializer.php", "line": 159, "function": "normalize", "class": "Symfony\\Component\\Serializer\\Debug\\TraceableNormalizer", "type": "->" }, { "file": "vendor/api-platform/core/src/Hydra/Serializer/CollectionNormalizer.php", "line": 91, "function": "normalize", "class": "Symfony\\Component\\Serializer\\Serializer", "type": "->" }, { "file": "vendor/api-platform/core/src/Serializer/AbstractCollectionNormalizer.php", "line": 106, "function": "getItemsData", "class": "ApiPlatform\\Hydra\\Serializer\\CollectionNormalizer", "type": "->" }, { "file": "vendor/api-platform/core/src/Hydra/Serializer/PartialCollectionViewNormalizer.php", "line": 50, "function": "normalize", "class": "ApiPlatform\\Serializer\\AbstractCollectionNormalizer", "type": "->" }, { "file": "vendor/api-platform/core/src/Hydra/Serializer/CollectionFiltersNormalizer.php", "line": 91, "function": "normalize", "class": "ApiPlatform\\Hydra\\Serializer\\PartialCollectionViewNormalizer", "type": "->" }, { "file": "vendor/symfony/serializer/Debug/TraceableNormalizer.php", "line": 58, "function": "normalize", "class": "ApiPlatform\\Hydra\\Serializer\\CollectionFiltersNormalizer", "type": "->" }, { "file": "vendor/symfony/serializer/Serializer.php", "line": 159, "function": "normalize", "class": "Symfony\\Component\\Serializer\\Debug\\TraceableNormalizer", "type": "->" }, { "file": "vendor/symfony/serializer/Serializer.php", "line": 138, "function": "normalize", "class": "Symfony\\Component\\Serializer\\Serializer", "type": "->" }, { "file": "vendor/symfony/serializer/Debug/TraceableSerializer.php", "line": 47, "function": "serialize", "class": "Symfony\\Component\\Serializer\\Serializer", "type": "->" }, { "file": "vendor/api-platform/core/src/State/Processor/SerializeProcessor.php", "line": 65, "function": "serialize", "class": "Symfony\\Component\\Serializer\\Debug\\TraceableSerializer", "type": "->" }, { "file": "vendor/api-platform/core/src/State/Processor/WriteProcessor.php", "line": 42, "function": "process", "class": "ApiPlatform\\State\\Processor\\SerializeProcessor", "type": "->" }, { "file": "vendor/api-platform/core/src/Symfony/Controller/MainController.php", "line": 111, "function": "process", "class": "ApiPlatform\\State\\Processor\\WriteProcessor", "type": "->" }, { "file": "vendor/symfony/http-kernel/HttpKernel.php", "line": 181, "function": "__invoke", "class": "ApiPlatform\\Symfony\\Controller\\MainController", "type": "->" }, { "file": "vendor/symfony/http-kernel/HttpKernel.php", "line": 76, "function": "handleRaw", "class": "Symfony\\Component\\HttpKernel\\HttpKernel", "type": "->" }, { "file": "vendor/symfony/http-kernel/Kernel.php", "line": 197, "function": "handle", "class": "Symfony\\Component\\HttpKernel\\HttpKernel", "type": "->" }, { "file": "vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php", "line": 35, "function": "handle", "class": "Symfony\\Component\\HttpKernel\\Kernel", "type": "->" }, { "file": "vendor/autoload_runtime.php", "line": 29, "function": "run", "class": "Symfony\\Component\\Runtime\\Runner\\Symfony\\HttpKernelRunner", "type": "->" }, { "file": "public/index.php", "line": 5, "function": "require_once" } ], "hydra:title": "An error occurred", "hydra:description": "Unexpected non-iterable value for to-many relation." } 可能是因为 symfony 序列化器在规范化过程中将 getEmail 确定为 emails 属性的访问器,从而导致类型不匹配,因为 getEmail 返回单个对象,而 emails 是一个集合。 为了避免这些“晚期”错误,您可以通过向每个属性、方法参数和返回添加严格类型来改进代码。 它仍然会失败,但会给你更容易理解的错误消息。 <?php namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\Groups; #[ApiResource( normalizationContext: ["groups" => ["contact", "contact_read"]], denormalizationContext: ["groups" => ["contact", "contact_write"]] )] #[ORM\Entity] class Contact { // ... /** * @var Collection|Email[] */ #[ORM\OneToMany(targetEntity: Email::class, mappedBy: "contact")] #[Groups(["contact"])] private Collection $emails; // ... public function __construct() { $this->emails = new ArrayCollection(); } // ... /** * @return Collection|Email[] */ public function getEmails(): Collection { return $this->emails; } // ... #[Groups(["contact"])] public function getEmail(): ?Email { $criteria = Criteria::create()->where(Criteria::expr()->eq("is_primary", true)); $matches = $this->emails->matching($criteria); return ($matches->count()) ? $matches->first() : null; } }

回答 1 投票 0

使用结果缓存和不使用结果缓存对我的 php 函数进行计时

我一直在尝试测量我的一个php函数在使用结果缓存和不使用结果缓存时所花费的时间。 我遇到了麻烦,因为无论哪个先运行结果都会给出......

回答 2 投票 0

使用 Twig 时如何处理 Doctrine 可嵌入的空值?

我在 Symfony 应用程序中实现 Doctrine 嵌入时遇到问题 我在树枝模板中有这个: {% if m.getSecurity.getIsin %}{{ m.getSecurity.getIsin }}{% endif %} 我在 Symfony 应用程序中实现 Doctrine 嵌入时遇到问题 我在树枝模板中有这个: <td>{% if m.getSecurity.getIsin %}{{ m.getSecurity.getIsin }}{% endif %}</td> 当渲染模板时,我收到此错误: Typed property App\Entity\ISIN::$value must not be accessed before initialization 这是我的实体定义: <?php declare(strict_types=1); namespace App\Entity; use App\Exceptions\InvalidCUSIP; use App\Exceptions\InvalidISIN; use DateTimeInterface; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\Embedded; use Doctrine\ORM\Mapping\Embeddable; /** * Security. * * @ORM\Table(name="security") * @ORM\Entity(repositoryClass="App\Repository\SecurityRepository") */ class Security { /** * @ORM\Embedded(class = "ISIN", columnPrefix=false) */ private ?ISIN $isin = null; public function getIsin(): ?ISIN { return $this->isin; } /** * @return $this */ public function setIsin(?ISIN $isin): self { $this->isin = $isin; return $this; } } /** @Embeddable */ class ISIN { public const CHAR_COUNT = 12; /** * @ORM\Column (name = "isin", type="string", length=12, nullable=true, unique=true) */ private string $value; /** * @throws InvalidISIN */ public function __construct(string $value) { if (strlen($value) != self::CHAR_COUNT) { throw new InvalidISIN($value); } $this->value = $value; } public function __toString(): string { return $this->value; } } 我最近更改了一个更简单的实现,该实现将字段 isin 作为主要实体的一部分,并且一切正常。我在数据库中有一些记录,其中字段 isin 为空,这应该没问题...... 有什么想法吗? P.S.:相关问题(也是我提出的:如何使用 Twig 渲染自定义可嵌入学说字段?) 您试图在初始化之前访问 $value 变量。为了解决这个问题,您可以使其可为空,并使用以下声明将其初始化为空值: private ?string $value = null;

回答 1 投票 0

为同一实体定义多个存储库

同一个实体是否可以有两个存储库? 我尝试类似的方法,但它不起作用.. 类 PackageRepository 扩展 EntityRepository { 公共函数 __construct(...

回答 4 投票 0

如何使用 Twig 渲染自定义的可嵌入学说字段?

我有一个 Doctrine 实体,它有一个字符串字段,我正在尝试将其更改为可嵌入的以使用更好的抽象。 我创建了新类,更改了字段类型,然后更改了用于

回答 1 投票 0

架构中没有名为“doctrine_migration_versions”的表

我有一个使用 Doctrine 来管理其迁移的应用程序。这在开发过程中效果很好,但是当尝试在新数据库上部署应用程序时,迁移命令失败......

回答 1 投票 0

Symfony Fixtures 加载:尝试从命名空间“Doctrine\Common\Util”加载类“ClassUtils”

我正在使用 Symfony7 和 PHP 8.2。 我已经使用composer req orm-fixtures 命令通过composer 安装了固定装置。我也创建了我的固定装置。 当我尝试执行此命令时:symfony console

回答 1 投票 0

学说未定义常量“Doctrine\DBAL\Types\Types::ARRAY”

每当我尝试使用 php bin/console make:entity 命令创建实体时,在添加一些特定数据类型(例如整数)时会收到错误。 我收到的错误是 在 DoctrineHelper.php 中...

回答 2 投票 0

Symfony Doctrine 未定义常量数组

每当我尝试使用 php bin/console make:entity 命令创建实体时,在添加一些特定数据类型(例如整数)时会收到错误。 我收到的错误是 在 DoctrineHelper.php 中...

回答 1 投票 0

使用 php-di 在 slim 4 中集成学说

尝试使用 slim 4 和 php-di 的学说,但我无法使用 autowire 运行它。 按照我的设置: 索引.php $定义= [ “设置”=> [ '教义' => [ '

回答 1 投票 0

如何将预取的数据映射到PHP中的模型?

鉴于我已经通过外部服务检索了几行作为数组。在基于 Symfony2 和 Doctrine 构建的 PHP 应用程序中。 我如何继续将这些数据映射到模型,以便我可以...

回答 2 投票 0

PHPUnit 学说模拟问题

对于我的项目来说,理想的情况是我只需要在数据库中存储时间(学说 type="time_immutable")。然而它会导致 phpunit 出现问题,我找不到任何有效的解决方案。 当我尝试...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.