XF 2.2 Fetch all items from relation

emiya

Member
Hello!

First of all, I'll show you my current situation.

I have 2 tables which kinda looks like:

Transactions
payment_iduser_idusernamedate
11admintimestamp

Codes
code_idpayment_idcodeamount
118367453895410.00
210957409857410.00
318949836543910.00

Now I have created 2 entities for the above tables and for the "Transactions" entity I have created a relation to the "Codes" table.
My problem is if I want to get all items from "Transactions" I also want every code related to each transaction. Currently I only receive one item from "Codes".

This is my relationship from "Transactions"
PHP:
 $structure->relations = [
            'Codes' => [
                'entity' => 'foo\foo:Codes',
                'type' => self::TO_ONE,
                'conditions' => 'payment_id'
            ]
        ];
        $structure->defaultWith = ['Codes'];

and here is a snippet of the foreach loop
HTML:
 <xf:foreach loop="$codeList" value="$test" key="$key">
                        <tr>
                            <td>{$test.username}</td>
                            <td>{$test.title}</td>
                            <td></td> <!-- This should be all items from "[B]Codes[/B]" that belong to the Payment_id, but I only get one item
                            <td>{$test.Codes.amount}</td>
                            <td>{$test.created}</td>
                            <td>{$test.complete}</td>
                            <td></td>
                        </tr>
                    </xf:foreach>

The dump of $test.Codes
JSON:
Codes {#333 ▼
  #_getterCache: []
  #_valueCache: []
  #_structure: Structure {#336 ▶}
  #_em: Manager {#236 ▶}
  -_uniqueEntityId: 6
  #rootClass: "Foo\Foo\Entity\Codes"
  #_useReplaceInto: false
  #_newValues: []
  #_values: array:5 [▼
    "code_id" => 1
    "payment_id" => 3
    "code" => "83674538954"
    "amount" => "10.00"
  ]
  #_relations: []
  #_previousValues: []
  #_options: []
  #_deleted: false
  #_readOnly: false
  #_writePending: false
  #_writeRunning: false
  #_errors: []
  #_whenSaveable: []
  #_cascadeSave: []
  #_behaviors: null
}

I hope you understand my situation and are willing to help me.
 
I encountered a similar issue about a year ago. I think you're going to have to run a separate query to get those. You can't use a relation to grab multiple rows. Check the following thread for more info and a better explanation.

 
I could be misinterpreting your issue but can't you simply use a TO_MANY relationship?

PHP:
'type' => self::TO_MANY
 
That was my thought too, but unfortunately taht doesn't work as expected.
I don't know, the following should work if one is using TO_MANY relationship.

HTML:
<xf:foreach loop="$codeList" value="$test" key="$key">
    <tr>
        <td>{$test.username}</td>
        <td>{$test.title}</td>
        <td>
            <xf:foreach loop="$test.Codes" value="$singleCode" i="$i">
                ({$i}) {$singleCode.amount}<br />
            </xf:foreach>
        </td>        
        <td>{$test.created}</td>
        <td>{$test.complete}</td>
    </tr>
</xf:foreach>
 
Last edited:
Back
Top Bottom