---
title: "Generating Repositories"
description: "Generate the repositories of an ADR project for the tables of the database with make:repository."
version: "1.0"
---

> Documentation Index
> Fetch the complete documentation index at: https://crest.phalcon.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Generating Repositories

**ADR** For ADR projects (see the flavor table in [Overview](/1.0/introduction)).

An ADR project reads and writes its tables through repositories, not through the Active Record models of `make:model`. `make:repository` reads a table through the `db` service of the project, a `Phalcon\DataMapper\Pdo\Connection` (see [The database](/1.0/creating-a-project#the-database)), and writes a repository for it:

```bash
crest make:repository users
```

```text
Created src/Repository/Base/UsersRepositoryBase.php
Created src/Repository/UsersRepository.php
Nothing registers it: the container makes it when a class asks for it. Give
\App\Repository\UsersRepository to the constructor of an action or a domain class.
It needs the Phalcon\DataMapper\Pdo\Connection service, which the DbProvider
of crest new --database registers.
```

| File | Owner | On another run |
|---|---|---|
| `src/Repository/Base/UsersRepositoryBase.php` | Crest | writes it again when the table changes |
| `src/Repository/UsersRepository.php` | you | keeps it, unless `--force` |

The base has the name of the table, its primary key and its columns (the constants `TABLE`, `PRIMARY_KEY` and `COLUMNS`). It also has four methods that use the primary key: `find()`, `add()`, `update()` and `delete()`. A row is an array, with the column names as keys. `add()` gives the id of the new row, and a `null` value writes `NULL`. Add the other queries of your application to `UsersRepository`, through `$this->connection` and `$this->queryFactory`.

The class name comes from the table name, as for `make:model`: `user_profiles` gives `UserProfilesRepository`. The table must have a primary key with one column. Crest stops on a table with no primary key, on a composite primary key, and on a table that the database does not have.

Source: https://crest.phalcon.io/1.0/repositories/index.mdx
