参照テーブルを介して親オブジェクトに関連付けられているすべてのレコードを取得し、モデルに直接挿入しようとしています。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 になります。ここで何か間違ったことをしていますか?手順がありませんか?
どんな助けでも大歓迎です。ありがとうございました!