2015-04-07 85 views
0

我想通過我創建的模型查詢數據庫。唯一的問題是我必須將JSON提供給AngularJS應用程序。我想獲得的記錄是這樣的:PhalconPHP通過模型獲取信息

$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); 

我得到的是預期的響應:

[ 
    { 
     "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" 
      } 
     ] 
    } 
] 

然而,這並不理想,因爲我可能要回到屬於該原材料動態所有領域而不是僅將rawmaterial_name硬編碼到數組中。然後,我試過這段代碼(是的,我知道這是有點壞的):

$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); 

這一次,我可以輕鬆地獲得從原材料表中的所有字段,但不幸的是它也給了我一些東西,我不希望看到如依賴注入:

[ 
    { 
     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" 
      } 
     ] 
    } 
] 

有什麼辦法,我可以通過使某種模型中的功能,我可以打電話接收無框架依賴注入的東西各個領域的解決這個問題,所以我可以將它放入json_encode並完成它?

回答

1

也許它不是最優雅的,但你可以使用連接編寫查詢並限制你想要的列。這樣你就可以控制正在傳遞的數據。更清潔一點,它只是一個查詢而不是兩個。