XF 2.2 Multiple Entity Relations

Taylor J

Well-known member
I'm in need of some guidance when it comes to setting up multiple entity relations when extending a class (User in this instance).

This is my current User entity in my addon that I am extending
PHP:
<?php

namespace TaylorJ\Blogs\Entity;

use XF\MVC\Entity\Structure;

class User extends \XF\Entity\User
{

    public static function getStructure(\XF\Mvc\Entity\Structure $structure)
    {
        $structure = parent::getStructure($structure);

        $structure->relations = [
            'Blogs' => [
                'entity'    => 'TaylorJ\Blogs:Blogs',
                'type'      => self::TO_ONE,
                'conditions' => 'blog_id',
                'primary'   => true
            ],
            'Blog Posts' => [
                'entity'    => 'TaylorJ\Blogs:Posts',
                'type'      => self::TO_MANY,
                'conditions' => 'post_id',
                'primary'   => true
            ]
        ];

        return $structure;
    }
}
but currently I am getting this error back on every page (forum home, members, what's new, etc)

Code:
Template Compilation Error
public:forum_list - Unknown relation or alias Privacy accessed on xf_user in /Users/taylorjones/Development/apache/php7/xf/src/XF/Mvc/Entity/Finder.php:768
I should add that it also returns the same error if I remove the Blog Posts relation and only have the Blogs one there.

Is there something I'm just doing completely wrong here?
 
You are overwriting the existing relations, eg. they no longer exist.

You need to add your relations instead:
PHP:
$structure->relations['Blogs'] = [
    'entity'    => 'TaylorJ\Blogs:Blogs',
    'type'      => self::TO_ONE,
    'conditions' => 'blog_id',
    'primary'   => true
];
$structure->relations['BlogPosts'] = [
    'entity'    => 'TaylorJ\Blogs:Posts',
    'type'      => self::TO_MANY,
    'conditions' => 'post_id',
    'primary'   => true
];

But even if you do so the relations just don"t make sense and most certainly will not work :)
 
You are overwriting the existing relations, eg. they no longer exist.

You need to add your relations instead:
PHP:
$structure->relations['Blogs'] = [
    'entity'    => 'TaylorJ\Blogs:Blogs',
    'type'      => self::TO_ONE,
    'conditions' => 'blog_id',
    'primary'   => true
];
$structure->relations['BlogPosts'] = [
    'entity'    => 'TaylorJ\Blogs:Posts',
    'type'      => self::TO_MANY,
    'conditions' => 'post_id',
    'primary'   => true
];

But even if you do so the relations just don"t make sense and most certainly will not work :)
Ahhh that makes sense. I was thinking that was the way at first but couldn't find a good example of anything with multiples and just went based off of how the main User entity is setup.
Also yes the relations most likely don't make sense, I'm toying around with them as I figure out how to work with relations. I've only had experience with non relational database solutions (primarily mongo but a couple of others). Gotta figure out things somehow.

Thank you!
 
Top Bottom