Getting multiple table names from block controller - getBlockTypeDatabaseTable()

Almost all blocks have a primary database table for storing information (structured within the db.xml file). As a developer you can pull the name of that table using the getBlockTypeDatabaseTable() function on any block type controller. However, in the situation where there are MULTIPLE tables within the db.xml file, it still only pulls the first one. I’ve searched the source code all over, and can’t find any answers. Has anyone worked out a way to extra ALL the table names from a given block type?

Not sure if there is anything in core.
But you can always use SimpleXML or similar library to read db.xml and convert it to array.
Actually built-in PHP method simplexml_load_file() should do it automatically and then you just loop through array to get values.

Thanks for the tip. The challenge I’ve got is doing this on a larger scale with unknown blocks created by 3rd party developers. So your method might work well because I hit a wall at not knowing the primary key of the secondary tables (or being able to do it efficiently at scale). But using SimpleXML should be able to pull out the primary keys much easier.

Technically you have access to the global variable $this->btExportTables from the block’s controller. That variable should list all tables added by the block.
But it is up to the developer to use it or not so there is no guarantee it’ll give you what you want. So @Parasek is right, his solution is probably the best one.
I would point out however that I saw packages with blocks for which the developer had put extra block tables in a package root db.xml file instead of the block root. So things work as long as developers follow conventions. Otherwise you’re pretty much left guessing.

1 Like

Thanks for the feedback. That was actually the first approach I had tried but in order to search the secondary tables I’d need to know the primary keys that (usually) match to the main table. That could be done with a separate SQL statement which is more resource hungry, or read the full XML file which has the key identified. But I’m thinking there’s no silver bullet. Hopefully this also helps other developers looking for the same info in the future.