Skip to content

Commit

Permalink
update docs for latest DbalMapper & generics
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 4, 2023
1 parent 191ecbb commit e8a90db
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"phpstan/phpstan-nette": "1.2.3",
"phpstan/phpstan-mockery": "1.1.0",
"phpstan/phpstan-strict-rules": "1.4.5",
"nextras/orm-phpstan": "~1.0",
"nextras/orm-phpstan": "~1.0@dev",
"marc-mabe/php-enum-phpstan": "dev-master",
"tracy/tracy": "~2.3"
},
Expand Down
35 changes: 27 additions & 8 deletions docs/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ Table names are directly resolved in the mapper layer; they are derived from the
If you would like to force some other table name, define `$tableName` property, or override `getTableName()` method in the mapper class.

```php
use Nextras\Orm\Mapper\Mapper;
use Nextras\Orm\Mapper\Dbal\DbalMapper;

class EventsMapper extends Mapper
/**
* @extends DbalMapper<Event>
*/
class EventsMapper extends DbalMapper
{
protected $tableName = 'events';

Expand Down Expand Up @@ -42,10 +45,13 @@ These predefined classes assume "camelCase" naming in the entity layer and trans
You are free to add your own mapping. Just call `setMapping($entityName, $storageName)` method. The right way to do this is to inherit `createConventions()` method in your mapper class.

```php
use Nextras\Orm\Mapper\Mapper;
use Nextras\Orm\Mapper\Dbal\DbalMapper;
use Nextras\Orm\Mapper\Dbal\Conventions\IConventions;

class EventsMapper extends Mapper
/**
* @extends DbalMapper<Event>
*/
class EventsMapper extends DbalMapper
{
protected function createConventions(): IConventions
{
Expand All @@ -69,6 +75,9 @@ class File extends Nextras\Orm\Entity\Entity
{
}

/**
* @extends DbalMapper<File>
*/
class FilesMapper extends Nextras\Orm\Mapper\Dbal\DbalMapper
{
protected function createConventions(): Nextras\Orm\Mapper\Dbal\Conventions\IConventions
Expand Down Expand Up @@ -97,6 +106,9 @@ class File extends Nextras\Orm\Entity\Entity
{
}

/**
* @extends DbalMapper<File>
*/
class FilesMapper extends Nextras\Orm\Mapper\Dbal\DbalMapper
{
protected function createConventions(): Nextras\Orm\Mapper\Dbal\Conventions\IConventions
Expand All @@ -114,11 +126,15 @@ class FilesMapper extends Nextras\Orm\Mapper\Dbal\DbalMapper
There are many possibilities to change default table joining conventions. If you are using `m:m`, you can change its pattern property. By default, the pattern is defined as `%s_x_%s`. The first placeholder is the primary table name.

```php
use Nextras\Orm\Mapper\Mapper;
use Nextras\Orm\Mapper\Dbal\Conventions\Conventions;
use Nextras\Orm\Mapper\Dbal\Conventions\IConventions;
use Nextras\Orm\Mapper\Dbal\DbalMapper;

class BaseMapper extends Mapper
/**
* @template E of \Nextras\Orm\Entity\IEntity
* @extends DbalMapper<E>
*/
class BaseMapper extends DbalMapper
{
protected function createConventions(): IConventions
{
Expand All @@ -133,9 +149,12 @@ class BaseMapper extends Mapper
If you need more advanced configuration, feel free to override `getManyHasManyParameters()` method in your mapper. This method returns an array where the first value is a joining table name, the second is an array of joining keys/columns. If you have only one `m:m` relationship between two entities, you can return the result based only on the passed target mapper, source property's metadata are available for more detailed matching.

```php
use Nextras\Orm\Mapper\Mapper;
use Nextras\Orm\Mapper\Dbal\DbalMapper;

class EmployeesMapper extends Mapper
/**
* @extends DbalMapper<Employee>
*/
class EmployeesMapper extends DbalMapper
{
public function getManyHasManyParameters(PropertyMetadata $sourceProperty, DbalMapper $targetMapper): array
{
Expand Down
3 changes: 3 additions & 0 deletions docs/entity-sti.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class PublicAddress extends Address
{
}

/**
* @extends Nextras\Orm\Repository\Repository<Address>
*/
class AddressesRepository extends Nextras\Orm\Repository\Repository
{
public static function getEntityClassNames(): array
Expand Down
12 changes: 9 additions & 3 deletions docs/mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ Dbal mapper uses [Nextras Dbal][1] library. Both Nextras Dbal and Orm support th
- Postgres,
- SQL Server (currently not supported auto-update mapping).

Dbal mapper is aliased as `Nextras\Orm\Mapper\Mapper` class. To set mapper's database **table name** set `$tableName` property or override `getTableName()` method.
To set mapper's database **table name** set `$tableName` property or override `getTableName()` method.

```php
class BooksMapper extends Nextras\Orm\Mapper\Mapper
/**
* @extends DbalMapper<Book>
*/
class BooksMapper extends Nextras\Orm\Mapper\Dbal\DbalMapper
{
protected $tableName = 'tbl_book';

Expand All @@ -37,7 +40,10 @@ If it is impossible to filter data by the repository layer API, you can write mo
You can get a new query builder instance by calling the `builder()` method. An instance of the current database connection is available in `$connection` property. Always wrap the result to collection with `toCollection()` call.

```php
class BooksMapper extends Nextras\Orm\Mapper\Mapper
/**
* @extends DbalMapper<Book>
*/
class BooksMapper extends Nextras\Orm\Mapper\Dbal\DbalMapper
{
/** @return Nextras\Orm\Collection\ICollection<Book> */
public function getRandomBooksByBuilder(): Nextras\Orm\Collection\ICollection
Expand Down
9 changes: 8 additions & 1 deletion docs/repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Repository provides `findAll()` method, which returns `Nextras\Orm\Collection\IC
Repository has to define a static method `getEntityClassNames()` that returns an array of entity names that the repository produce. Repository itself can contain user defined methods:

```php
/**
* @extends Repository<Book>
*/
final class BooksRepository extends Repository
{
static function getEntityClassNames(): array
Expand Down Expand Up @@ -46,13 +49,17 @@ Sometimes, it is needed to write pure SQL queries. SQL queries can be written on
```php
/**
* @method ICollection<Book> findBooksWithEvenId()
* @extends Repository<Book>
*/
final class BooksRepository extends Repository
{
// ...
}

final class BooksMapper extends Mapper
/**
* @extends DbalMapper<Book>
*/
final class BooksMapper extends DbalMapper
{
/** @return ICollection<Book> */
public function findBooksWithEvenId(): ICollection
Expand Down

0 comments on commit e8a90db

Please sign in to comment.