特征 - 属性与父类冲突 [英] Traits - property conflict with parent class

查看:163
本文介绍了特征 - 属性与父类冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类 Zgh \FEBundle \ Entity \ User ,它扩展了 FOS \ UserBundle \ Model \ User

I have this class Zgh\FEBundle\Entity\User which extends FOS\UserBundle\Model\User.

use FOS\UserBundle\Model\User as BaseUser;

class User extends BaseUser implements ParticipantInterface
{
    use BasicInfo;
    // ..
}

BaseUser class:

abstract class User implements UserInterface, GroupableInterface
{
    protected $id;
    // ..
}

BaseInfo 特质:

trait BasicInfo
{
    /**
     * @ORM\Column(type="string", length=255)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    protected $id;

    // ..
}

但是当我跑步的时候我的代码我收到此错误:

But when I run my code i get this error:


严格标准:FOS \ UserBundle \ Model = \\用户和
Zgh \ FEBundle\Model\Partial\BasicInfo在
中定义了相同的属性($ id)Zgh \ FEBundle \ Entity \ User的组成。这可能是
不兼容,考虑在traits中使用accessor方法。

Strict standards: FOS\UserBundle\Model\User and Zgh\FEBundle\Model\Partial\BasicInfo define the same property ($id) in the composition of Zgh\FEBundle\Entity\User. This might be incompatible, consider using accessor methods in traits instead.

我正在使用Symfony框架。

I'm using Symfony framework.

无论如何要解决trait和父类对象之间关于这个属性的冲突吗?

Is there anyway to resolve this conflict between the trait and the parent class object about this property ?

推荐答案

不,现在还不能通过使用Trait重写映射属性。

No, it's not yet possible to rewrite a mapped property by using a Trait.

另外,一种可能的替代方法是使用多个抽象实体类,并根据您的需要扩展您的子实体。

Also, a possible alternative is to use multiple abstract entity classes, and extend your child entities depending on your need.

ie

<?php

use FOS\UserBundle\Model\User as BaseUser;

abstract class AbstractStrategyNoneEntity extends BaseUser
{
    /**
     * @ORM\Column(type="string", length=255)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    protected $id;
}

abstract class AbstractStrategyAutoEntity extends BaseUser
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     */
    protected $id;
}

并在您的子实体中扩展其中一个。

And extend one of them in your child entities.

/**
* @ORM\Entity
*/
class Child extends AbstractStrategyNoneEntity 
{
    // Inherited mapping
}

希望这能回答你的问题。

Hopes this answers your question.

这篇关于特征 - 属性与父类冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆