XF 2.1 How to learn xenforo2?

Robert9

Well-known member
My next add-on needs all users from a usergroup.
To learn how to do that, i can start with the docs:

Here i could learn about:

$finder = \XF::finder('XF:User');
$users = $finder->where([
'user_state' => 'valid',
* and member of usergroup 21 *
])
->fetch();

but now i miss the important part. My next step is to feed ultraedit with $finder = \XF::finder('XF:User'); and then i search for the right lines to know more about how to fetch all users from a special usergroup. With the search query above, i will find nothing, so i will try shorter code. With ('XF:User i found 593 places, many stuff to go on.


Another way that helps sometimes is to look for an add-on that makes something equal. And for many cases this is a good start to find out. AndyB for example has a lot of short add-ons to understand some ways of XF2, but also he has a lot of old code with using normal PHP instead of XF-functions.


Maybe someone has other ways to find out more about coding in XF?

For my example from above i go now and search for the right add-on or try to watch the 593 places on my harddisc.
Both needs normally more or less a lot of time.
 
When you run this
PHP:
\XF::finder('XF:User')
XF will automagically read the first argument as XF\Finder\User and then create an instance of that class. A similar concept goes with the entity, controller, repository etc.

ultraedit
I highly recommend using PHPStorm for developing XF add-on. If you cannot afford the license, you could get the EAP version which is unstable (read: but still usable) from here.
 
Thank you. I know this. I use php Storm (WIN)+ LAMP/Xdebug (LINUX), but to search things inside a half harddisk there is nothing faster then UE.

Today i need to fetch all users of the usergroup x; i can do this with mysql.

but I want to know the way to do it with XF2 and i want to know how to learn XF (faster);

So, how you would find infos about that question: How to fetch all user of group 123?
 
It really depends on the context where you want to fetch the users in certain groups and what you want to do with those users.
 
What you mean concrete with "depends on context"?
Which contexts should be possible if i need

SELECT * FROM xf_user where secondary_user_id = {find 123 in varbinary commalist)
while fetching { do }


Is there any function in XF2 to do this?
 
Yes, i got the idea "by context".

I would like to know more about some examples, please.

Which wonderful situations you can imagine for

SELECT * from xf_user WHERE second_usergroup_ids = "123"?

For my there is an result or non result.
It could be one or more than one, i can fetch or fetchOne

and then we do something like

DELETE FROM xf_user where user_id= $result->userid (No, we dont, i am joking)
 
PHP:
$finder->where('user_state', 'valid');

Primary only
PHP:
$finder->where('user_group_id', 123);

Secondary only
PHP:
$finder->whereSql("FIND_IN_SET('123', " . $finder->columnSqlName('secondary_group_ids'))

Primary or Secondary
PHP:
$finder->whereSql($finder->columnSqlName('user_group_id') . " = 123 OR FIND_IN_SET('123', " . $finder->columnSqlName('secondary_group_ids'));
 
Yes, thank you. :)

I did

$userFinder = \XF::app()->finder('XF:User');
$userFinder
->where('secondary_group_ids', 'LIKE', '%11%')
->fetch();

$users = $userFinder->fetch();
foreach ($users AS $user)
{


How you fetch the user_id only?



Here i found an foreach before the query?
foreach ($this->finder('XF:UserGroup')->fetch() AS $userGroup)

Someone should write down all this examples. :)
 
I fetch all mebers of group 11;
Code:
        // get timestamp
        $tme = time() - (86400 * 90);

        // fetch all group-members
        $finder = \XF::finder('XF:User');
        $userFinder = $finder
        ->where('secondary_group_ids', 'LIKE', '%11%')
        ->ORDER('user_id','ASC')
        ;

        $users = $userFinder->fetch();
        foreach ($users AS $user)
        {

with my user_id

Code:
            $userId=$user->user_id;

            $finder2 = \XF::finder('XF:Post');
            $postFinder = $finder2
            ->where('user_id',$userId)
            ->where('post_date','>',$tme)
            ;

            $posts = $postFinder->fetch();

I count the posts from the last 90 days ($tme)
Code:
            $total = $posts->count();


finally we update a new field kposts in xf_user
Code:
            // update
            $user->fastUpdate('kposts',$total);


This code is valid and does what it should, but ...

How many queries happens here?

1. select from user
2. select from posts

Is this the correct way for select COUNT(*) from post?

I need to know only if there are more than {10} posts; so should is use count?



Maybe i should use another query like

select user_id from user where {in group}
with a JOIN of posts?


and finally not implemented here, i want to use XONs table for wordcount also to count only posts with {150} words.



To make it more complicated, just to know it:

If i need 30, 60 and 90 days; then i have to query at least two times (30 and 90 for example; then we calculate cnt60=cnt90-cnt30)



Say we have 100, 500, 5000, 50.000 users; how could be a strategy now?


All this is to know how many posts (with {x} words some special users {in group 123} has written in the last {y} days.
 
Top Bottom