XF 2.0 How to properly extend XF:Entity:Thread.php

AndyB

Well-known member
I would like to extend the src/XF/Entity/Thread/php structure relation.

My add-on works correctly if I hack the code by adding the following:

PHP:
'History' => [
    'entity' => 'Andy\History:History',
    'type' => self::TO_ONE,
    'conditions' => 'thread_id',
    'primary' => true
]

to this section:

PHP:
$structure->relations = [
    'Forum' => [
        'entity' => 'XF:Forum',
        'type' => self::TO_ONE,
        'conditions' => 'node_id',
        'primary' => true,
        'with' => 'Node'
    ],
    'User' => [
        'entity' => 'XF:User',
        'type' => self::TO_ONE,
        'conditions' => 'user_id',
        'primary' => true
    ],
    'FirstPost' => [
        'entity' => 'XF:post',
        'type' => self::TO_ONE,
        'conditions' => [['post_id', '=', '$first_post_id']],
        'primary' => true
    ],
    'LastPost' => [
        'entity' => 'XF:post',
        'type' => self::TO_ONE,
        'conditions' => [['post_id', '=', '$last_post_id']],
        'primary' => true
    ],
    'LastPoster' => [
        'entity' => 'XF:User',
        'type' => self::TO_ONE,
        'conditions' => [['user_id', '=', '$last_post_user_id']],
        'primary' => true
    ],
    'Prefix' => [
        'entity' => 'XF:ThreadPrefix',
        'type' => self::TO_ONE,
        'conditions' => 'prefix_id',
        'primary' => true
    ],
    'Read' => [
        'entity' => 'XF:ThreadRead',
        'type' => self::TO_MANY,
        'conditions' => 'thread_id',
        'key' => 'user_id'
    ],
    'Watch' => [
        'entity' => 'XF:ThreadWatch',
        'type' => self::TO_MANY,
        'conditions' => 'thread_id',
        'key' => 'user_id'
    ],
    'UserPosts' => [
        'entity' => 'XF:ThreadUserPost',
        'type' => self::TO_MANY,
        'conditions' => 'thread_id',
        'key' => 'user_id'
    ],
    'DeletionLog' => [
        'entity' => 'XF:DeletionLog',
        'type' => self::TO_ONE,
        'conditions' => [
            ['content_type', '=', 'thread'],
            ['content_id', '=', '$thread_id']
        ],
        'primary' => true
    ],
    'DraftReplies' => [
        'entity' => 'XF:Draft',
        'type' => self::TO_MANY,
        'conditions' => [
            ['draft_key', '=', 'thread-', '$thread_id']
        ],
        'key' => 'user_id'
    ],
    'ApprovalQueue' => [
        'entity' => 'XF:ApprovalQueue',
        'type' => self::TO_ONE,
        'conditions' => [
            ['content_type', '=', 'thread'],
            ['content_id', '=', '$thread_id']
        ],
        'primary' => true
    ],
    'Redirect' => [
        'entity' => 'XF:ThreadRedirect',
        'type' => self::TO_ONE,
        'conditions' => 'thread_id',
        'primary' => true
    ],
    'ReplyBans' => [
        'entity' => 'XF:ThreadReplyBan',
        'type' => self::TO_MANY,
        'conditions' => 'thread_id',
        'key' => 'user_id'
    ],
    'Poll' => [
        'entity' => 'XF:poll',
        'type' => self::TO_ONE,
        'conditions' => [
            ['content_type', '=', 'thread'],
            ['content_id', '=', '$thread_id']
        ]
    ],
    'CustomFields' => [
        'entity' => 'XF:ThreadFieldValue',
        'type' => self::TO_MANY,
        'conditions' => 'thread_id',
        'key' => 'field_id'
    ],
    'History' => [
        'entity' => 'Andy\History:History',
        'type' => self::TO_ONE,
        'conditions' => 'thread_id',
        'primary' => true
    ]
];

but of course I need to extend properly.

I tried to extend using the XFCP system like this:

PHP:
<?php

namespace Andy\History\XF\Entity;

use XF\Mvc\Entity\Structure;

class Thread extends XFCP_Thread
{
    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);
       
        $structure->relations = [
            'History' => [
                'entity' => 'Andy\History:History',
                'type' => self::TO_ONE,
                'conditions' => 'thread_id',
                'primary' => true
            ]
        ];
       
        return $structure;   
    }   
}

then defining the class extensions like this:

187602

but I get the following error when trying to view the forum_list:

187603
 
You're overwriting the entire relations array rather than adding your new entry.
 
It's fairly basic PHP principles to be honest. I'm sure you've probably done it before.

Have a Google for "adding items to an existing PHP array". Note, there are likely multiple ways to achieve the same thing.
 
Have a Google for "adding items to an existing PHP array".

Normally I would add to an object using setParams but that is giving me the following error:

An exception occurred: [Error] Call to undefined method XF\Mvc\Entity\Structure::setParams()
 
Hello,
You have to enter your relations like this:

PHP:
<?php

namespace Andy\History\XF\Entity;

use XF\Mvc\Entity\Structure;

class Thread extends XFCP_Thread
{
    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);
      
        $structure->relations['History'] = [
            'entity' => 'Andy\History:History',
            'type' => self::TO_ONE,
            'conditions' => 'thread_id',
            'primary' => true
        ]
      
        return $structure;   
    }   
}

Regards,
XenConcept
 
Back
Top Bottom