<p>A great place to start with this is over on <a href="https://pluginfactory.io/" rel="nofollow noreferrer">https://pluginfactory.io/</a>.</p>

<p>Select "Craft CMS Plugin Version 3.x" and check any plugin components you'd like to include, and it will build out the plugin scaffolding for you with ample comments and some examples.</p>

<p>For accessing database content, Craft heavily relies on the framework it uses, Yii 2.  The "Working with Databases" docs are a good place to start: <a href="https://www.yiiframework.com/doc/guide/2.0/en/start-databases#working-with-databases" rel="nofollow noreferrer">https://www.yiiframework.com/doc/guide/2.0/en/start-databases#working-with-databases</a></p>

<p>It will go over how to retrieve data using Active Record (Craft's <a href="https://github.com/craftcms/cms/tree/develop/src/records" rel="nofollow noreferrer">AR classes</a>) and Query Builder for direct database access.</p>

<p>You can do that data access from your plugin's Variable class, but a more common pattern is for the Variable class to have a wrapper method that calls a <a href="https://github.com/craftcms/docs/blob/v3/en/services.md" rel="nofollow noreferrer">Service class</a> method that returns the value to the Variable, then back to the template.</p>

<p>You can see Craft's Service classes here: <a href="https://github.com/craftcms/cms/tree/develop/src/services" rel="nofollow noreferrer">https://github.com/craftcms/cms/tree/develop/src/services</a></p>

<p>And in case it's not obvious yet, the Craft source code itself is a great place to look for examples of how to do things in your plugin.</p>

CC BY-SA 4.0
2018-03-19T18:03:19.607Z
2018-11-18T18:04:14.157Z
2018-11-18T18:04:14.157Z
Brad Bell
<p>Learning how to create plugins. I want to do a select query in an existing table in the Craft database, build navigation HTML and then return that HTML using a variable. I can see how to set up my own variable using the *Variable.php file. In the docs I don't see how querying database tables is done with any of the sections of the plugin found <a href="https://github.com/craftcms/docs#plugin-development" rel="nofollow noreferrer">here</a>.</p>

<p>I am guessing that I would use a class in the Craft classes?</p>

<p>If so then...</p>

<p>Do I put my logic for querying the table in my method to return the variable value, in my primary plugin class or somewhere else?</p>

<p>Which classes would I use?</p>

<plugin-development><craft3>
Example of Craft 3.0 plugin - querying an existing table
# Example of Craft 3.0 plugin - querying an existing table

(Asked by: username_0 on 2018-03-19)

Learning how to create plugins. I want to do a select query in an existing table in the Craft database, build navigation HTML and then return that HTML using a variable. I can see how to set up my own variable using the \*Variable.php file. In the docs I don't see how querying database tables is done with any of the sections of the plugin found [here](https://github.com/craftcms/docs#plugin-development).

I am guessing that I would use a class in the Craft classes?

If so then...

Do I put my logic for querying the table in my method to return the variable value, in my primary plugin class or somewhere else?

Which classes would I use?

**Tags:** plugin-development, craft3

---

## 1 answers

### Answer by username_1 on 2018-03-19. Score: 3

A great place to start with this is over on <https://pluginfactory.io/>.

Select "Craft CMS Plugin Version 3.x" and check any plugin components you'd like to include, and it will build out the plugin scaffolding for you with ample comments and some examples.

For accessing database content, Craft heavily relies on the framework it uses, Yii 2. The "Working with Databases" docs are a good place to start: <https://www.yiiframework.com/doc/guide/2.0/en/start-databases#working-with-databases>

It will go over how to retrieve data using Active Record (Craft's [AR classes](https://github.com/craftcms/cms/tree/develop/src/records)) and Query Builder for direct database access.

You can do that data access from your plugin's Variable class, but a more common pattern is for the Variable class to have a wrapper method that calls a [Service class](https://github.com/craftcms/docs/blob/v3/en/services.md) method that returns the value to the Variable, then back to the template.

You can see Craft's Service classes here: <https://github.com/craftcms/cms/tree/develop/src/services>

And in case it's not obvious yet, the Craft source code itself is a great place to look for examples of how to do things in your plugin.


---

