XF 2.2 struggling with table joins

briansol

Well-known member
i'm up to the point in my addon where i need to bring in a relational data set. simple A.tableid -> B.id relationship.

I built out the relationship in $structure->relations = [] and now it's asking me for a getter. I don't know what to put in there.

I made this to simply get past the dead page load:

Code:
$structure->getters = [
            'myfield' => true
        ];


public static function getMyfield()
    {
        return;
    }

And the page loads but i'm not actually getting my join, as expected. Looking at other code, i can't figure out how to actually run the join.

Any advice?


my relation:

Code:
$structure->relations = [
            'myfield' => [
                'entity' => 'MyNS\MyMod:MyClass',
                'type' => self::TO_ONE,
                'conditions' => 'id',
                'primary' => true
            ],

I also made the reverse relation on the other table.
 
Took me a day, but i figured it out. Documenting here for future reference to hopefully help others.

Building the getters and method is not the correct approach. Don't do that. That was a hack i thought i needed to make but I actually was having a template issue, not a code issue.

The issue is that the template needs to use the relation as a parent object.

so, if you join
Code:
->with('MYRELATION')

and your relations looks something like:

Code:
$structure->relations = [
            'MYRELATION' => [
                'entity' => 'My\Mod:MyJoiningClass',
                'type' => self::TO_ONE,
                'conditions' => [['id', '=', '$thistableid']],
                'primary' => false
            ],

and populate the template like

Code:
$viewParams = [
            'mydata' => $finder->fetch()
        ];

iterate like this:

Code:
<xf:if is="$mydata is not empty">
    <xf:foreach loop="$mydata" value="$d">      
        <xf:macro name="myIndex_block"
            arg-id="{$d.id}"
            arg-field="{$d.field}"
            arg-joinedfield="{$d.MYRELATION.field}"

and finally have the row template populate with those arg="!" statements as usual.

Code:
<xf:macro name="myIndex_block" arg-id="!" arg-field="!" arg-joinedfield="!">
...  html here ...
</xf:macro>
 
Last edited:
Top Bottom