XF 2.0 How to update my member statistics

right, my fault. Just replace $entity->user_id with $entity->follow_user_id.
Ah now it's working with this code :
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);
    }

And for unfollow I use this one :
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);
    }

But when you say this :
right, my fault. Just replace $entity->user_id with $entity->follow_user_id.
I figured why not try to do the same with the other code ! This one :
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");
        }
    }

But only the decreaseFollowersCount is working, not increaseFollowersCount. I'm not sure why :unsure:
Anyway, thank you very much for the help. You will be in the credits of the add-on ! :D
 
And for unfollow I use this one :
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);
}
To be secure you should exclude users who have sc_user_follow = 0:
PHP:
public static function decreaseFollowersCount($entity)
{
    \XF::db()->query('UPDATE xf_user SET sc_user_follow = sc_user_follow - 1 WHERE user_id = ? AND sc_user_follow > 0', $entity->follow_user_id);
}
or for the other approach:
PHP:
    public static function decreaseFollowersCount($entity)
    {
        $finder = \XF::finder('XF:User')
            ->where('user_id', $entity->follow_user_id);

        $user = $finder->fetchOne();

        if ($user)
        {
            \XF::db()->update('xf_user', ['sc_user_follow' => 'sc_user_follow - 1'], 'user_id = ? AND sc_user_follow > 0', $user->user_id);
        }
    }
But only the decreaseFollowersCount is working, not increaseFollowersCount. I'm not sure why :unsure:
yeah, strange..

What if you replace:
PHP:
$user->fastUpdate('sc_user_follow', $finder->columnSqlName('sc_user_follow') . "+ 1");
with:
PHP:
$user->fastUpdate('sc_user_follow', 'sc_user_follow + 1');
.. not sure if columnSqlName() is necessary/helpful in this case.. :D
 
To be secure you should exclude users who have sc_user_follow = 0:
PHP:
public static function decreaseFollowersCount($entity)
{
    \XF::db()->query('UPDATE xf_user SET sc_user_follow = sc_user_follow - 1 WHERE user_id = ? AND sc_user_follow > 0', $entity->follow_user_id);
}
or for the other approach:
PHP:
    public static function decreaseFollowersCount($entity)
    {
        $finder = \XF::finder('XF:User')
            ->where('user_id', $entity->follow_user_id);

        $user = $finder->fetchOne();

        if ($user)
        {
            \XF::db()->update('xf_user', ['sc_user_follow' => 'sc_user_follow - 1'], 'user_id = ? AND sc_user_follow > 0', $user->user_id);
        }
    }

yeah, strange..

What if you replace:
PHP:
$user->fastUpdate('sc_user_follow', $finder->columnSqlName('sc_user_follow') . "+ 1");
with:
PHP:
$user->fastUpdate('sc_user_follow', 'sc_user_follow + 1');
.. not sure if columnSqlName() is necessary/helpful in this case.. :D
I'll try that tomorrow, I'll go to sleep. Thanks again ! ;)
 
What if you replace:
PHP:
$user->fastUpdate('sc_user_follow', $finder->columnSqlName('sc_user_follow') . "+ 1");
with:
PHP:
$user->fastUpdate('sc_user_follow', 'sc_user_follow + 1');
.. not sure if columnSqlName() is necessary/helpful in this case.. :D
Hello, It doesn't change anything, really weird :unsure:
 
Top Bottom