简体   繁体   中英

Association can't get access to method

I have a problem. I created two entities with association.

Entity code:

    /** @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;
        }
    }

Getting data:

    $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());
    }

When I try access to $employee_shop->employeeShop()->getIdShop() I got error message:

Fatal error: Call to undefined method Doctrine\\ORM\\PersistentCollection::getIdShop() in /../class/employee.class.php on line 27

How can I fix it?

Thanks

"$employee_shop->employeeShop()" - This is a Collection .

Use "foreach($employee_shop->employeeShop() as $obj)"
and so try print the id with "$obj->getIdShop()".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM