Running the code:
$member_groups = ee('Model')->get($member_groups)->limit(1)->all();returns an object with the first few lines:
Member Groups EllisLab\ExpressionEngine\Service\Model\Collection Object ( [association:EllisLab\ExpressionEngine\Service\Model\Collection:private] => [elements:protected] => Array ( [0] => EllisLab\ExpressionEngine\Model\Member\MemberGroup Object
My
print_r()of this object takes up 160,858 lines on my 27 inch monitor and 20MB of file space. A
var_dumpyields only 133 lines, but at the bottom gives me:
protected '_facade' =>
object(EllisLab\ExpressionEngine\Service\Model\Facade)[462]
...
protected '_validator' =>
object(EllisLab\ExpressionEngine\Service\Validation\Validator)[551]
...
protected '_associations' =>
array (size=12)
...
protected '_property_types' =>
array (size=101)
...
protected '_filters' =>
array (size=4)
...
protected '_clean_backups' =>
array (size=0)
...
protected '_mixin_manager' =>
object(EllisLab\ExpressionEngine\Library\Mixin\Manager)[443]
...so I can’t really tell what’s in there and/or why I should care.
Could we not have an option to just give us the contents of the table in which we are interested?
Ultimately - are there plans to document what the “facade” stuff is all about and why it needs to be in the object?
I don’t make these comments lightly, but the ability to segment a large object into manageable chunks (say, for debugging) is only doable when you already know something about its structure before you start.
Suggestions/rebuttals welcome.
Hi John,
It’s a lot of object references, so luckily the actual object isn’t very big at all.
Could we not have an option to just give us the contents of the table in which we are interested?
I think what you’re looking for here is $model->getValues() (alias: toArray(), which might be easier to remember), if you var_dump that you should see a fairly nicely sized array of fields.
Ultimately - are there plans to document what the “facade” stuff is all about and why it needs to be in the object?
These related objects are largely private and supporting. The facade in question is essentially equivalent to the object that you get when you call ee('Model'). It’s unfortunate that php dumps them even though they are not publicly accessible.
Hi Pascal - to get the member group table contents, I re-tried using ->asArray():
$member_groups = ee('Model')->get($member_groups)->filter('group_id', '!=', 1)->all()->asArray();That returns an array of objects, each with protected elements. That’s fine, except that each member group is returned with “protected ‘_facade’ =>” etcetera at the end.
I can deal with that by casting to an array and splitting off the parts I need, but it seems a naff/hacky (not to mention frowned-upon) way to get around the fact that the elements are protected, when all I needed to do in the first place was run a query on the member groups table. I know, run a query = disruptive.
Is there a native way to return whatever parts I want from each object - something like a foreach over an array? The documents mention:
Partial Data TODO Explain the fields() method.
Is the answer in there?
Thanks
Hi John,
toArray() should do what you’re looking for. asArray() on the collection just unwraps the collection, whereas toArray() is a method on the model that will actually convert the model to a more meaningful array of public fields. Calling a model method on a collection automatically applies it to all contained models. For example:
$member_groups = ee('Model')->get($member_groups)->filter('group_id', '!=', 1)->all();
var_dump($member_groups->toArray());
// is equivalent to
foreach ($member_groups as $group)
{
var_dump($group->toArray());
}Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.