XF 2.0 Update SQL column with Listener.php

SyTry

Well-known member
Hello,

I'm trying to make an add-on to add a member statistic to the notable members page :
Here is the code I got with the help of @nocte in my Listener.php :
PHP:
    public static function decreaseFollowersCount($entity)
    {
        $finder = \XF::finder('XF:User')
            ->where('user_id', $entity->follow_user_id);

        $user = $finder->fetchOne();

        if ($user)
        {
            $user->fastUpdate('sc_user_follow', $finder->columnSqlName('sc_user_follow') . "- 1");
        }
    }

    public static function increaseFollowersCount($entity)
    {
        $finder = \XF::finder('XF:User')
            ->where('user_id', $entity->follow_user_id);

        $user = $finder->fetchOne();

        if ($user)
        {
            $user->fastUpdate('sc_user_follow', $finder->columnSqlName('sc_user_follow') . "+ 1");
        }
    }
My code_event_listener :
  • entity_post_save : increaseFollowersCount
  • entity_post_delete : decreaseFollowersCount
  • Event hint : XF\Entity\UserFollow
The problem is that the function increaseFollowersCount is not working but decreaseFollowersCount is working fine.. Really weird ! :unsure:

If someone can help me or give me some directions, thank you ! ;)

Regards, SyTry
 

nocte

Well-known member
You can try a raw sql query:
PHP:
public static function increaseFollowersCount($entity)
{
    \XF::db()->query('UPDATE xf_user SET sc_user_follow = sc_user_follow + 1 WHERE user_id = ?', $entity->follow_user_id);
}
does this work?
 

nocte

Well-known member
o.k. then let's try this one, so we can narrow down where the bug is.

PHP:
    public static function increaseFollowersCount($entity)
    {
        $finder = \XF::finder('XF:User')
            ->where('user_id', $entity->follow_user_id);

        $user = $finder->fetchOne();

        if ($user)
        {
            \XF::db()->query('UPDATE xf_user SET sc_user_follow = sc_user_follow + 1 WHERE user_id = ?', $user->user_id);
        }
    }
But you say it's not recommended in most cases :unsure:
well, if it is not necessary, you should not use raw sql (because a little typo can cause a security risk). But in this case I assume there's no security issue :D
 

SyTry

Well-known member
o.k. then let's try this one, so we can narrow down where the bug is.

PHP:
    public static function increaseFollowersCount($entity)
    {
        $finder = \XF::finder('XF:User')
            ->where('user_id', $entity->follow_user_id);

        $user = $finder->fetchOne();

        if ($user)
        {
            \XF::db()->query('UPDATE xf_user SET sc_user_follow = sc_user_follow + 1 WHERE user_id = ?', $user->user_id);
        }
    }

well, if it is not necessary, you should not use raw sql (because a little typo can cause a security risk). But in this case I assume there's no security issue :D
Hello,

Sorry for my late response, it's working now ! :)
 
Top