XF 2.1 Entity Relations based on a content type?

Jaxel

Well-known member
Right now I have a relation in my entity based on a content_id:
Code:
    $structure->relations = [
        'Thread' => [
            'entity' => 'XF:Thread',
            'type' => self::TO_ONE,
            'conditions' => 'content_id',
            'primary' => true,
        ],
    ];
This of course, is assuming the content_type is thread.

Is it possible to put in additional relations, but base them on what the content_type value is? So only relate to threads if the content type is thread, etc?
 
PHP:
    $structure->relations = [
        'Thread' => [
            'entity' => 'XF:Thread',
            'type' => self::TO_ONE,
            'conditions' => [
                ['content_type', '=', 'thread'],
                ['content_id', '=', '$content_id']
            ],
            'primary' => true,
        ],
    ];
 
Hmm... the above code doesn't seem to work. It returns the error Unknown column 'xf_thread_Thread_1.content_type' in 'on clause'
 
Wait, I'm dumb, it would have to be something like
PHP:
    $structure->relations = [
        'Thread' => [
            'entity' => 'XF:Thread',
            'type' => self::TO_ONE,
            'conditions' => [
                ['thread_id', '=', '$content_id']
            ],
            'primary' => true,
        ],
    ];

You should probably set up a getter like getContent and have a switch on the content_type, accessing the correct relation based on that.
 
This is a limitation of XF's relationship finder condition structure. You can't have the LHS equaling a constant and the RHS being a column you are joining to.
 
Top Bottom