2013-02-05 60 views
0

我試圖通過查找表獲取與父對象關聯的所有記錄,並將它們直接插入到模型中。我有一個對象,角色,hasMany()RoleEndpoints。 RoleEndpoints屬於角色和hasMany()端點。所有的數據都按照我的預期進行檢索,但是,在我設置它後,它似乎消失了。成功設置其他屬性後消失

<?php 
class ACL { 
    private $_di; 

    public function __construct($di) { 
     $this->_di = $di; 
    } 

    public function createACL() { 
     if(!$this->_acl) { 
      $this->_acl = new stdClass(); 

      $roles = $possibleRoles = Roles::find(); 

      /** 
      * Check if there is at least one role out there 
      */ 
      if($roles->count() > 0) { 
       /** 
       * Iterate over all of the records 
       */ 
       while($roles->valid()) { 
        $endpoints = array(); 

        /** 
        * Grab each role's endpoints through the relationship 
        */ 
        foreach($roles->current()->getRoleEndpoints() as $roleEndpoint) { 
         $endpoints[] = Endpoints::findFirst($roleEndpoint->endpoint_id); 
        } 

        /** 
        * At this point, the endpoints exist in the current Role model; 
              I tried several different approaches; this seemed the best 
        */ 
        $roles->current()->endpoints = $endpoints; 
       } 

       /** 
       * Set object to loop through from the beginning 
       */ 
       $roles->rewind(); 

       /** 
       * Here is where my issue lies. 
       * 
       * The endpoints attribute, which is set as a public attribute in the model class 
       * gets unset for some reason 
       */ 
       while($roles->valid()) { 
        echo '<pre>'; 
        var_dump($roles->current()); 
        exit; 
       } 

正如註釋所述,在結果集的第二次迭代期間,由於某種原因,端點屬性drop將變爲null。我在這裏做錯了什麼?我錯過了一步嗎?

任何幫助,將不勝感激。謝謝!

回答

1

存在丟失的下一個()的迭代器遍歷:

while ($roles->valid()) { 

    $endpoints = array(); 

    /** 
    * Grab each role's endpoints through the relationship 
    */ 
    foreach ($roles->current()->getRoleEndpoints() as $roleEndpoint) { 
     $endpoints[] = Endpoints::findFirst($roleEndpoint->endpoint_id); 
    } 

    /** 
    * At this point, the endpoints exist in the current Role model; 
    * I tried several different approaches; this seemed the best 
    */ 
    $roles->current()->endpoints = $endpoints; 

    //Missing next 
    $roles->next(); 
} 

而且,你不需要迭代光標那樣,只是一個foreach很容易閱讀和維護:

$roles = Roles::find(); 

$roleEndpoints = array(); 

if (count($roles)) { 
    foreach ($roles as $role) { 

     $endpoints = array(); 

     /** 
     * Grab each role's endpoints through the relationship 
     */ 
     foreach ($role->getRoleEndpoints() as $roleEndpoint) { 
      $endpoints[] = Endpoints::findFirst($roleEndpoint->endpoint_id); 
     } 

     /** 
     * At this point, the endpoints exist in the current Role model; 
     * I tried several different approaches; this seemed the best 
     */ 
     $roleEndpoints[$role->id] = $endpoints;  
    } 
} 

//Get the endpoints 
foreach ($roleEndpoints as $roleId => $endpoint) { 
    //... 
} 

而且,如果這是一個常見的任務,你可以添加一個方法到模型重用邏輯:

class Roles extends Phalcon\Mvc\Model 
{ 

    public function getEndpoints() 
    { 
     $endpoints = array(); 
     foreach ($this->getRoleEndpoints() as $roleEndpoint) { 
      $endpoints[] = Endpoints::findFirst($roleEndpoint->endpoint_id); 
     } 
     return $endpoints; 
    } 

    public function initialize() 
    { 
     //... 
    } 
} 

所以,你可以得到你的終點:

$roles = Roles::find(); 
if (count($roles)) { 
    foreach ($roles as $role) {      
     $endpoints = $role->getEndpoints();  
    } 
} 
+0

我結束了最後的解決方案,其中該邏輯是在模型中去。我要把這個報告給Phalcon的開發者,因爲我不明白爲什麼設置的屬性會消失。謝謝你的幫助! –

相關問題