0

我正在尝试通过查找表获取与父对象相关的所有记录,并将它们直接插入模型中。我有一个对象 Role,它有很多() RoleEndpoints。RoleEndpoints 属于 Role 和 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 出于某种原因变为空。我在这里做错了吗?我错过了一步吗?

任何帮助,将不胜感激。谢谢!

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 回答