# Prefix for tables

**URL:** <https://discourse.laminas.dev/t/prefix-for-tables/2796>\
**Category:** Components & MVC\
**Tags:** laminas-db\
**Created:** [March 6, 2022, 5:03am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796 "2022-03-06T05:03:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![voltan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/voltan/32/710_2.png) [@voltan](https://discourse.laminas.dev/u/voltan)\
**Post date:** [March 6, 2022, 5:03am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/1 "2022-03-06T05:03:58Z")

</div>

Hello,  
do you have any way (or it’s possible) to add some custom prefix in table names, for example, I am at work on a user module, I have an `account` table. and I want to model or repository automatically add this prefix. for example

`p18g_account` or `p18g_user_account`,

its good if it can be added on config files

Edit: `p18g` is random prefix for add to all of the tables for more security

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [March 7, 2022, 9:40am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/2 "2022-03-07T09:40:00Z")

</div>

In the TableGatewayFactory, you could get the prefix from the config, and then get on with creating the tablegateway:

```auto
<?php
// module/Account/src/Model/Factory/AccountTableGatewayFactory.php

namespace Account\Model\Factory;

//use statements here

class AccountTableGatewayFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        //get the database adaper
        $dbAdapter = $container->get(AdapterInterface::class);
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new Account());
        //get the table prefix from config
        $tablePrefix = $container->get("config")["table_prefix"];
        return new TableGateway($tablePrefix.'_account', $dbAdapter, null, $resultSetPrototype);
    }
}

```

And then in a config file of your choice:

```auto
<?php
return [
  "table_prefix" => "pg18"
];

```

---

<div class="post-metadata">

**Author:** ![voltan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/voltan/32/710_2.png) [@voltan](https://discourse.laminas.dev/u/voltan)\
**Post date:** [March 10, 2022, 7:17am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/3 "2022-03-10T07:17:11Z")

</div>

Thank you very much, But by this config, I have to set `table_prefix` for each table manually, I looking for a method to add this setting just in one area, does laminas-db have this option?

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [March 10, 2022, 9:09am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/4 "2022-03-10T09:09:35Z")

</div>

I am afraid it does not by default - _however_ you could introduce a subclass of AbstractTableGateway and handle the table prefix there. you will still need to change any references on the original TableGateway-class to your own implementation.  
Sample code from the [docs](https://docs.laminas.dev/laminas-db/table-gateway/#tablegateway-features) (I don’t know if you would need the GlobalAdapterFeature, but if I were to guess, you don’t.)

```auto
use Laminas\Db\TableGateway\Feature;

class MyTableGateway extends AbstractTableGateway
{
    public function __construct($prefix)
    {
        $this->table = $prefix.'_my_table';
        $this->initialize();
    }
}

```

`$prefix` could come from a TableGatewayFactory where you get it from the configuration as mentioned in previous posts.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 10, 2022, 9:17am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/5 "2022-03-10T09:17:49Z")

</div>

> [@makxk](#):
>
> _however_ you could introduce a subclass of AbstractTableGateway and handle the table prefix there. you will still need to change any references on the original TableGateway-class to your own implementation.

Why should the table gateway class be extended if only the name of the table is to be changed?

A custom [`TableIdentifier`](https://github.com/laminas/laminas-db/blob/2.14.x/src/Sql/TableIdentifier.php#L12) can be used here and besides the table gateway this also works for the `Sql` classes of laminas-db.

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [March 10, 2022, 9:25am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/6 "2022-03-10T09:25:14Z")

</div>

They asked for a option to set the prefix _in one area_ - if setting a TableIdentifier would do that too, that’s cool and the idea with extending is to be entirely blamed on me thinking too big lol

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 10, 2022, 9:30am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/7 "2022-03-10T09:30:23Z")

</div>

I liked your idea with the factory more. 😃

* * *

But regardless of that, I wouldn’t put so much energy into that component anymore because it will be marked as feature-complete.

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [March 10, 2022, 10:37am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/8 "2022-03-10T10:37:00Z")

</div>

Thank you, that means much to me coming from you 😃  
To be honest and after some thought, I like my first approach more too.  
Ultimately, the choice is up to Voltan, though.

* * *

Regardless of the state of laminas-db, it is still widely used and installed in skeleton applications - which means that we do need to make do with laminas-db until someone comes up with a migration path to doctrine 😃

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 10, 2022, 11:10am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/9 "2022-03-10T11:10:42Z")

</div>

> [@makxk](#):
>
> which means that we do need to make do with laminas-db until someone comes up with a migration path to doctrine

You can always use something other than laminas-db because it is not a required dependency for laminas-mvc or Mezzio.  
I have not used the component myself for several years.

---

<div class="post-metadata">

**Author:** ![voltan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/voltan/32/710_2.png) [@voltan](https://discourse.laminas.dev/u/voltan)\
**Post date:** [March 12, 2022, 6:49pm UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/10 "2022-03-12T18:49:15Z")

</div>

Hello

Then your idea is to stop using `laminas-db` and use another component for the database. What component is suggested?

Is laminas-db dead?

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [March 14, 2022, 7:45am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/11 "2022-03-14T07:45:59Z")

</div>

A widely used database component would be [doctrine](https://www.doctrine-project.org/), I suppose the majority use this instead of laminas-db.

About the state of laminas-db, see [this thread](https://discourse.laminas.dev/t/is-laminas-db-abandoned/2721). It has been decided to mark laminas-db as “feature-complete”.

---

<div class="post-metadata">

**Author:** ![voltan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/voltan/32/710_2.png) [@voltan](https://discourse.laminas.dev/u/voltan)\
**Post date:** [March 16, 2022, 5:56am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/12 "2022-03-16T05:56:24Z")

</div>

Then I should try updating my DB component doctrine, do you have any example or document for starting?

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [March 16, 2022, 9:42am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/13 "2022-03-16T09:42:48Z")

</div>

I am currently working on switching to Doctrine, too.

There’s a module for laminas which integrates the ORM: [Introduction - Doctrine Doctrine ORM Module for Laminas](https://www.doctrine-project.org/projects/doctrine-orm-module/en/5.1/index.html#introduction) whose docs are helping me greatly.  
Doctrine also has an example which originally outlines the usage of hydrators together with laminas-form, there are some tricks to take away from that one:  
[A Complete Example using Laminas\Form - Doctrine Doctrine Laminas Hydrators](https://www.doctrine-project.org/projects/doctrine-laminas-hydrator/en/3.0/laminas-form.html#a-complete-example-using-laminas-form)

---

<div class="post-metadata">

**Author:** ![voltan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/voltan/32/710_2.png) [@voltan](https://discourse.laminas.dev/u/voltan)\
**Post date:** [March 16, 2022, 9:46am UTC](https://discourse.laminas.dev/t/prefix-for-tables/2796/14 "2022-03-16T09:46:14Z")

</div>

Thank you very much both of them are good documents, I will start to switch my projects to Doctrine in next days
