XF 2.2 Limit fields in returned entity

Orit

Active member
Hello
I'm working on an API endpoint which returns posts from a specific thread, with an option to limit results and order them with orderByDate('...').

My problem is with the result returned:
As I'm returning a XF entity, the results contain many unwanted fields (mainly user data and node data that should not be shown to the world)

I saw this thread: https://xenforo.com/community/threa...f-columns-from-an-entity-using-finder.160806/

and understand this is not possible.
You can do $finder->fetchOnly(['column1', 'column2']), but this will not give you an entity. As far as I know, entities require the entire set of data to work properly. The performance of SELECT cols vs SELECT * may be better relatively, but it's unlikely to have any real (discernible) performance impact.
finder->fetchRaw(['fetchOnly' => ['user_id', 'username']]) returns it as an array, but for the toApiResults() function I need the results to be an entity.

Is there a way to limit the fields returned via API?
I only managed to add extra fields, not remove unwanted ones.

Or maybe a different approach?

Thanks!
 
Assuming you have a \XF\Api\Result\EntityResultInterface object:

PHP:
$result->skipColumn('some_column');
$result->skipRelation('SomeRelation');

You can also construct any response manually:

PHP:
return $this->apiResult([
    'some_field' => $entity->some_field,
]);
 
Back
Top Bottom