如何使用 Doctrine 和 PHP 属性创建表索引

发布于 2025-01-12 19:34:38 字数 1282 浏览 1 评论 0原文

这是带有 PHP 注释的代码,

/**
 * @ORM\Table(name="telegram_accounts", schema="users", indexes={
 *      @ORM\Index(name="subscriber_notification_idx", columns={"subscriber_notification"}, options={"where": "subscriber_notification = TRUE"}),
 *      @ORM\Index(name="rename_notification_idx", columns={"rename_notification"}, options={"where": "rename_notification = TRUE"}),
 * })
 * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Account

我尝试使用 PHP 属性,但我无法找出正确的语法:

#[ORM\Table(name="telegram_accounts", schema="users", indexes: [
      ORM\Index(name: "subscriber_notification_idx", columns:["subscriber_notification"], options=["where" => "subscriber_notification = TRUE"]),
      ORM\Index(name: "rename_notification_idx", columns: ["rename_notification"], options:["where" => "rename_notification = TRUE"])
])]
#[ORM\Entity(repositoryClass: "Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository"]
#[ORM\HasLifecycleCallbacks]
 */
class Account

但这不起作用。

如何将 ORM\Index 插入到 ORM\Table 属性的索引数组中?

Here is the code with PHP annotations

/**
 * @ORM\Table(name="telegram_accounts", schema="users", indexes={
 *      @ORM\Index(name="subscriber_notification_idx", columns={"subscriber_notification"}, options={"where": "subscriber_notification = TRUE"}),
 *      @ORM\Index(name="rename_notification_idx", columns={"rename_notification"}, options={"where": "rename_notification = TRUE"}),
 * })
 * @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Account

I tried to use PHP Attributes but I cant figure out the correct syntax:

#[ORM\Table(name="telegram_accounts", schema="users", indexes: [
      ORM\Index(name: "subscriber_notification_idx", columns:["subscriber_notification"], options=["where" => "subscriber_notification = TRUE"]),
      ORM\Index(name: "rename_notification_idx", columns: ["rename_notification"], options:["where" => "rename_notification = TRUE"])
])]
#[ORM\Entity(repositoryClass: "Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository"]
#[ORM\HasLifecycleCallbacks]
 */
class Account

But this does not work.

How do I insert ORM\Index into the indexes array of ORM\Table attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦 2025-01-19 19:34:39

好的,事实证明,使用属性,语法已移至 单独的属性

#[ORM\Table(name: "telegram_accounts", schema: "users")]
#[ORM\Index(name: "subscriber_notification_idx", columns: ["subscriber_notification"], options: ["where" => "subscriber_notification = TRUE"])]
#[ORM\Index(name: "rename_notification_idx", columns: ["rename_notification"], options: ["where" => "rename_notification = TRUE"])]
#[ORM\Entity(repositoryClass: "Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository"]
#[ORM\HasLifecycleCallbacks]

Ok, turns out that with attributes the syntax moved to separate attribute:

#[ORM\Table(name: "telegram_accounts", schema: "users")]
#[ORM\Index(name: "subscriber_notification_idx", columns: ["subscriber_notification"], options: ["where" => "subscriber_notification = TRUE"])]
#[ORM\Index(name: "rename_notification_idx", columns: ["rename_notification"], options: ["where" => "rename_notification = TRUE"])]
#[ORM\Entity(repositoryClass: "Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository"]
#[ORM\HasLifecycleCallbacks]
一抹微笑 2025-01-19 19:34:39

要从 @ORM\Things 转换为 #[ORM\Things],您可以使用 rector 将为您进行这些更改。

要使用它,您必须:

  1. 使用composer require --dev rector/rector在开发模式下安装rector依赖项
  2. 在项目的根文件夹中创建一个文件rector.php ,内容如下:
<?php

use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\Config\RectorConfig;

return function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/src',
    ]);
    $rectorConfig->sets([
        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES
    ]);
};
  1. 运行vendor/bin/rector process

可选步骤

  • 删除rector:composer删除rector/rector && rm rector.php

一些有用的链接:

To convert from @ORM\Things to #[ORM\Things], instead of doing it per entity manually as you do, you can use rector that will do those changes for you.

To use it, you have to :

  1. Install rector dependency in dev mode with composer require --dev rector/rector
  2. Create a file rector.php in the root folder of your project, with this content :
<?php

use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\Config\RectorConfig;

return function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/src',
    ]);
    $rectorConfig->sets([
        DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
        SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES
    ]);
};
  1. Run vendor/bin/rector process

Optional steps:

  • Remove rector: composer remove rector/rector && rm rector.php

Some useful links:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文