XF 2.0 Issue with new column/entity

GPA-R

Active member
Hi,

I created a new column in xf_user and created a code event listener on entity_structure. The new column is integer and defaults to 0. My Listener callback function is this:

PHP:
namespace MyApp\Main;

use XF\Mvc\Entity\Entity;

class Listener {
    public static function userShowCup(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure) {
        $structure->columns['user_show_cup'] = ['type' => Entity::INT, 'default' => 0];
    }
}

I made changes to the templates and I call $user.user_show_cup to show the result on the user's profile. It works.

Now the problem is that whenever I try to post/reply/login etc I get

PHP:
XF\Db\Exception: MySQL statement prepare error [1054]: Unknown column 'user_show_cup' in 'on clause' in src/XF/Db/AbstractStatement.php at line 212

I am not very familiar with XF2 so I don't really know what the correct way of setting entities is. (I've read the dev manual).

It is important to note that this column should not be updated from any screen. It is not meant to be updated by the front-end, only read.

Any ideas?
 
Last edited:
Have you run the queries to actually add the column to the DB? The entity system doesn't try to interact with the DB to do things like alters or sync the schema.
 
Thanks for your reply.

Yes the column is there, I get its data and show it in the templates which seems to be working fine.

I'll check my code to see if I missed anything.
 
The error you posted is coming from actually running the query -- it's MySQL not finding the column, as opposed to XF doing anything.

You should be able to see the full query in the error log in the control panel. You may need to confirm whether the query running is what you expect.
 
@Mike

Hm there's something wrong here and its probably the way I setup the entity.

The system tries to find my new column in every table.

I have an event_listener setup listening to "entity_structure".
Execute Callback: MyApp\Main\Listener
Method: userShowCup

This is my entity code:
PHP:
namespace MyApp\Main;

use XF\Mvc\Entity\Entity;

class Listener {
    public static function userShowCup(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure) {
        $structure->columns['user_show_cup'] = ['type' => Entity::INT, 'default' => 0];
    }
}

A sample of the MySQL error is this when a user tries to logon:
SQL:
INSERT INTO `xf_ip` (`user_id`, `ip`, `content_type`, `content_id`, `action`, `ip_id`, `log_date`, `user_show_cup`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)

I thought of specifying a table in userShowCup() but that resulted in more errors:

PHP:
class Listener {
    public static function userShowCup(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure) {
        $structure->table = 'xf_user';
        $structure->columns['user_show_cup'] = ['type' => Entity::INT, 'default' => 0];
    }
}

SQL:
SELECT `xf_user`.*
            FROM `xf_user`
            ORDER BY `xf_user`.`display_order` ASC
PHP:
XF\Db\Exception: MySQL statement prepare error [1054]: Unknown column 'xf_user.display_order' in 'order clause' src/XF/Db/AbstractStatement.php:212

Im pretty sure I did a stupid mistake somewhere.
 
Well, try to set an "Event hint" to XF\Entity\User in ACP. With this hint, you will be extending User entity only.

Without the hint, you will be adding a user_show_cup to every entity and this will produce errors as other entities, obvioulsy, do not have a column user_show_cup in database.
 
@CMTV ,

Great, now it works as it should, thanks!

By the way is there any way one can automatically create event listeners upon activating an add-on? In the setup file for example?
 
Not sure I understand the question.

All your event listeners created in ACP will be automatically included in addon's .zip archive when using xf-addon:build-release command and added to target user's ACP when installing your addon.
 
Not sure I understand the question.

All your event listeners created in ACP will be automatically included in addon's .zip archive when using xf-addon:build-release command and added to target user's ACP when installing your addon.
Awesome, that answers my question :)(y)
 
@AGPR the same goes for template modifications, custom permissions, phrases and so on. They all will be automatically included in your addon.
 
Yep. You can automate this too by adding $config['development']['defaultAddOn'] = 'YOUR_ADDON_ID'; in your confing.php file. I highly recommend to read this when you have time. :)
 
Top Bottom