关联无法访问方法

时间:2014-01-29 15:19:05

标签: php doctrine-orm doctrine

我有问题。我创建了两个带关联的实体。

实体代码:

    /** @entity 
     *  @table(name="mt_employee")
     */
    class EmployeeEntity{
        /**
         * @id @column(type="integer")
         * @generatedValue
         */
        private $id_employee;
        /**
         * @column(length=100)
         */
        private $login;
        /**
         * @column(length=300)
         */
        private $password;
        /**
         * @column(length=50)
         */
        private $name;
        /**
         * @column(length=50)
         */
        private $surname;
        /**
         * @column(length=50)
         */
        private $email;
        /**
         * @column(type="smallint")
         */
        private $remote_login;
        /**
         * @column(length=100)
         */
        private $hash;
        /**
         * @oneToMany(targetEntity="EmployeeShopEntity", mappedBy="employee")
         * @joinColumn(name="id_employee", referencedColumnName="id_employee")
         */
        private $employeeShop;

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

        public function getLogin(){
            return $this->login;
        }

        public function getPassword(){
            return $this->password;
        }

        public function getName(){
            return $this->name;
        }

        public function getSurname(){
            return $this->surname;
        }

        public function getEmail(){
            return $this->email;
        }

        public function getRemoteLogin(){
            return $this->remote_login;
        }

        public function getHash(){
            return $this->hash;
        }

        public function setLogin($login){
            $this->login = $login;
        }

        public function setPassword($password){
            $this->password = $password;
        }

        public function setName($name){
             $this->name = $name;
        }

        public function setSurname($surname){
             $this->surname = $surname;
        }

        public function setEmail($email){
             $this->email = $email;
        }

        public function setRemoteLogin($remote_login){
             $this->remote_login = $remote_login;
        }

        public function setHash($hash){
             $this->hash = $hash;
        }

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

        public function getIdShop(){
            return $this->employeeShop->getIdShop();
        }
    }

    /** @entity
     *  @table(name="mt_employee_shop")
     */
    class EmployeeShopEntity{
        /**
         * @id @column(type="integer")
         * @generatedValue
         */
        private $id_employee_shop;
        /**
         * @column(type="integer")
         */
        private $id_employee;
        /**
         * @column(type="integer")
         */
        private $id_shop;
        /**
         * @manyToOne(targetEntity="EmployeeEntity", inversedBy="employeeShop")
         * @joinColumn(name="id_employee", referencedColumnName="id_employee")
         */
        private $employee;


        public function getIdShop(){
            return $this->id_shop;
        }
    }

获取数据:

    $query = Context::getContext()->entity_manager->createQuery('SELECT e FROM EmployeeEntity e JOIN e.employeeShop es WHERE e.login = ?1 AND e.password = ?2')->setParameter(1,$login)->setParameter(2,md5($password));
    $employee = $query->getResult(2);
    $employee = $employee[0];
    if($employee){
        foreach($employee AS $key=>$em){
            $this->{$key} = $em;
        }           
        $employee_shop = $query->getResult();
        $employee_shop = $employee_shop[0];
        print_r($employee_shop->employeeShop()->getIdShop());
    }

当我尝试访问$ employee_shop-> employeeShop() - > getIdShop()时收到错误消息:

  

致命错误:在第27行的/../class/employee.class.php中调用未定义的方法Doctrine \ ORM \ PersistentCollection :: getIdShop()

我该如何解决?

由于

1 个答案:

答案 0 :(得分:0)

“$ employee_shop-> employeeShop()” - 这是集合

使用“foreach($ employee_shop-> employeeShop()as $ obj)”
所以尝试用“$ obj-> getIdShop()”打印id。