PhalconPHP通过模型获取信息

时间:2015-04-07 11:10:10

标签: json angularjs phalcon

我想通过我创建的模型查询数据库。唯一的问题是我必须为AngularJS应用程序提供JSON。我试图以这种方式获取记录:

$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表中获取所有字段,但不幸的是它也给了我一些我不想看到的东西,例如依赖注入:

[
    {
        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 个答案:

答案 0 :(得分:1)

可能它不是最优雅的,但您可以使用join编写查询并限制所需的列。这样您就可以控制传递的数据。有点清洁,它只是一个查询,而不是两个。