具有略微相似实体的不同行为

时间:2014-01-22 18:42:08

标签: php symfony registration

我使用Symfony2创建了一个Web应用程序。有两种用户,UserOperatorUserGroundStation。它们都延伸User

用户地面站         

    namespace Acme\ManagementBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
    use Symfony\Component\Validator\Constraints as Assert;

    /**
     * @ORM\Entity
     * @ORM\HasLifecycleCallbacks()
     * @ORM\Table(name="user_GroundStation")
     * @UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User",         
    message="Username already_used")
     * @UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User", 
    message="Email already_used")
     */

    class UserGroundStation extends User
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
             /** 
         * @ORM\Column(type="string", length=60)
         * @var String
         */
        protected $name;
        /**
         * @var float
         *
         * @ORM\Column(name="latitude", type="float")
         */
        private $latitude;
        /**
         * @var float
         *
         * @ORM\Column(name="longitude", type="float")
         */
        private $longitude;

        /**
         * Set latitude
         *
         * @param string $latitude
         * @return UserGroundStation
         */
        public function setLatitude($latitude)
        {
            $this->latitude = $latitude;

            return $this;
        }

        /**
         * Get latitude
         *
         * @return string 
         */
        public function getLatitude()
        {
            return $this->latitude;
        }

        /**
         * Set longitude
         *
         * @param float $longitude
         * @return UserGroundStation
         */
        public function setLongitude($longitude)
        {
            $this->longitude = $longitude;   
            return $this;
        }
        /**
         * Get longitude
         *
         * @return float 
         */
        public function getLongitude()
        {
            return $this->longitude;
        }
        /**
         * @ORM\PrePersist
         */
        public function setCreatedAtValue()
        {
            $this->addRole('ROLE_GS');
        }       
    }

用户操作员

  namespace Acme\ManagementBundle\Entity;

  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
  use Symfony\Component\Validator\Constraints as Assert;

  /**
   * @ORM\Entity
   * @ORM\HasLifecycleCallbacks()
   * @ORM\Table(name="user_Operator")
   * @UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User", 
   message="Username already_used")
   * @UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User", 
   message="Email already_used")
   */

  class UserOperator extends User
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
       /** 
       * @ORM\Column(type="string", length=60)
       * @var String
       */
      protected $name;
       /**
       * @ORM\PrePersist
       */
      public function setCreatedAtValue()
      {
          $this->addRole('ROLE_OPERATOR');
      }    
  }

他们延长User

当我拨打UserOperator的注册页面时,它可以正常工作,当我拨打UserGroundStation的注册页面时,Symfony会提醒我:

  

属性“任务”和方法之一“getMission()”,“isMission()”,“hasMission()”,“__ get()”都不存在,并且在类“Acme \ ManagementBundle \ Entity”中具有公共访问权限\ UserGroundStation”。

怎么可能?我在哪里可以找到错误?

USER

  namespace Acme\ManagementBundle\Entity;

  use Doctrine\ORM\Mapping as ORM;
  use FOS\UserBundle\Model\User as BaseUser;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;


  /**
   * @ORM\Entity
   * @ORM\Table(name="user")
   * @ORM\InheritanceType("JOINED")
   * @ORM\DiscriminatorColumn(name="type", type="string")
   * @ORM\DiscriminatorMap({"user_one" = "UserGroundStation", "user_two" = "UserOperator"})
   * @ORM\MappedSuperclass()
   *
   */
  abstract class User extends BaseUser
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
          /**
       * @ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", mappedBy="users")
       */
      protected $missions;

      public function __construct(){
          $this -> missions = new ArrayCollection();
      }

      public function setMissions(Collection $missions){
          $this -> missions = $missions;
          return $this;
      }

      public function addMission($mission){
          $this -> missions -> add($mission);
          return $this;
      }

      public function getMissions(){
          return $this -> missions;
      }

1 个答案:

答案 0 :(得分:0)

这真是个问题。

他们有不同的FormType:

RegistrationUserOperatorFormType与RegistrationUserGroundStationFormType和 ProfileUserGroundStationFormType与ProfileUserOperatorFormType不同。