Reputation: 196
I would like to query the database through the models I created. The only problem is that I have to serve JSON to a AngularJS application. I am trying to get records this way:
$rawmaterials = Rawmaterials::find();
foreach($rawmaterials as $rawmaterial) {
foreach($rawmaterial->RawMaterial_ingredients as $ingredient) {
$ingredients[] = $ingredient;
}
$data[] = array(
'name' => $rawmaterial->rawmaterial_name,
'ingredients' => $ingredients
);
}
return json_encode($data);
The response I get is as expected:
[
{
"name": "TestRawMaterial",
"ingredients": [
{
"ingredient_id": "1",
"ingredient_name": "curcumine gele kleurstof_2",
"ingredient_name_imis": "curcumine",
"enumber": "100",
"enumber_addition": null,
"general_comments": "geen neveneffecten in voedselgebruik",
"further_info": "gele kleurstof curcuma geel",
"alba_list": null,
"created_by": "Nienke",
"created_at": "2014-07-02 14:38:22"
}
]
}
]
However this is not ideal as I might want to return all fields that belong to this rawmaterial dynamically instead of just the rawmaterial_name hardcoded into a array. Then I tried this piece of code (yes I know it's kinda bad):
$rawmaterials = Rawmaterials::find();
foreach($rawmaterials as $rawmaterial) {
foreach($rawmaterial->RawMaterial_ingredients as $ingredient) {
$ingredients['ingredients'][] = $ingredient;
}
$data[] = array_merge((array)$rawmaterial, $ingredients);
}
return json_encode($data);
This time I can easily get all fields from the rawmaterial table but unfortunately it also gives me some stuff I don't want to see such as dependency injection:
[
{
rawmaterial_id: "1",
rawmaterial_name: "TestRawMaterial",
rawmaterial_spec_date: "2015-03-27 22:33:14",
rawmaterial_spec_source: "Koenders",
supplier_name: "Koenders",
supplier_description: "Trolol",
article_number: "45645456465",
supplier_article_number: "56465465465",
barcode: "99999999999999",
function: "Nothing",
organoleptic: null,
storage_temperature: null,
shelf_life: null,
chemical: null,
metal_check: null,
glass_check: null,
ion_radiation: null,
heat_treatment: null,
sizes: null,
foodgrade: null,
gmo: null,
versie: null,
temperature: null,
freezing_date: "0000-00-00 00:00:00",
web_link: null,
tester: "1",
origin_country: "NL",
production_country: "NL",
fish_catch_area: null,
created_at: "0000-00-00 00:00:00",
created_by: "0",
*_dependencyInjector: {
_services: {
router: {
},
dispatcher: {
},
url: {
},
modelsManager: {
},
modelsMetadata: {
},
response: {
},
cookies: {
},
request: {
},
filter: {
},
escaper: {
},
security: {
},
crypt: {
},
annotations: {
},
flash: {
},
flashSession: {
},
tag: {
},
session: {
},
sessionBag: {
},
eventsManager: {
},
transactionManager: {
},
assets: {
},
view: {
},
db: {
}
},
_sharedInstances: {
router: {
},
request: {
},
view: {
},
dispatcher: {
},
Api\LoginController: {
},
modelsManager: {
},
modelsMetadata: {
},
db: {
}
},
_freshInstance: false
},
*_modelsManager: {
},
*_modelsMetaData: {
},
*_errorMessages: null,
*_operationMade: 0,
*_dirtyState: 0,
*_transaction: null,
*_uniqueKey: null,
*_uniqueParams: null,
*_uniqueTypes: null,
*_skipped: null,
*_related: null,
*_snapshot: null,
rawmaterial_ingredients: {
},
ingredients: [
{
ingredient_id: "1",
ingredient_name: "curcumine gele kleurstof_2",
ingredient_name_imis: "curcumine",
enumber: "100",
enumber_addition: null,
general_comments: "geen neveneffecten in voedselgebruik",
further_info: "gele kleurstof curcuma geel",
alba_list: null,
created_by: "Nienke",
created_at: "2014-07-02 14:38:22"
}
]
}
]
Is there any way I can solve this problem by making some kind of function in the model which I can call to receive all fields without framework dependency injection stuff so I can put that into json_encode and be done with it?
Upvotes: 0
Views: 81
Reputation: 291
May be it isn't the most elegant but you could write a query using join and limit the columns you want. That way you are controlling what data is being passed. A bit cleaner is it is just one query instead of two as well.
Upvotes: 1