MIGX-db in a TV - how to filter the table by the resource id?

Hello,

I’m looking for a solution to filter a MIGX-DB list according to the id of the resource that contains this TV.

I have “event” type resources. Among the TVs in these resources, there’s one called “playlist”.
It’s a MIGX variable, filled in from a table (created with ExtraBuilder).

<model package="Qlive\Model" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" phpdoc-package="" version="3.0">
  <object class="Playlist\Model" table="qlive_playlists" extends="xPDO\Om\xPDOSimpleObject">
    <field key="evenement_id" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
    <field key="code_q" dbtype="varchar" precision="20" phptype="string" null="true" default=""/>
    <field key="titre" dbtype="varchar" precision="191" phptype="string" null="true" default=""/>
    <field key="interprete" dbtype="varchar" precision="191" phptype="string" null="true" default=""/>
    <field key="horodatage" dbtype="datetime" phptype="datetime" null="true"/>
    <field key="statut" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
  </object>
</model>

Each line of this table lists a song, its time of passage AND the event concerned.
(pieces are added to this table from the front, by indicating the event id concerned)

So I created a MIGX TV, entered the package and class in MIGXdb-settings, and it’s perfect:
in each Event document, I have the list of songs in the playlist displayed.

EXCEPT that I have ALL the songs from the playlist table.

The question: how can I limit this list to only those songs whose :

WHERE event_id = resource->get('id')

How can I ensure that the TV only lists the songs for the resource I’m in?
where do you specify this “WHERE event_id= the id of the current resource?

  • is this even possible?

Thank you !

If you have a field in your class with the name resource_id, then this should work automatically.

When “Check Resource” is set to yes in the MIGX config (tab “MIGXdb-Settings”), this field resource_id should get set when you add a new item in the manager and the list should get filtered by the ID of the current resource.


I’m not sure where the resource ID is stored in your schema.
Is “evenement_id” the resource ID?

If you can’t rename the field, then you probably have to create custom processors for “getlist” and “update” to make it work.

Hello halftrainedharry, and thank you again for your reply.

It doesn’t work. Let me explain…

There are “Evenements” resources (events)
For each event we have a list of songs,
all stored in a “playlist” table where the “resource_id” field = the id of an event.
The aim of the game is to display in a TV showing a read-only list of all songs played for each event.

I initially had a standard MIGX TV, but for each event there can be hundreds of lines.
So I thought it would be better to use a clean table, rather than a huge JSON value.
it’s super easy to do with ExtraBuilder (thank Jared Healy)

so÷

  • I’ve remade the “Playlist” class, replacing the “evenement_id” field with “resource_id” as you told me.
  • regenerated the schema, class and SQL table as required.
  • modified the snippet used to insert songs into the playlist table => $modx->newObject(Playlist…), etc.

Everything works fine.

  • I then created a new MIGX config: “playlists”.
  • I filled in MIGXdb-settings with my class: Qlive\Model\Playlist
  • I filled in Formtabs and Columns using the “select DB-fields” button
  • I created a TV playlist for “Evenement” resources using this config (Evenements id being the “resource_id” of the playlist).

=> The “Evenement” resources display the TV, with correct headers, but nothing in the table, no rows, even though there is data in the table.

=> if I add items to the playlist from the TV, they’re displayed in the table, but when I checked, I realized that they filled in the TV value in modx_site_tmplvar_contentvalues, but didn’t add the new items to the playlist table!

I don’t understand because
MIGX’s configuration seems to be correct, as it offers me the correct fields when I click on select DB-fields

So I created a CMP with namespace:migx & configs=playlist
and now this CMP displays the correct values for the playlist table! (so the MIGX config is really correct).

In the CMP, this lists the data for all events.
Hence my wish to use this MIGX config in a TV for Event resources, listing only the songs related to the event.
In fact, these playlists are a kind of log for each event.
They are filled in externally, and can be consulted in read-only mode in the Event resources.

I think at worst I could use the CMP by filtering by resource_id,
but it would still be super handy to have the playlist for each event in the event’s resource…

What is the “Input Type” of this TV?
Is it migx or migxdb?


Also what versions of MODX and MIGX are you using?
As far as I know, not every functionality of MIGX is yet tested with MODX 3.

In general, what you are trying to achieve should work with a MIGXdb-TV.
Maybe you could share your MIGX config (Right-click → “Export/Import”) and the schema so that I can try to reproduce it.

Thank you for your reply,

MODX 3.1.2pl
MIGX 3.0.2-beta1

The TV was indeed set to MIGX, not MIGXdb!
After making this change, the TV does display the results of the Playlist table, and no longer the value of tmplvar_contentvalues.
Similarly, when a song is added to the list from the TV, the Playlist table is filled.

So it’s better, but there are still 2 problems:

  1. When adding items (from TV or CMP)
    the ‘resource_id’ field doesn’t fill in correctly and remains at 0 (default value).

I think it’s coming from the MIGX config, since if I add elements from the snippet, the resource_id value is filled in correctly in the database. (and listed correctly in the TV or CMP).

  1. If I activate “Check Resource” in the MIGXdb-Settings to display only items where resource_id: the list remains completely empty.

There seems to be a problem with this resource_id field, but I don’t understand what it is, since when you use the snippet to add elements, it works fine.

The playlist Class :


<?xml version="1.0" encoding="UTF-8"?>
<model package="Qlive\Model" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" phpdoc-package="" version="3.0">
  <object class="Playlist" table="qlive_playlists" extends="xPDO\Om\xPDOSimpleObject">
    <field key="resource_id" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
    <field key="code_q" dbtype="varchar" precision="20" phptype="string" null="true" default=""/>
    <field key="titre" dbtype="varchar" precision="191" phptype="string" null="true" default=""/>
    <field key="interprete" dbtype="varchar" precision="191" phptype="string" null="true" default=""/>
    <field key="horodatage" dbtype="datetime" phptype="datetime" null="true"/>
    <field key="statut" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
  </object>
</model>

The MIGX config

{
  "formtabs":[
    {
      "MIGX_id":5,
      "caption":"Une playlist",
      "print_before_tabs":"0",
      "fields":[
        {
          "MIGX_id":32,
          "field":"id",
          "caption":"id",
          "description":"",
          "description_is_code":"0",
          "inputTV":"",
          "inputTVtype":"",
          "validation":"",
          "configs":"",
          "restrictive_condition":"",
          "display":"none",
          "sourceFrom":"config",
          "sources":"",
          "inputOptionValues":"",
          "default":"",
          "useDefaultIfEmpty":"0",
          "pos":1
        },
        {
          "MIGX_id":33,
          "field":"resource_id",
          "caption":"resource_id",
          "pos":2
        },
        {
          "MIGX_id":34,
          "field":"code_q",
          "caption":"code_q",
          "pos":3
        },
        {
          "MIGX_id":35,
          "field":"titre",
          "caption":"titre",
          "pos":4
        },
        {
          "MIGX_id":36,
          "field":"interprete",
          "caption":"interprete",
          "pos":5
        },
        {
          "MIGX_id":37,
          "field":"horodatage",
          "caption":"horodatage",
          "pos":6
        },
        {
          "MIGX_id":38,
          "field":"statut",
          "caption":"statut",
          "pos":7
        }
      ],
      "pos":1
    }
  ],
  "contextmenus":"update||remove",
  "actionbuttons":"addItem",
  "columnbuttons":"update||remove",
  "filters":"",
  "extended":{
    "migx_add":"",
    "disable_add_item":"",
    "add_items_directly":"",
    "formcaption":"",
    "update_win_title":"",
    "win_id":"",
    "maxRecords":"",
    "addNewItemAt":"bottom",
    "media_source_id":"",
    "multiple_formtabs":"",
    "multiple_formtabs_label":"",
    "multiple_formtabs_field":"",
    "multiple_formtabs_optionstext":"",
    "multiple_formtabs_optionsvalue":"",
    "actionbuttonsperrow":4,
    "winbuttonslist":"",
    "extrahandlers":"",
    "filtersperrow":4,
    "packageName":"Qlive\\Model",
    "classname":"Qlive\\Model\\Playlist",
    "task":"",
    "getlistsort":"",
    "getlistsortdir":"",
    "sortconfig":"",
    "gridpagesize":"",
    "use_custom_prefix":"0",
    "prefix":"",
    "grid":"",
    "gridload_mode":1,
    "check_resid":1,
    "check_resid_TV":"",
    "join_alias":"",
    "has_jointable":"yes",
    "getlistwhere":"",
    "joins":"",
    "hooksnippets":"",
    "cmpmaincaption":"Playlists",
    "cmptabcaption":"Playlists",
    "cmptabdescription":"liste des morceau jou\u00e9s",
    "cmptabcontroller":"",
    "winbuttons":"",
    "onsubmitsuccess":"",
    "submitparams":""
  },
  "permissions":{
    "apiaccess":"",
    "view":"",
    "list":"",
    "save":"",
    "create":"",
    "remove":"",
    "delete":"",
    "publish":"",
    "unpublish":"",
    "viewdeleted":"",
    "viewunpublished":""
  },
  "fieldpermissions":"",
  "columns":[
    {
      "MIGX_id":2,
      "dataIndex":"id",
      "header":"id"
    },
    {
      "MIGX_id":3,
      "dataIndex":"resource_id",
      "header":"resource_id"
    },
    {
      "MIGX_id":4,
      "dataIndex":"code_q",
      "header":"code_q"
    },
    {
      "MIGX_id":5,
      "dataIndex":"titre",
      "header":"titre"
    },
    {
      "MIGX_id":6,
      "dataIndex":"interprete",
      "header":"interprete"
    },
    {
      "MIGX_id":7,
      "dataIndex":"horodatage",
      "header":"horodatage"
    },
    {
      "MIGX_id":8,
      "dataIndex":"statut",
      "header":"statut"
    }
  ],
  "category":""
}

the snippet used to add items to the playlist

 $track = $modx->newObject(Playlist::class);
            $track->set('code_q', 'CODE-Q');
            $track->set('titre', 'titre');
            $track->set('interprete', 'artiste');
            $track->set('statut', '1');
            $track->set('horodatage', '2025-04-25 12:03');
            $track->set('resource_id', '12');
            $track->save();

another interesting clue:
If in MIGXdb-Setting I select Check Resource=YES

  • the TV list is empty
  • when an item is added from the TV or CMP, the resource_id value is correctly entered in the database.

If I select Check Resource=NO in MIGXdb-Setting

  • the TV list displays all items
  • when an item is added from the TV or CMP, the resource_id value remains at 0 in the database.

Here is how to make it work (for a MIGXdb-TV):

  • Install the lastest version of MIGX (3.0.5-beta) directly from the Github repo. Changes were made to fix bugs with MIGX in MODX 3 that aren’t available in version 3.0.2-beta1. (I don’t know why the newer versions were never added to the official package provider.) Download the ZIP from Github and then use drap&drop to add it to the list in the “Package Management”.

  • Remove the field “resource_id” from the MIGX config (tab “Formtabs”). “resource_id” gets set automatically and a field with the same name interferes.


Other things you could change in the MIGX config:

  • “Load Grid” = auto (tab “MIGXdb-Settings”) could make the handling easier.
  • The field “Package” (tab “MIGXdb-Settings”) doesn’t have to be filled in. It can be left empty in most cases and must have the value qlive (instead of Qlive\Model) if you use custom processors or other customization.

I followed your instructions:

  • migx 3.0.5
  • delete the resource_id field
  • leave Package field empty

I can confirm that everything now works as I’d hoped:

  • In each Event resource, the TV Playlist displays only the items from the Playlist table corresponding to the resource id.

  • Adding items from the resource correctly populates the resource_id field in the database.
    (which I don’t really need, but it proves that everything works perfectly)

Perfect!

Concerning “Load Grid” = auto
=> I prefer loading with the button :

  • if you leave auto, the grid isn’t displayed across the entire width, which hides part of the datetime field, for example.
  • If you load with the button, the grid takes up the whole width of the page, which is much more comfortable.
    What’s more, I think it avoids loading data when you don’t need it, which is much greener…

I am very grateful to you for helping me.
Thank you.