Skip to content

Commit

Permalink
Various fixes (#385)
Browse files Browse the repository at this point in the history
Co-authored-by: Tigrov <rrr-r@ya.ru>
  • Loading branch information
samdark and Tigrov authored Aug 29, 2024
1 parent d6591f4 commit 05942a1
Show file tree
Hide file tree
Showing 28 changed files with 214 additions and 204 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<a href="https://github.com/yiisoft" target="_blank">
<img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px" alt="Yii">
</a>
<h1 align="center">Yii ActiveRecord Library</h1>
<h1 align="center">Yii ActiveRecord</h1>
<br>
</p>

Expand Down
27 changes: 14 additions & 13 deletions docs/create-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class properties, the table name and relations.

### Dynamic properties

Easiest way to define properties is to use dynamic properties. This way you don't need to define properties explicitly.
The easiest way to define properties is to use dynamic properties.
This way you don't need to define properties explicitly.

```php
use Yiisoft\ActiveRecord\ActiveRecord;
Expand All @@ -20,7 +21,7 @@ use Yiisoft\ActiveRecord\ActiveRecord;
* @property string $email
*
* The properties in PHPDoc are optional and used by static analysis and by IDEs for autocompletion, type hinting,
* code generation and inspection tools. This does not affect code execution.
* code generation, and inspection tools. This doesn't affect code execution.
**/
#[\AllowDynamicProperties]
final class User extends ActiveRecord
Expand All @@ -45,8 +46,8 @@ $user->save();

Notes:
- It needs to use the `#[\AllowDynamicProperties]` attribute to enable dynamic properties;
- ❌ It does not use strict typing and can be a reason of hard-to-detect errors;
- ❌ It is slower than explicitly defined properties, it is not optimized by PHP opcache and uses more memory;
- ❌ It doesn't use strict typing and can be a reason of hard-to-detect errors;
- ❌ It is slower than explicitly defined properties, it isn't optimized by PHP opcache and uses more memory;

### Public properties

Expand Down Expand Up @@ -75,7 +76,7 @@ final class User extends ActiveRecord
As with dynamic properties, you can use `$user->id`, `$user->username`, `$user->email` to access the properties.

Notes:
- ✔️ It allows to use strict typing and define default values for properties;
- ✔️ It allows using strict typing and define default values for properties;
- ✔️ It works faster than dynamic properties, optimized by PHP opcache and uses less memory;

### Protected properties (recommended)
Expand Down Expand Up @@ -145,9 +146,9 @@ $user->save();

Notes:
- To access properties, you need to define getter and setter methods.
- ✔️ It allows to use strict typing and define default values for properties;
- ✔️ It allows to access uninitialized properties, using **null coalescing operator** `return $this->id ?? null;`
- ✔️ It allows to reset relations when setting the property, using `ActiveRecordInterface::setAttribute()` method.
- ✔️ It allows using strict typing and define default values for properties;
- ✔️ It allows accessing uninitialized properties, using **null coalescing operator** `return $this->id ?? null;`
- ✔️ It allows resetting relations when setting the property, using `ActiveRecordInterface::setAttribute()` method.

### Private properties

Expand Down Expand Up @@ -206,7 +207,7 @@ use Yiisoft\ActiveRecord\Trait\MagicPropertiesTrait;
* @property string $email
*
* The properties in PHPDoc are optional and used by static analysis and by IDEs for autocompletion, type hinting,
* code generation and inspection tools. This does not affect code execution.
* code generation, and inspection tools. This doesn't affect code execution.
**/
final class User extends ActiveRecord
{
Expand All @@ -223,11 +224,11 @@ You can use `$user->id`, `$user->username`, `$user->email` to access the propert

Notes:
- It needs to use the `MagicPropertiesTrait` to enable magic properties;
- Compared to dynamic properties, they are stored in the `private array $attributes` property;
- ✔️ It allows to access relations as properties;
- ❌ It does not use strict typing and can be a reason of hard-to-detect errors;
- Compared to dynamic properties, they're stored in the `private array $attributes` property;
- ✔️ It allows accessing relations as properties;
- ❌ It doesn't use strict typing and can be a reason of hard-to-detect errors;
- ❌ It is slower than explicitly defined properties, it is not optimized by PHP opcache and uses more memory.
In some cases it can be 100 times slower than explicitly defined properties;
Sometimes it can be 100 times slower than explicitly defined properties;

## Relations

Expand Down
4 changes: 2 additions & 2 deletions docs/define-connection.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Define the DB connection for Active Record
# Define the database connection for Active Record

To use the Active Record, you need to define the DB connection in one of the following ways:

## Using the bootstrap configuration

Add the following code to the configuration file, for example in `config/common/bootstrap.php`:
Add the following code to the configuration file, for example, in `config/common/bootstrap.php`:

```php
use Psr\Container\ContainerInterface;
Expand Down
12 changes: 6 additions & 6 deletions docs/define-relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ $this->hasOne(User::class, ['id' => 'user_id']);

### Many-to-many

The relationships are used when you need to link multiple records from one table to multiple records from another table.
This is common in scenarios where entities have a bidirectional relationship, such as users belonging to multiple groups
Use relationships when you need to link multiple records from one table to multiple records from another table.
This is common when entities have a bidirectional relationship, such as users belonging to multiple groups
and groups having multiple users.

```mermaid
Expand Down Expand Up @@ -348,8 +348,8 @@ Use this method when you don't need to store additional information in the junct

## Inverse Relations

An inverse relation is a relation that is defined in the related record to link back to the current record. It is used
to associate the related record(s) with the current record in a more efficient way by avoiding additional queries.
An inverse relation is a relation defined in the related record to link back to the current record.
It associates the related record(s) with the current record in a more efficient way by avoiding additional queries.

To define an inverse relation, use the `ActiveQueryInterface::inverseOf()` method.

Expand Down Expand Up @@ -393,8 +393,8 @@ final class Order extends ActiveRecord

## Eager Loading

**Relations are loaded lazily**, meaning that the related record(s) are not loaded until you access them. This allows
you to load only the data you need and avoid unnecessary queries.
**Relations are loaded lazily**, meaning that the related record(s) aren't loaded until you access them.
This allows you to load only the data you need and avoid unnecessary queries.

However, there are cases when you need to load the related record(s) in advance to avoid the **N+1 query problem**.
To do this, use the `ActiveQueryInterface::with()` method.
Expand Down
37 changes: 19 additions & 18 deletions docs/internals.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Internals

This package can be tested globally or individually for each DBMS.
You can test this package either globally or individually for each DBMS.

- [MSSQL](https://github.com/yiisoft/db-mssql)
- [MySQL/MariaDB](https://github.com/yiisoft/db-mysql)
Expand All @@ -10,59 +10,60 @@ This package can be tested globally or individually for each DBMS.

## GitHub actions

All our packages have GitHub actions by default, so you can test your [contribution](https://github.com/yiisoft/db/blob/master/.github/CONTRIBUTING.md) in the cloud.
All packages have GitHub actions by default, so you can test your [contribution](https://github.com/yiisoft/db/blob/master/.github/CONTRIBUTING.md) in the cloud.

> Note: We recommend pull requesting in draft mode until all tests pass.
## Docker images

For greater ease we recommend to use Docker container for each DBMS. For this you can use the [docker-compose.yml](https://docs.docker.com/compose/compose-file/) file that's in the root directory of each package.
For greater ease, we recommend to use Docker container for each DBMS.
For this, you can use the [docker-compose.yml](https://docs.docker.com/compose/compose-file/) file in the root directory of each package.

- [MSSQL 2022](https://github.com/yiisoft/db-mssql/blob/master/docker-compose.yml)
- [MySQL 8](https://github.com/yiisoft/db-mysql/blob/master/docker-compose.yml)
- [MariaDB 10.11](https://github.com/yiisoft/db-mysql/blob/master/docker-compose-mariadb.yml)
- [Oracle 21](https://github.com/yiisoft/db-oracle/blob/master/docker-compose.yml)
- [PostgreSQL 15](https://github.com/yiisoft/db-pgsql/blob/master/docker-compose.yml)

To run Docker containers you can use the following command:
To run Docker containers, you can use the following command:

```shell
docker compose up -d
```

### Global testing

The following steps are required to run tests.
To run tests, follow these steps.

1. Install all DBMS dependencies with composer.

```shell
composer require --dev yiisoft/db-mssql yiisoft/db-mysql yiisoft/db-oracle yiisoft/db-pgsql yiisoft/db-sqlite --ansi
```
```shell
composer require --dev yiisoft/db-mssql yiisoft/db-mysql yiisoft/db-oracle yiisoft/db-pgsql yiisoft/db-sqlite --ansi
```

2. Run all Docker containers for each DBMS.
3. Run the tests.

```shell
vendor/bin/phpunit
```
```shell
vendor/bin/phpunit
```

### Individual testing

The following steps are required to run tests.
To run tests, follow these steps.

1. Install DBMS dependencies with Composer.

```shell
composer require --dev yiisoft/db-pgsql --ansi
```
```shell
composer require --dev yiisoft/db-pgsql --ansi
```

2. Run the Docker container for the DBMS you want to test.
3. Run the tests.

```shell
vendor/bin/phpunit --testsuite=Pgsql
```
```shell
vendor/bin/phpunit --testsuite=Pgsql
```

Suites available:

Expand Down
4 changes: 2 additions & 2 deletions docs/using-di.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Using Dependency Injection With Active Record

Using [dependency injection](https://github.com/yiisoft/di) in the Active Record model allows to inject dependencies
Using [dependency injection](https://github.com/yiisoft/di) in the Active Record model allows injecting dependencies
into the model and use them in the model methods.

To create an Active Record model with dependency injection, you need to use
Expand Down Expand Up @@ -62,7 +62,7 @@ final class User extends ActiveRecord
}
```

This will allow to create the `ActiveQuery` instance without calling `ActiveRecord::withFactory()` method.
This will allow creating the `ActiveQuery` instance without calling `ActiveRecord::withFactory()` method.

```php
$userQuery = new ActiveQuery($factory->create(User::class));
Expand Down
Loading

0 comments on commit 05942a1

Please sign in to comment.