<p>I tried eager loading before, and I couldn't figure out how to do it. </p>

<p><a href="https://docs.craftcms.com/v3/dev/eager-loading-elements.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/eager-loading-elements.html</a></p>

<p>Now I just query it using SQL. I know it's a bit hacking, but it gets the job done. The DB structure of <code>craft_content</code> table hasn't change since Craft 2 (when I started using craft), so its unlikely that future updates will break your code.</p>

<pre><code>$command = Craft::$app-&gt;getDB()-&gt;createCommand("SELECT `ct`.`id`, `cc`.`title` FROM `craft_tags` as `ct` left join `craft_content` as `cc` on `ct`.`id` = `cc`.`elementId`");
$tags = $command-&gt;queryAll();
</code></pre>

<p>This is only a quick fix, if anyone had a better idea, just ignore this answer.</p>

CC BY-SA 4.0
2019-11-12T05:12:17.963Z
2019-11-12T22:25:21.853Z
2019-11-12T22:25:21.853Z
Parry Huang
<p>I'm trying to get tags out of the underlying craft DB and serve it as a REST endpoint - however, I'm only interested in exposing the tag id and title, so my data should look like this: </p>

<pre><code>{id: 1, title: "hello"}, {id: 2, title: "test"}
</code></pre>

<p>I'm currently able to achieve this with the following query: </p>

<pre><code>$tags = \craft\elements\Tag::find()-&gt;all();
</code></pre>

<p>The problem with this is that this returns all the fields the tag has. I then have to filter out the two fields I want by doing: </p>

<pre><code>foreach($tags as $tag) {
   $out = [];
   $out['id'] = $tag['id'];
   $out['title'] = $tag['title'];
   return $out;
}
</code></pre>

<p>This is quite wasteful as I'm fetching a bunch of fields I don't need from the DB, then looping through all the tags and parsing out the two fields I need. </p>

<p>What I'd like to do instead is only fetch these two fields from craft in my query. </p>

<p>I found two possible ways of doing this - one of them uses the <code>select</code> method: </p>

<p>Something like </p>

<pre><code>$tags = \craft\elements\Tag::find()-&gt;select(['id', 'title']).all();
</code></pre>

<p>The problem with the above code is that the SQL query which is executed by the above doesn't actually have fields called <code>id</code> and <code>title</code> due to aliasing and joins being carried out on multiple tables - therefore I'd have to know exactly which fields I want and in which table in the SQL schema they sit...this doesn't scale well for more complex queries. </p>

<p>I also found <a href="https://www.yiiframework.com/doc/api/2.0/yii-base-arrayable#fields()-detail" rel="nofollow noreferrer">https://www.yiiframework.com/doc/api/2.0/yii-base-arrayable#fields()-detail</a> which seems like it could be useful but I wasn't able to get this to work at all and the documentation isn't too helpful. </p>

<craft3><entry><php><yii><element>
Select specific fields from query
# Select specific fields from query

(Asked by: username_0 on 2019-11-12)

I'm trying to get tags out of the underlying craft DB and serve it as a REST endpoint - however, I'm only interested in exposing the tag id and title, so my data should look like this: 
{id: 1, title: "hello"}, {id: 2, title: "test"}

I'm currently able to achieve this with the following query: 
$tags = \craft\elements\Tag::find()->all();

The problem with this is that this returns all the fields the tag has. I then have to filter out the two fields I want by doing: 
foreach($tags as $tag) {
   $out = [];
   $out['id'] = $tag['id'];
   $out['title'] = $tag['title'];
   return $out;
}

This is quite wasteful as I'm fetching a bunch of fields I don't need from the DB, then looping through all the tags and parsing out the two fields I need. 
What I'd like to do instead is only fetch these two fields from craft in my query. 
I found two possible ways of doing this - one of them uses the select method: 
Something like 
$tags = \craft\elements\Tag::find()->select(['id', 'title']).all();

The problem with the above code is that the SQL query which is executed by the above doesn't actually have fields called id and title due to aliasing and joins being carried out on multiple tables - therefore I'd have to know exactly which fields I want and in which table in the SQL schema they sit...this doesn't scale well for more complex queries. 
I also found https://www.yiiframework.com/doc/api/2.0/yii-base-arrayable#fields()-detail which seems like it could be useful but I wasn't able to get this to work at all and the documentation isn't too helpful.

**Tags:** craft3, entry, php, yii, element

---

## 1 answers

### Answer by username_1 on 2019-11-12. Score: 1

I tried eager loading before, and I couldn't figure out how to do it. 
https://docs.craftcms.com/v3/dev/eager-loading-elements.html
Now I just query it using SQL. I know it's a bit hacking, but it gets the job done. The DB structure of craft_content table hasn't change since Craft 2 (when I started using craft), so its unlikely that future updates will break your code.
$command = Craft::$app->getDB()->createCommand("SELECT `ct`.`id`, `cc`.`title` FROM `craft_tags` as `ct` left join `craft_content` as `cc` on `ct`.`id` = `cc`.`elementId`");
$tags = $command->queryAll();

This is only a quick fix, if anyone had a better idea, just ignore this answer.


---

