XF 2.0 Relation my entity to XF user entity

Snog

Well-known member
This is driving me nuts. I can't seem to pull the XF user entity directly into a query result.

My entity:
Code:
public static function getStructure(Structure $structure)
{
    $structure->table = 'xf_snog_addon_users';
    $structure->shortName = 'Snog\Addon:User';
    $structure->primaryKey = 'user_id';

    $structure->columns = [
        'user_id' => ['type' => self::UINT],
        'myfield' => ['type' => self::SERIALIZED_ARRAY, 'default' => []]
    ];

    $structure->getters = [];

    $structure->relations = [
        'User' => [
            'entity' => 'XF:User',
            'type' => self::TO_ONE,
            'conditions' => 'user_id',
            'primary' => true
        ],
    ];

    return $structure;
}

The PHP code for the finder:
Code:
$users = $this->finder('Snog\Addon:User')
        ->with('User')
        ->where('myfield', 'LIKE', $fieldSearch)
        ->fetch();

The print_r result from PHP:
Code:
XF\Mvc\Entity\ArrayCollection Object
(
    [entities:protected] => Array
        (
            [1] => Snog\Addon\Entity\User Object
                (
                    [_uniqueEntityId:XF\Mvc\Entity\Entity:private] => 4
                    [rootClass:protected] => Snog\Addon\Entity\User
                    [_newValues:protected] => Array
                        (
                        )

                    [_values:protected] => Array
                        (
                            [user_id] => 1
                            [myfield] => a:2:{i:0;s:1:"1";i:1;s:1:"2";}
                        )

                    [_relations:protected] => Array
                        (
                        )

                    [_previousValues:protected] => Array
                        (
                        )

.....
)

I would expect the relation to be in that print_r dump. Yes? No?

It is there with other similar relations I've done, but for some reason this one... nadda.

If I do a loop through and then request the User relation it does show up there if I request it with.
Code:
foreach ($users as $user)
{
      print_r($user->User);
}

But I'm thinking it's doing a database query each time (not checked).
 
Last edited:
OK, after a bit more research, it appears like the first user ID is not getting the relation. I thought it was an admin that wasn't having the relation applied, but I tried changing a user to an admin and it does apply...

Results for users after user ID 1:
Code:
[30] => Snog\Addon\Entity\User Object
                (
                    [_uniqueEntityId:XF\Mvc\Entity\Entity:private] => 13
                    [rootClass:protected] => Snog\Addon\Entity\User
                    [_newValues:protected] => Array
                        (
                        )

                    [_values:protected] => Array
                        (
                            [user_id] => 30
                            [myfield] => a:2:{i:0;s:1:"1";i:1;s:1:"2";}
                        )

                    [_relations:protected] => Array
                        (
                            [User] => XFRM\XF\Entity\User Object
                                (
                                    [_uniqueEntityId:XF\Mvc\Entity\Entity:private] => 12
                                    [rootClass:protected] => XF\Entity\User
                                    [_newValues:protected] => Array
                                        (
.....

The [User] => XFRM\XF\Entity\User Object changes depending on what other add-ons are enabled and I'm guessing this is normal and is just the name of the last entity extension/change applied.
 
And now finally after testing several different ways, I can say that the relation is not returned for the first user in the entity object array no matter what user ID it is. But the relation is returned for all other users.

I do believe this may be a bug in XenForo.
 
Yes, that could very well be.

But after re-thinking the way I was going to do something, I don't really need the full user info anyway and can just use my table's user_id and pass that to an XF function. It was a case of over thinking and re-inventing the wheel. ;)
 
Top Bottom