【问题标题】:Call to undefined method, ArrayCollection调用未定义的方法,ArrayCollection
【发布时间】:2014-01-24 17:49:28
【问题描述】:

我是 Symfony 的新手,我用它制作了一个 Web 应用程序,其中一个用户与一个实体“任务”相关的 ManyToMany。它不起作用,因为我收到错误

 FatalErrorException: Error: Call to undefined method 
 Acme\ManagementBundle\Entity\UserGroundStation::getMission()

getMission 在 MissionEntity 中定义,并在构造中调用。我必须重新定义它吗?我不明白。

我的用户是:

 <?php

 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\HasLifecycleCallbacks()
  * @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(){
         parent::__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;
     }

 }

任务实体是:

  /**
  * @ORM\Entity
  */
 class Mission {
     /** 
      * @ORM\Id
      * @ORM\GeneratedValue
      * @ORM\Column(type="integer")
      * @var integer
      */
     protected $id;
         /** 
      * @ORM\Column(type="string", length=60)
      * @var String
      */
     protected $name;
     /** 
      * @ORM\Column(type="string", length=600)
      * @var String
      */
     protected $description;
     /**
      * @ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\User", inversedBy="users")
      */
     protected $users;

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

     public function setUsers(Collection $users){
         $this -> users = $users;
         return $this;
     }

     public function addUser($user){
         $this -> users -> add($user);
         return $this;
     }

     public function getUsers(){
         return $this -> users;
     }
     //setters and getters for id, name and description
 }

如何解决这个问题?谢谢

【问题讨论】:

    标签: php symfony entity arraycollection


    【解决方案1】:

    你有错别字:

    getMission
    

    代替:

    getMissions
    

    或者您肯定没有在 Mission 实体中声明该方法,而且您从中调用该方法的类也不在列表中。确保它继承自 User。

    【讨论】:

    • 哦,谢谢,是错字,我以为是这样的。但我不明白你回答的第二部分; Mission实体中声明了方法“user”,我该怎么办?
    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 2019-10-25
    • 2018-07-11
    • 2013-10-23
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多