XF 2.2 How do I get a variable into my controller?

grantus

Active member
I have two things I'm trying to understand with the Xenforo system.

Let's say I have two tables: MyTable1 and MyTable2. I want to query MyTable1 and use that column value in my query for MyTable2.

In PHP I would simply do a query on MyTable1, set a variable then use that variable in my query for MyTable2.

The second thing is I would use $_GET to get from a URL then use that value in my query.

How can I do these with the Entity/Controller?

I understand about relations in the Entity but I still don't see how I can either get a value from a link (url) or from one table to another.

I saw that I could use this:

Code:
$variable = $this->filter('my_column', 'uint');

But this is where I get confused on how to implement everything.
 
Solution
I'm assuming that's not the normal way that Xenforo passes an ID through?
Through a URL, you mean? Most IDs are passed as route parameters.

For example, the threads/ route format is :int<thread_id,title>/:page, which produces URLs like threads/title.1/.

In a template, you can build a link to this route using the link template function:
HTML:
<xf:comment>assuming $thread is a thread entity</xf:comment>
<a href="{{ link('threads', $thread) }}">{$thread.title}</a>

The parameters (thread_id and title) are automatically pulled from the properties of the thread object.

In the corresponding controller action you'll receive a \XF\Mvc\ParameterBag object, which you can use to access...
In PHP I would simply do a query on MyTable1, set a variable then use that variable in my query for MyTable2.
You could essentially do the same thing:
PHP:
// assuming $myTable1 is an instance of \Your\AddOn\Entity\MyTable1
$myTable2 = $this->finder('Your\AddOn:MyTable2')
    ->where('other_column', $myTable1->first_column)
    ->fetchOne();

Alternatively, you could define a relation on your MyTable1 entity structure:
PHP:
$structure->relations = [
    'MyTable2' => [
        'entity' => 'Your\AddOn:MyTable2',
        'type' => self::TO_ONE, // assuming a one-to-one relation
        'conditions' => [['second_column', '=', '$first_column']],
        'primary' => true, // assuming second_column is the primary key, otherwise omit this
    ],
];
PHP:
// assuming $myTable1 is an instance of \Your\AddOn\Entity\MyTable1
$myTable2 = $myTable1->MyTable2;


The second thing is I would use $_GET to get from a URL then use that value in my query.
You've basically got it. Use
PHP:
$value = $this->filter('param_name', 'type');

Then you can use $value in a finder to set a where clause.
 
Thank you, I see what you mean and it makes more sense now.

I'm assuming that's not the normal way that Xenforo passes an ID through?

For example, I'm looking to see how it's done in the thread template but I don't know how it's done to simply obtain the thread_id. I have my own template "battles" and I just want to get the "battle_id" to show the proper battle inside my template.

Normally in PHP I would just do mysite.com/battles.php?battle_id=x

So essentially they way I'm thinking of it is like this:

Battles controller -> finder results -> battles template - shows all the battles and links to battle_view template with the battle_id.

How would I then get the battle_id and pass it into my battle_view template? Obviously I'd have a separate finder for battle_view, but that's where I'm stuck.

Sorry if it's confusing, it's just I'm trying to integrate my old setup into Xenforo's.
 
I'm assuming that's not the normal way that Xenforo passes an ID through?
Through a URL, you mean? Most IDs are passed as route parameters.

For example, the threads/ route format is :int<thread_id,title>/:page, which produces URLs like threads/title.1/.

In a template, you can build a link to this route using the link template function:
HTML:
<xf:comment>assuming $thread is a thread entity</xf:comment>
<a href="{{ link('threads', $thread) }}">{$thread.title}</a>

The parameters (thread_id and title) are automatically pulled from the properties of the thread object.

In the corresponding controller action you'll receive a \XF\Mvc\ParameterBag object, which you can use to access the route params ($params->thread_id) and do whatever you want with them.

How would I then get the battle_id and pass it into my battle_view template? Obviously I'd have a separate finder for battle_view, but that's where I'm stuck.
You'd have a separate controller action for every page, ie. actionList and actionView. In your view action, you can use a shortcut for fetching the entity by primary key:

PHP:
// assuming you have your route set up roughly as outlined above
$battle = $this->em()->find('Your\AddOn:Battle', $params->battle_id)
 
Solution
Now I get it! So it coincides with the routing, that makes sense now.

So far I thought it was strictly actionIndex used to display for each template.

I fixed my routing and set it up as archives/battle/battle_id and it works! I'm starting to understand more how the params are used, it's starting to come together.

Thanks again for all the help.
 
Back
Top Bottom