我正在尝试通过查找表获取与父对象相关的所有记录,并将它们直接插入模型中。我有一个对象 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 出于某种原因变为空。我在这里做错了吗?我错过了一步吗?
任何帮助,将不胜感激。谢谢!