程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 PHP 8 属性时如何覆盖子类中的 Doctrine 字段关联映射?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 php 8 属性时如何覆盖子类中的 Doctrine 字段关联映射??

开发过程中遇到使用 php 8 属性时如何覆盖子类中的 Doctrine 字段关联映射?的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 php 8 属性时如何覆盖子类中的 Doctrine 字段关联映射?的解决方法建议,希望对你解决使用 php 8 属性时如何覆盖子类中的 Doctrine 字段关联映射?有所启发或帮助;

如何在父类中定义 Doctrine 属性并覆盖扩展父类的类中的关联?使用注解时,这是通过使用 AssociationOverride 实现的,但是,我认为在使用 php 8 属性时它们不可用

为什么我想要:

我有一个类 AbstractTenantEntity,其目的是将数据访问权限限制为拥有数据的给定 Tenant(即帐户、所有者等),任何扩展此类的实体都将拥有tenant_ID 在创建时插入到数据库中,所有其他请求都会将 tenant_ID 添加到 WHERE 子句中。 Tenant 通常没有扩展 AbstractTenantEntity 的各种实体的集合,但有一些。使用注释时,我通过将 Doctrine 的 AssociationOverrIDe 注释应用于 Tenant 中应该有集合的扩展类来处理它,但是我不知道在使用 php 8 属性时如何实现这一点?>


我在下面描述的尝试没有成功,因为我错误地认为 Annotation class 如果适当修改会神奇地处理属性,但现在我看到 other code 必须能够根据属性应用适当的逻辑.因此,我放弃了这种方法,只是在具体类中保护和复制它们。

我的尝试:

租户实体

use Doctrine\ORM\MapPing\Entity;
use Doctrine\ORM\MapPing\column;
use Doctrine\ORM\MapPing\ID;
use Doctrine\ORM\MapPing\OnetoMany;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

#[Entity()]
class Tenant
{
    #[ID,column(type: "Integer")]
    #[GeneratedValue]
    private ?int $ID = null;

    #[OnetoMany(targetEntity: Asset::class,mappedBy: 'tenant')]
    private array|Collection|ArrayCollection $assets;

    // Other propertIEs and typical getters and setters
}

AbstractTenantEntity 实体

use Doctrine\ORM\MapPing\ManyToOne;
use Doctrine\ORM\MapPing\Joincolumn;

abstract class AbstractTenantEntity implements TenanTinterface
{
    /**
     * inversedBy performed in child where required
     */
    #[ManyToOne(targetEntity: Tenant::class)]
    #[Joincolumn(nullable: falsE)]
    protected ?Tenant $tenant = null;

    // Typical getters and setters
}

这是让我卡住的部分。使用注解时,我的代码如下:

use Doctrine\ORM\MapPing as ORM;

/**
 * @ORM\Entity()
 * @ORM\AssociationOverrIDes({
 *     @ORM\AssociationOverrIDe(name="tenant",inversedBy="assets")
 * })
 */
class Asset extends AbstractTenantEntity
{
    // VarIoUs propertIEs and typical getters and setters
}

但是 AssociationOverrIDes 尚未修改为使用属性,因此基于 official class,我创建了自己的类,类似于 Doctrine 已更新的其他类:

namespace App\MapPing;

use Attribute;
use Doctrine\Common\Annotations\Annotation\namedArgumentConstructor;
use Doctrine\ORM\MapPing\Annotation;

/**
 * This Annotation is used to overrIDe association mapPing of property for an entity relationship.
 *
 * @Annotation
 * @namedArgumentConstructor()
 * @Target("AnnOTATION")
 */
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEAtablE)]
final class AssociationOverrIDe implements Annotation
{
    /**
     * The name of the relationship property whose mapPing is being overrIDden.
     *
     * @var String
     */
    public $name;

    /**
     * The join column that is being mapped to the persistent attribute.
     *
     * @var array<\Doctrine\ORM\MapPing\Joincolumn>
     */
    public $joincolumns;

    /**
     * The join table that maps the relationship.
     *
     * @var \Doctrine\ORM\MapPing\Jointable
     */
    public $jointable;

    /**
     * The name of the association-fIEld on the inverse-sIDe.
     *
     * @var String
     */
    public $inversedBy;

    /**
     * The fetching strategy to use for the association.
     *
     * @var String
     * @Enum({"LAZY","EAGER","EXTRA_LAZY"})
     */
    public $fetch;

    public function __construct(
        ?String $name = null,?array  $joincolumns = null,?String $jointable = null,?String $inversedBy = null,?String $fetch = null
    ) {
        $this->name    = $name;
        $this->joincolumns = $joincolumns;
        $this->jointable = $jointable;
        $this->inversedBy = $inversedBy;
        $this->fetch = $fetch;
        //$this->deBUG('__construct',);
    }

    private function deBUG(String $message,String $file='test.Json',?int $options = null)
    {
        $content = file_exists($filE)?Json_decode(file_get_contents($filE),truE):[];
        $content[] = ['message'=>$message,'object_vars'=>get_object_vars($this),'deBUG_BACktrace'=>deBUG_BACktrace($options)];
        file_put_contents($file,Json_encode($content,JsON_PRETTY_PRint));
    }
}

在验证映射时,Doctrine 抱怨目标实体不包含所需的 inversedBy我花了一些时间浏览 Doctrine 源代码,但没有取得太大进展。

我目前的方法是否有价值,如果有,请填补空白。但是,如果没有,您建议如何满足这种需求?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的使用 PHP 8 属性时如何覆盖子类中的 Doctrine 字段关联映射?全部内容,希望文章能够帮你解决使用 PHP 8 属性时如何覆盖子类中的 Doctrine 字段关联映射?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。