I am using Propel 2. I am hydrating objects through the relations, like so:
$return = OrderQuery::create()
->joinWith('Customer')
->joinWith('Status')
->find()
->toArray(TableMap::TYPE_PHPNAME, true, [], true);
The resulting Array would look something like this:
{
"Id": 1,
"CustomerId": 1,
"StatusId": 1,
"Initiated": "2016-01-01T01:01:01+00:00",
"Customer": {
"Id": 1,
"Forname": "Test",
"Surname": "Smith",
"Orders": [
"*RECURSION*"
]
}
"Status": {
"Id": 1,
"Title": "title 1",
"Priority": 1,
"Orders": [
"*RECURSION*"
]
},
}
I want to remove the fields where the value is *RECURSION*
. I tried using the $alreadyDumpedObjects
(3rd) parameter to toArray()
but that didn't seem to help. I could also do some form of array walking with unset()
calls, but I'm hoping there's a better way, maybe with a formatter or something?
For bonus points, I'd quite like to remove the columns which define the foreign key relationship. For instance, CustomerId
would go, but Customer
would remain.
The answer, it seems, appears to be very simple.
If I use a standard ArrayFormatter
like this:
$return = OrderQuery::create()
->setFormatter('Propel\Runtime\Formatter\ArrayFormatter');
->joinWith('Customer')
->joinWith('Status')
->find()
->toArray();
What is returned is an ArrayCollection
that, when toArray()
is called on, is exactly the same as my original output apart from the recursion fields are missing.
Note, when getting one result, for instance with ->findPk(1)
, it will return an associative array, so you shouldn't use ->toArray()
explicitly.