Forum

Thread tagged as: Addons, Api, Add-on-development

Use PerchAPI_Factory without the perch_ prefix

I am trying to manage a pre-existing DB on a site, and have fallen at the first hurdle. I have got the FactoryAPI object, but the table I am trying to read is called 'Supermarket' whereas due to how I believe perch handles it's DB requests, it will try to find 'perch_Supermarket'.

Is there a way for my app to tell it to not use the prefix for this object?

Dan Duke

Dan Duke 1 points

  • 6 years ago

As a workaround, I have realised I can use MYSQL views procedures to map 'perchs_supermarket' to the select * from supermarket query. Not ideal, but may be only way looking at the applications source code. Define seems to ingrained to modify at the app level.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can always drop the prefix (set it to an empty string) if you're never going to have more than one site in the same database.

But then you lose the nice seperation of tables as to what is owned by the application (in this case, it's an MVC app) and what is owned by Perch. in this specific case, the table is owned by the application really, with purge doing a little management of it. In an ideal world, I would be able to use the prefix for my perch site, but in my application, when I use the factory, alongside the table variable, I can have a 'table_prefix' override variable too.

In your opinion, is the 'view' approach to this problem a bad one with regards to how perch works?

Drew McLellan

Drew McLellan 2638 points
Perch Support

The view should be fine as long as you're only reading from the table.

The problem here is you're mixing API code that has one set of rules with a database table that follows another. I'm not saying it can't be done, but you should expects some compromises if you take that approach.

Okay, I may also need to write, which appears to also be working with the view. It may be the way workbench has implimented it for me though.

I have it all working anyway, it was more a desire to keep the MVC tables the same, and have perch manage things, which it is doing at the moment. If I have to rename and migrate the DB table to also have the prefix, I will do that. Not ideal, but will do the trick.

I am not sure on your point around mixing the code though, and how that actually affects the rules the DB table follows. The MVC table doesn't mind the name, and I understand that perch has a prefix, and in 99.99% of cases, that would be fine, but an override so use can retrofit perch (it's bit lure for me) into an old framework easily, would be handy.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I had no idea you could write through views. That said, I don't think I've used them with MySQL.