如何使用 Doctrine 和 PHP 属性创建表索引
这是带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,事实证明,使用属性,语法已移至 单独的属性:
Ok, turns out that with attributes the syntax moved to separate attribute:
要从
@ORM\Things
转换为#[ORM\Things]
,您可以使用 rector 将为您进行这些更改。要使用它,您必须:
composer require --dev rector/rector
在开发模式下安装rector依赖项rector.php
,内容如下:vendor/bin/rector process
可选步骤:
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 :
composer require --dev rector/rector
rector.php
in the root folder of your project, with this content :vendor/bin/rector process
Optional steps:
composer remove rector/rector && rm rector.php
Some useful links: