dongsheng66783619 2014-01-22 18:42
浏览 39
已采纳

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

I made a web application with Symfony2. There are 2 kind of user, a UserOperator and a UserGroundStation. Both of them extend User.

USER GROUND STATION

    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');
        }       
    }

USER OPERATOR

  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');
      }    
  }

They extend User.

When I call the registration page for UserOperator, it works, when I call registration page for UserGroundStation, Symfony alerts me:

Neither the property "mission" nor one of the methods "getMission()", "isMission()", "hasMission()", "__get()" exist and have public access in class "Acme\ManagementBundle\Entity\UserGroundStation".

How is it possible? Where can I find the error?

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条回答 默认 最新

  • duan5801 2014-01-23 13:38
    关注

    It was really a dummy problem.

    They have different FormType:

    RegistrationUserOperatorFormType is different to RegistrationUserGroundStationFormType and ProfileUserGroundStationFormType is different to ProfileUserOperatorFormType.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程