Reputation: 225
I am having problem combining the results of two queries into one json object.
$item_results = ItemQuery::create('item')
->filterByCategory($categoryObjects, Criteria::IN)
->groupBy('item.ID')
->find();
returns an object like
{"ID":35,"Title":"Individual Asset","Description":"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.","DateRecorded":"01\/02\/01"}
Then I want to attach the categories back in so I run
foreach($item_results as $item_result) {
$categories = ItemCategoryQuery::create()
->filterByItem($item_result)
->find();
$item_result->categories = Array();
$item_result->categories = $categories->toArray();
echo json_encode($item_result->toArray());
}
but I get it back WITHOUT the categories still.... same json. So I ran
var_dump($item_result);
and got back
object(Item)#39 (18) {
["id":protected]=>
int(35)
["title":protected]=>
string(16) "Individual Asset"
["description":protected]=>
string(124) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
["date_recorded":protected]=>
string(10) "2001-01-02"
...
["categories"]=>
array(2) {
[0]=>
array(2) {
["ItemID"]=>
int(35)
["CategoryID"]=>
int(19)
}
[1]=>
array(2) {
["ItemID"]=>
int(35)
["CategoryID"]=>
int(15)
}
}
So the categories are in the new object it just doesn't serialize with it... Any ideas?
Upvotes: 1
Views: 1574
Reputation: 935
I know this is a very old question but has I found it by googling I think it might be worth the effort to write an answer.
Propel, as already suggested to you, exposes the toArray method on each model it generates, but that method will only handle the attributes or the collections (more on this next) defined in the schema definition.
What you're trying to achieve is the serialization of a model instance (an element from the ItemQuery::find method resulting array) with the related collection of categories into it.
Propel stores the results from a relation based query into a collection that is a protected attribute of the instance the query has been made. If the collection for a certain relationship has been loaded from the database will be included in the toArray method result.
In a Propel model the population/retrieval method for relationships are generated in the form get + related_model_name_pluralized
(if the relation this is an 1:N).
I think an example might be of help more than words.
foreach($item_results as $item_result) {
$item_result->getItemCategories(); //This is the method that will
//populate the collection.
//Now the collection is populated so we can json_encode
echo json_encode($item_result->toArray());
}
Upvotes: 0
Reputation: 365
toArray() is Propel-generated method, it turns all protected values of your object to array. It takes keys for array from your model - schema.
If you want to generate array with non-existing field, you can manually assign this value to array
$result = $item_result->toArray();
$result['categories'] = $categories->toArray()
or rewrite toArray method
Upvotes: 1