# Prefix db passing to repository class

**URL:** <https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144>\
**Category:** Components & MVC\
**Tags:** laminas-db\
**Created:** [March 1, 2021, 4:11pm UTC](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144 "2021-03-01T16:11:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [March 1, 2021, 4:11pm UTC](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/1 "2021-03-01T16:11:32Z")

</div>

Hi,

what is the best practice for passing the table prefix to the repository. In my project, depending on the controller, I would like to retrieve data from repository queries, but with different prefix depending on the state of the master object. Let’s say for active products (a\_products table where I keep the product status) I fetch data from active related tables “a\_products\_category”, “a\_products\_info” etc, but when I change the product status to inactive the data from the tables are copied to the tables with inactive items like “a\_archive\_products\_category”, “a\_archive\_products\_info” etc. This solution is optimal for me in terms of database performance. The question is whether to pass the prefixes as a variable to each method called, or is there a better way.

---

<div class="post-metadata">

**Author:** ![ezkimo](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ezkimo/32/781_2.png) [@ezkimo](https://discourse.laminas.dev/u/ezkimo)\
**Post date:** [March 2, 2021, 9:30am UTC](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/2 "2021-03-02T09:30:31Z")

</div>

You mention repositories. Do you use an ORM system like Doctrine or do you use Laminas’ own DBAL system?

---

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [March 3, 2021, 1:51am UTC](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/3 "2021-03-03T01:51:40Z")

</div>

I dont use ORM. I use Laminas own DBAL system

---

<div class="post-metadata">

**Author:** ![kliker02](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kliker02](https://discourse.laminas.dev/u/kliker02)\
**Post date:** [March 10, 2021, 9:36pm UTC](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/4 "2021-03-10T21:36:41Z")

</div>

Hello!  
Step1: You Can create 4 different repositories:

1. Table named as a\_products\_category,
2. a\_products\_info,
3. a\_archive\_products\_category,  
4.a\_archive\_products\_info;

Step2:Create service which copy data from tables to archive  
Step3: check in controller for product status and call service method

I’ll do that.

Anyway i think pass table name(prefix) as param is a bad way.

---

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [March 11, 2021, 11:42am UTC](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/5 "2021-03-11T11:42:36Z")

</div>

Hi @ [kliker02](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/4)

Thank you for your suggestions. I had doubts about creating a repository copy of the tables a\_… for a\_archive\_… (basically the same functions) is not duplicating code and functions, i.e. denying object-oriented programming, and passing a parameter with a prefix will result in no copying of methods that basically do the same thing.

---

<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 11, 2021, 12:02pm UTC](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/6 "2021-03-11T12:02:12Z")

</div>

If a database is used or which table is a detail that is only of interest at the persistence layer!

For example, use a repository with the following methods:

```php
interface ProductRepositoryInterface
{
    public function fetchActiveProducts(): ProductCollection;

    public function fetchInactiveProducts(): ProductCollection;
}

```

This makes it uninteresting for the rest of the application where and how something is stored.

---

<div class="post-metadata">

**Author:** ![Kriss\_Kals](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kriss_kals/32/1295_2.png) [@Kriss\_Kals](https://discourse.laminas.dev/u/Kriss_Kals)\
**Post date:** [March 11, 2021, 12:05pm UTC](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/7 "2021-03-11T12:05:29Z")

</div>

Thank you for your help. I will do according to your instructions ( [froschdesign](https://discourse.laminas.dev/u/froschdesign) and [kliker02](https://discourse.laminas.dev/t/prefix-db-passing-to-repository-class/2144/4)).
