XF 2.2 Extending user groups

RisteD

Active member
I want to extend the UserGroup class and to implement group permission and defining relationship on column user_group_id and entity XF:User, so here is my entity:
<?php
namespace usergrouplegend\Entity;
use \XF\Mvc\Entity\Structure;
class UserGroup extends XFCP_UserGroup {
public static function getStructure(Structure $structure){
$structure->table = 'xf_user_group';
$structure->getters = [
'PermissionSet' => [
'getter' => true,
'cache' => true
],
];
$structure->relations = [
'User' => [
'entity' => 'XF:User',
'type' => self::TO_MANY,
'conditions' => 'user_group_id',
'primary' => false
]
];

}
public function hasPermission($group,$permission){
return $this->PermissionSet->hasGlobalPermission($group,$permission);
}
}
Class extended in acp:
And in my controller i'm using:
$finder = \XF::finder('XF:UserGroup')->with('XF:User')->fetch()->toArray();
I've tried also like:
$finder = \XF::finder('usergrouplegend:Usergroup')->with('XF:User')->fetch()->toArray();
which i'm getting this error:
ErrorException: [E_WARNING] Creating default object from empty value in src\XF\Mvc\Entity\Manager.php at line 71
i wonder why this is caused. Tried also with extend \XF\Entity\UserGroup but the problem is the same.
 
Solution
You are overwriting the Structure.
Extend your structure with a CodeEvent Listener and extend the Structure Array.
Write a Listener.php with this code: (fit it to your needs)

PHP:
<?php
namespace usergrouplegend;

use XF\Mvc\Entity\Entity;
use XF\Container;

class Listener
{
    public static function userGroupEntityStructure(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure)
    {
        $structure->columns['your_column'] = ['type' => Entity::INT, 'default' => null, 'changeLog' => false];
        $structure->relations += [
            'YourRelation' => [
                'entity' => 'SomeEntity',
                'type' => Entity::TO_ONE,
                'conditions' => [ 'user_id' ],
            ]
        ];   
    }
}...

Hoffi

Well-known member
You are overwriting the Structure.
Extend your structure with a CodeEvent Listener and extend the Structure Array.
Write a Listener.php with this code: (fit it to your needs)

PHP:
<?php
namespace usergrouplegend;

use XF\Mvc\Entity\Entity;
use XF\Container;

class Listener
{
    public static function userGroupEntityStructure(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure)
    {
        $structure->columns['your_column'] = ['type' => Entity::INT, 'default' => null, 'changeLog' => false];
        $structure->relations += [
            'YourRelation' => [
                'entity' => 'SomeEntity',
                'type' => Entity::TO_ONE,
                'conditions' => [ 'user_id' ],
            ]
        ];   
    }
}

Add add a Listener:
1613133886262.png
 
Solution

RisteD

Active member
Still nothing
LogicException: Unknown relation or alias usergrouplegend:User accessed on xf_user in src\XF\Mvc\Entity\Finder.php at line 761
Controller:
$finder = \XF::finder('usergrouplegend:UserGroup')->with('usergrouplegend:User')->where('user_group_id','=',$groupid)->fetch()->toArray();
Entity User:
PHP:
<?php

namespace usergrouplegend\Entity;

use usergrouplegend\XF\Entity\XFCP_User;

use XF\Mvc\Entity\Structure;

class User extends XFCP_User {

    public static function getStructure(Structure $structure){

        $structure = parent::getStructure($structure);

        $structure->columns['user_group_id'] = ['type' => self::INT, 'default' => null, 'changeLog' => false];

        $structure->relations['UserGroup'] = [

                              'entity' => 'usergrouplegend:UserGroup',

                              'type' => self::TO_ONE,

                              'conditions' => 'user_group_id',

                              'primary' => true

            ];

        return $structure;

    }

}

Entity Usergroup;

<?php

namespace usergrouplegend\Entity;;



use usergrouplegend\XF\Entity\XFCP_UserGroup;

use \XF\Mvc\Entity\Structure;

class UserGroup extends XFCP_UserGroup{

    public static function getStructure(Structure $structure){

        $structure = parent::getStructure($structure);

        $structure->getters = [

            'PermissionSet' => [

                'getter' => true,

                'cache' => true,

                'setter' => true

            ],

        ];

        $structure->columns['user_group_id'] = ['type' => self::INT, 'default' => null, 'changeLog' => false];

        $structure->relations['User'] = [

                         'entity' => 'usergrouplegend:User',

                         'type'  => self::TO_MANY,

                         'conditions' => 'user_group_id',

                         'primary' => false

            ];

        return $structure;



}

    public function hasPermission($group,$permission){

        return $this->PermissionSet->hasGlobalPermission($group,$permission);

    }

}
Trying to set relationship between Usergroup (xf_user_group) and user (xf_user) table, on column user_group_id on both tables. And then i can list all members based on the given grup id.

Trying to implement that feature.
 
Last edited:
Top