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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度