XF 2.1 How to extend User entity by adding a column

AndyB

Well-known member
I'm trying to make this work using the finder:

PHP:
$finder = \XF::finder('XF:User');
$members = $finder
    ->where('last_activity', '>', $dateline)
    ->where('user_state', 'valid')
    ->where('is_banned', 0)
    ->where('andy_weekly_digest_opt_out', 0)
    ->where('Option.receive_admin_email', 1)
    ->where('Option.is_discouraged', 0)
    ->fetch();

In the setup.php file the andy_weekly_digest_opt_out is added. Now if I use raw MySQL query it works fine, but when I try to use the finder it fails:

1566496698385.webp

I tried extending the User entity like this:

PHP:
<?php

namespace Andy\WeeklyDigest\XF\Entity;

use XF\Mvc\Entity\Entity;
use XF\Mvc\Entity\Structure;

class User extends XFCP_User
{
    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);
      
		$structure->columns = [
			'andy_weekly_digest_opt_out' => ['type' => self::BOOL, 'default' => false]
        ];
      
        return $structure;   
    }   
}

But it's not adding the column as I would expect.

Thank you for your help.
 

Extend it the same way as for thread entity in the developer docs. NOTE: You just need what's in the function in the docs in your function, you still need the parent call, etc in your function. You don't need the full function public static function threadEntityStructure(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure)
 
But it's not adding the column as I would expect.
I mean, that code, as you've written it, probably is adding the column. In fact, that code, will replace ALL columns in the User entity with your new column.

You certainly don't want to replace the entire columns entry with a new array. You want to add a new entry to the existing array.
 
Thank you, Snog and Chris.

The problem was I had failed to create the code event listener.
 
Just change your code to:

Code:
$structure->columns['andy_weekly_digest_opt_out'] = ['type' => self::BOOL, 'default' => false];

and it should work.
 
Top Bottom