2014-01-29 130 views
0

我遇到問題。我創建了兩個關聯的實體。協會無法訪問方法

實體代碼:

/** @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()我收到錯誤消息:

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

我該如何解決?

感謝

回答

0

「$ employee_shop-> employeeShop()」 - 這是一個集合

使用「foreach($ employee_shop-> employeeShop()as $ obj)」
等等嘗試使用「$ obj-> getIdShop()」打印ID。