XF 2.2 Field name as the criteria value with the Finder

smozgur

Active member
This is how I would query threads by the solution post's user id in a controller action:

PHP:
$finder = $this->finder('XF:Thread');
$finder->where('Question.solution_user_id', 123);

However, I need to refer to the xf_thread.user_id field in the table instead of using a scalar or array (123 in the sample) to retrieve the threads that users marked their own posts as the solution. Following obviously doesn't work, but just to demonstrate what I am looking for:

PHP:
$finder->where('Question.solution_user_id', 'user_id');

Basically I would like to use a field name as the criteria. Using custom SQL query is perfectly fine, but I am just curious if this is possible with the Finder instead of using custom SQL?

Thanks.
 

smozgur

Active member
Following is a quick/lucky solution for my question, but it would be still great if there is a way as I originally thought. Because this is a lucky situation that user_id field exists only in one of the join tables.

PHP:
$finder = $this->finder('XF:ThreadQuestion');
$finder->with('Thread');
$finder->whereSql('solution_user_id = user_id');
 

smozgur

Active member
Have you tried the columnSqlName() method?

e.g. $finder->columnSqlName('Question.solution_user_id')
This one solves the duplicate field name issue. While it still requires using whereSql method, it definitely solves the problem.

I didn't know this method. Thank you so much, @nocte!
 
Top