Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Get query results from table which listed multiple *primary* relations between another table
  • FuelPhp Version: 1.4

    Hello FuelPhp Community,
    i have a problem with the orm package. I have a table called `userdata` where i storage the data by the user but every unique dataset "Auto Increment" can contain another relations between the table `userdata` and `usermultipedata` by the autoincrement ID. So far for example:

    Userdata:

    Autoincrement(newsletter_userdata_id):     Key:      Value:
    1                         foo        bar

    Usermitpledata:   NO INDEX
    Autoincrement (newsletter_userdata_id)     Key        Value
    1                         bar        foo
    1                         lol        dod

    When i try to get the data by the model it does work but i get not all results. The results look like:

    array(1) {
    ["[]"]=>
    object(Model_Usernewsletterdatamultiple)#27 (10) {
    ["_is_new":protected]=>
    bool(false)
    ["_frozen":protected]=>
    bool(false)
    ["_data":protected]=>
    array(6) {
    ["newsletter_userdata_id"]=>
    string(2) "31"
    ["name"]=>
    string(9) "interests"
    ["value"]=>
    string(7) "Fitness"
    ["inputType"]=>
    string(13) "multicheckbox"
    ["created_at"]=>
    string(10) "1353936398"
    ["updated_at"]=>
    string(10) "1353936398"
    }
    ["_custom_data":protected]=>
    array(0) {
    }
    ["_original":protected]=>
    array(6) {
    ["newsletter_userdata_id"]=>
    string(2) "31"
    ["name"]=>
    string(9) "interests"
    ["value"]=>
    string(7) "Fitness"
    ["inputType"]=>
    string(13) "multicheckbox"
    ["created_at"]=>
    string(10) "1353936398"
    ["updated_at"]=>
    string(10) "1353936398"
    }
    ["_data_relations":protected]=>
    array(0) {
    }
    ["_original_relations":protected]=>
    array(0) {
    }
    ["_reset_relations":protected]=>
    array(0) {
    }
    ["_view":protected]=>
    NULL
    ["_iterable":protected]=>
    array(0) {
    }
    }
    }

    Look at the first line the index "[]" what happen? I want just fetching all datasets but the ORM makes one.

    Heres my code:

    1.Model : http://ideone.com/OvQygA
    2. Model : http://ideone.com/x8aHXc

    3. Code with the result above : http://ideone.com/C3Si0h

    Thank You!
  • First, an ORM is not a query builder. If you want to run queries, just use DB::select. Saves you from creating models, and the overhead of the ORM.

    Second, you need to understand how an ORM works. An ORM provides an object oriented approach to data, where each record in a table is represented by a Model object.

    As documented, every ORM table MUST have a primary key. It is used to create relations between tables, and it's also the key to the ORM's caching system. The fact that your second table has no primary key makes it unusable for ORM.

    If you still want to use the ORM, add a primary key, auto increment, to the second table. Then define the relation between the tables using the correct foreign keys, and define the relationship in the ORM model.

    Your query will then be reduced to

    $userdata = Model_Usernewsletterdata::find(Input::get('cid'), array('related' => array('multiple')));
    var_dump($userdata->multiple);
    assuming you have named the relation between the two models 'multiple'.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion