0

参照テーブルを介して親オブジェクトに関連付けられているすべてのレコードを取得し、モデルに直接挿入しようとしています。RoleEndpointsMany() を持つオブジェクト Role があります。RoleEndpoints は Role と hasMany() Endpoints に属します。すべてのデータは期待どおりに取得されていますが、設定すると消えてしまうようです。

<?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;
                }

コメントにあるように、結果セットの 2 回目の反復中に、何らかの理由で endpoints 属性のドロップが null になります。ここで何か間違ったことをしていますか?手順がありませんか?

どんな助けでも大歓迎です。ありがとうございました!

4

1 に答える 1

1

イテレータのトラバースに next() がありません。

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();    
    }
}
于 2013-02-05T21:12:24.930 に答える