# Get data from table using snippet

**URL:** <https://community.modx.com/t/get-data-from-table-using-snippet/7458>\
**Category:** Development\
**Created:** [January 28, 2024, 12:52pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458 "2024-01-28T12:52:03Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![pakosfakos](https://community.modx.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pakosfakos](https://community.modx.com/u/pakosfakos)\
**Post date:** [January 28, 2024, 12:52pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/1 "2024-01-28T12:52:03Z")

</div>

Hello, i am trying to create a simple snippet who will return specific data from database table using xpdo. Following the official modx tutorial: ([Using Custom Database Tables - Tutorials | MODX Documentation](https://docs.modx.com/current/en/extending-modx/tutorials/using-custom-database-tables)) i got stacked in one basic step.  
In this tutorial is describing that we need to follow the steps:

1. creating a schema xml for the table we will need to query
2. Building the Schema
3. Using our New Model (creating and using the snippet.)

I got stacked into the second step: Building the Schema. My question is how to build it? it is a php coded file but what tool i need to use in order to run it under modx?  
I tried to run using console tool that i installed in modx but it return some errors regarding the directory structure etc… Of course icorected the directories to be under ~/core/components/console/ and i succeed to build and use my snippet but i believe that this is not the correct way.  
Also i started by creating the directory of my packageName inside core/components/ is this the correct way?

---

<div class="post-metadata">

**Author:** ![bruno17](https://community.modx.com/user_avatar/community.modx.com/bruno17/32/71_2.png) [@bruno17](https://community.modx.com/u/bruno17)\
**Post date:** [January 28, 2024, 3:04pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/2 "2024-01-28T15:04:26Z")

</div>

Which MODX version? Steps are a bit different between 2.x and 3.x

No matter, which version. MIGX should help to build and parse your schema, create your tables and get your data from your tables with migxLoopCollection.

---

<div class="post-metadata">

**Author:** ![pakosfakos](https://community.modx.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pakosfakos](https://community.modx.com/u/pakosfakos)\
**Post date:** [January 28, 2024, 7:12pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/3 "2024-01-28T19:12:42Z")

</div>

modx version 2.8.4, i wanted to learn the native way not the migx.  
Lets say that the tables already exist and i just want to create and build my own schema and then my own snippet.

---

<div class="post-metadata">

**Author:** ![bruno17](https://community.modx.com/user_avatar/community.modx.com/bruno17/32/71_2.png) [@bruno17](https://community.modx.com/u/bruno17)\
**Post date:** [January 28, 2024, 8:00pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/4 "2024-01-28T20:00:07Z")

</div>

did you allready write your schema?

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 28, 2024, 8:00pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/5 "2024-01-28T20:00:49Z")

</div>

> [@pakosfakos](#):
>
> i wanted to learn the native way not the migx.

It looks like the documentation you linked above, assumes that you have the standard folder structure for a MODX extra. (For the standard folder structure take a look at [this article](https://docs.modx.com/current/en/extending-modx/tutorials/developing-an-extra) or look at any Github repository of a MODX extra.)

You usually build these extras locally and outside of the folder with the MODX installation.  
To create the schema, you can just call `_build/build.schema.php` in your browser.

You would then create a transport package for the extra and install that on your server.

* * *

Usually to create a quick custom table directly on the server, it’s much easier to use an extra like _MIGX_ or _ExtraBuilder_.

The code you linked requires a lot of setup to basically just run `$generator->parseSchema()`. Honestly I don’t think there is much to learn by doing it all manually.

> [@pakosfakos](#):
>
> Lets say that the tables already exist

The _xPDOGenerator_ has a function `writeSchema` that you can use to generate a schema from existing database tables.

---

<div class="post-metadata">

**Author:** ![pakosfakos](https://community.modx.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pakosfakos](https://community.modx.com/u/pakosfakos)\
**Post date:** [January 29, 2024, 6:30am UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/6 "2024-01-29T06:30:52Z")

</div>

yes i wrote the schema so i am not sure about the next steps. As far as i understood i should create in core/components/myAppNAme and then inside that the model directory and schema directory. Then to put my schema xml and somehow to build it.

---

<div class="post-metadata">

**Author:** ![pakosfakos](https://community.modx.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pakosfakos](https://community.modx.com/u/pakosfakos)\
**Post date:** [January 29, 2024, 6:34am UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/7 "2024-01-29T06:34:49Z")

</div>

Thank you for your advices, so as far as i understood the best tactic is to create a directory \_build out of modx folder and then to create manually the deafaulte folder structure, to put the schema xml and build config, to build it and then copy paste the directories and files inside modx. in core/components.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 29, 2024, 8:34am UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/8 "2024-01-29T08:34:09Z")

</div>

> [@pakosfakos](#):
>
> the best tactic is to create a directory \_build out of modx folder and then to create manually the deafaulte folder structure

This is only the best tactic, if you want to create a **MODX extra**. (The advantage of a separate folder outside of MODX is, that you can easily manage this folder with Git.)

* * *

If this custom table is just a one-off thing, then the proceeding you described is way too complicated.

- Use **MIGX** and you can parse the schema and create the DB-tables with 2 clicks.

- If you really want to progam it yourself for some reason, then maybe create a **snippet** (with hard-coded folder paths) that you run once. Use the function [parseSchema](https://docs.modx.com/current/en/extending-modx/xpdo/class-reference/xpdogenerator/xpdogenerator.parseschema) to parse the schema and [createObjectContainer](https://docs.modx.com/current/en/extending-modx/xpdo/class-reference/xpdomanager/xpdomanager.createobjectcontainer) to create the database tables.  
(The example code in the documentation loads MODX externally, which you _don’t_ have to do in a snippet.)

* * *

> [@pakosfakos](#):
>
> As far as i understood i should create in core/components/myAppNAme and then inside that the model directory and schema directory.

It’s just a convention that the schema and the class files are located in such a folder structure. But you don’t have to follow this convention. Just make sure that when you use `$modx->addPackage()` in your code, you provide the correct path.

---

<div class="post-metadata">

**Author:** ![pakosfakos](https://community.modx.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pakosfakos](https://community.modx.com/u/pakosfakos)\
**Post date:** [January 29, 2024, 8:58am UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/9 "2024-01-29T08:58:21Z")

</div>

Thank you for your advice. Actually what i want to do is to create a snippet that i will use inside my html like that: [[!MySnippet? &someOptions]] and receive data from allready existing table ex. ms\_products table. I do not need to create new table.

---

<div class="post-metadata">

**Author:** ![bruno17](https://community.modx.com/user_avatar/community.modx.com/bruno17/32/71_2.png) [@bruno17](https://community.modx.com/u/bruno17)\
**Post date:** [January 29, 2024, 9:05am UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/10 "2024-01-29T09:05:16Z")

</div>

can you share your xml schema here?  
The next step, that you need is to parse that schema.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 29, 2024, 6:48pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/11 "2024-01-29T18:48:11Z")

</div>

> [@pakosfakos](#):
>
> Actually what i want to do is to create a snippet that i will use inside my html like that: [[!MySnippet? &someOptions]] and receive data from allready existing table ex. ms\_products table.

If the table already exists, you don’t necessarily need a schema to access the data.  
(You only need a schema if you want to have a PHP class, that corresponds to your database table.)

You can always use normal **PDO** to query a table. For example like this:

```auto
<?php
$stmt = $modx->prepare("SELECT * FROM `ms_products`");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$output = '';
foreach ($rows as $row) {
    $output .= '<pre>' . print_r($row, 1) . '</pre>';
    // $output .= $modx->getChunk('myRowChunk', $row); // To output a chunk for every row
}
return $output;

```

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [January 29, 2024, 8:03pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/12 "2024-01-29T20:03:34Z")

</div>

Here is some example code for parsing a schema in **MODX 2.x** :

* * *

Let’s assume we choose a packagename “somepackage”.

1. **Create a schema** under `core/components/somepackage/model/schema/somepackage.mysql.schema.xml` with the content:

```auto
<?xml version="1.0" encoding="UTF-8"?>
<model package="somepackage" baseClass="xPDOObject" platform="mysql" defaultEngine="InnoDB" version="1.1">
    <object class="somepackageTest" table="somepackage_test" extends="xPDOSimpleObject">
        <field key="some_field" dbtype="varchar" phptype="string" precision="100" null="false" default="" />
    </object>
</model>

```

1. To **parse the schema** , create a snippet with code like this:

```auto
<?php
$package_name = "somepackage";
$core_path = $modx->getOption('core_path', null, MODX_CORE_PATH ); // Path to 'core' folder
$model_folder = $core_path . 'components/' . $package_name . '/model/'; // Path where the generated class files should be saved to.
$schema_path = $core_path . 'components/' . $package_name . '/model/schema/' . $package_name . '.mysql.schema.xml'; // Path to your schema file

$manager = $modx->getManager();
$generator = $manager->getGenerator();
$success = $generator->parseSchema($schema_path, $model_folder);
if ($success){
    return 'schema successfully parsed.';
} else {
    return 'error parsing the schema.';
}

```

Run the snippet once. This should create the class files in `core/components/somepackage/model/somepackage/`.

1. To **create the database table** from the class files, create a snippet with code like this:

```auto
<?php
$package_name = "somepackage";
$core_path = $modx->getOption('core_path', null, MODX_CORE_PATH );
$modx->addPackage($package_name, $core_path . 'components/' . $package_name . '/model/');

$manager = $modx->getManager();
$success = $manager->createObjectContainer('somepackageTest');
if ($success){
    return 'table successfully created.';
} else {
    return 'error creating the table.';
}

```

Run the snippet once. This should create the database table `modx_somepackage_test`.

1. To **use your class** , create snippets with code like this:

To create a new entry:

```auto
<?php
$package_name = "somepackage";
$core_path = $modx->getOption('core_path', null, MODX_CORE_PATH );
$modx->addPackage($package_name, $core_path . 'components/' . $package_name . '/model/');

$new_object = $modx->newObject('somepackageTest');
$new_object->set('some_field', 'some value');
if ($new_object->save()) {
    return 'new row successfully created.';
} else {
    return 'error creating new row.';
}

```

To output the table content:

```auto
<?php
$package_name = "somepackage";
$core_path = $modx->getOption('core_path', null, MODX_CORE_PATH );
$modx->addPackage($package_name, $core_path . 'components/' . $package_name . '/model/');

$rows = $modx->getCollection('somepackageTest');
$output = '';
foreach ($rows as $row) {
    $output .= '<pre>' . print_r($row->toArray(), 1) . '</pre>';
}
return $output;

```

---

<div class="post-metadata">

**Author:** ![pakosfakos](https://community.modx.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pakosfakos](https://community.modx.com/u/pakosfakos)\
**Post date:** [January 29, 2024, 8:03pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/13 "2024-01-29T20:03:39Z")

</div>

yes this is doing the job, thank you for your advice and your time

---

<div class="post-metadata">

**Author:** ![pakosfakos](https://community.modx.com/letter_avatar_proxy/v4/letter/p/41988e/32.png) [@pakosfakos](https://community.modx.com/u/pakosfakos)\
**Post date:** [January 30, 2024, 2:35pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/14 "2024-01-30T14:35:28Z")

</div>

Perfect now is clear how it works Thanks alot

---

<div class="post-metadata">

**Author:** ![system](https://community.modx.com/user_avatar/community.modx.com/system/32/2_2.png) [@system](https://community.modx.com/u/system)\
**Post date:** [February 1, 2024, 2:35pm UTC](https://community.modx.com/t/get-data-from-table-using-snippet/7458/15 "2024-02-01T14:35:59Z")

</div>

Closed on February 1, 2024, 2:35pm UTC

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.
