【问题标题】:Association can't get access to method关联无法访问方法
【发布时间】:2014-01-29 15:19:05
【问题描述】:

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

实体代码:

    /** @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()

我该如何解决?

谢谢

【问题讨论】:

    标签: php doctrine-orm doctrine


    【解决方案1】:

    "$employee_shop->employeeShop()" - 这是一个收藏

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      相关资源
      最近更新 更多