|
| 1 | +# Cycle ORM integration for the Laravel Framework |
| 2 | + |
| 3 | +**!!! Important note!!!** |
| 4 | +At this moment this package work only with postgres database driver. |
| 5 | + |
| 6 | +### Requirements |
| 7 | +- Laravel 7.x |
| 8 | +- PHP 7.4 and above |
| 9 | + |
| 10 | +## Installation and Configuration |
| 11 | + |
| 12 | +From the command line run |
| 13 | +```shell script |
| 14 | +composer require butschster/cycle-orm |
| 15 | +``` |
| 16 | + |
| 17 | +### Env variables |
| 18 | + |
| 19 | +``` |
| 20 | +DB_CONNECTION=postgres |
| 21 | +DB_HOST=127.0.0.1 |
| 22 | +DB_PORT= # Default port 5432 |
| 23 | +DB_DATABASE=homestead |
| 24 | +DB_USERNAME=root |
| 25 | +DB_PASSWORD= |
| 26 | +
|
| 27 | +DB_MIGRATIONS_TABLE=migrations # Migrations table name |
| 28 | +
|
| 29 | +DB_SCHEMA_SYNC=false # Sync DB schema without migrations |
| 30 | +DB_SCHEMA_CACHE=true # Cache DB schema |
| 31 | +DB_SCHEMA_CACHE_DRIVER=file # DB schema cache driver |
| 32 | +``` |
| 33 | + |
| 34 | +### Configuration |
| 35 | + |
| 36 | +Publish the config file. |
| 37 | +```shell script |
| 38 | +php artisan vendor:publish --provider="Butschster\Cycle\Providers\LaravelServiceProvider" --tag=config |
| 39 | +``` |
| 40 | + |
| 41 | +**That's it!** |
| 42 | + |
| 43 | +### Console commands |
| 44 | + |
| 45 | +#### php artisan cycle:migrate |
| 46 | +Run cycle orm migrations from directory. |
| 47 | + |
| 48 | +#### php artisan cycle:refresh |
| 49 | +Refresh database schema. |
| 50 | + |
| 51 | +## Usage |
| 52 | +By default, class locator looks for entities in app folder. You can specify locations in `config/cycle.php` config file. |
| 53 | + |
| 54 | +```php |
| 55 | +... |
| 56 | +'directories' => [ |
| 57 | + app_path(), |
| 58 | +], |
| 59 | +... |
| 60 | +``` |
| 61 | + |
| 62 | +### Example |
| 63 | + |
| 64 | +#### User |
| 65 | +```php |
| 66 | +<?php |
| 67 | +namespace App; |
| 68 | + |
| 69 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 70 | + |
| 71 | +/** |
| 72 | + * @ORM\Entity( |
| 73 | + * table="users", |
| 74 | + * repository="App\UserRepository", |
| 75 | + * ) |
| 76 | + */ |
| 77 | +class User implements Authenticatable |
| 78 | +{ |
| 79 | + /** @ORM\Column(type="uuid", primary=true) */ |
| 80 | + protected string $id; |
| 81 | + |
| 82 | + /** @ORM\Column(type="string") */ |
| 83 | + protected string $password; |
| 84 | + |
| 85 | + /** @ORM\Column(type="string") */ |
| 86 | + protected string $rememberToken = ''; |
| 87 | + |
| 88 | + public function getId(): string |
| 89 | + { |
| 90 | + return $this->id; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return string |
| 95 | + */ |
| 96 | + public function getAuthIdentifierName(): string |
| 97 | + { |
| 98 | + return 'id'; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return string |
| 103 | + */ |
| 104 | + public function getAuthIdentifier(): string |
| 105 | + { |
| 106 | + return $this->getId(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return string |
| 111 | + */ |
| 112 | + public function getAuthPassword(): string |
| 113 | + { |
| 114 | + return $this->password; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param string $password |
| 119 | + */ |
| 120 | + public function setAuthPassword(string $password): void |
| 121 | + { |
| 122 | + $this->password = $password; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @return string |
| 127 | + */ |
| 128 | + public function getRememberToken(): string |
| 129 | + { |
| 130 | + return $this->rememberToken; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param string $value |
| 135 | + */ |
| 136 | + public function setRememberToken($value): void |
| 137 | + { |
| 138 | + $this->rememberToken = $value; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @return string |
| 143 | + */ |
| 144 | + public function getRememberTokenName(): string |
| 145 | + { |
| 146 | + return 'rememberToken'; |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +#### Repository |
| 152 | +```php |
| 153 | +namespace App; |
| 154 | + |
| 155 | +use Butschster\Cycle\Repository; |
| 156 | + |
| 157 | +class UserRepository extends Repository |
| 158 | +{ |
| 159 | + /** @inheritDoc */ |
| 160 | + public function findByUsername(string $username): ?User |
| 161 | + { |
| 162 | + return $this->findOne(['username' => $username]); |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +#### Create user |
| 168 | +```php |
| 169 | +use Cycle\ORM\ORMInterface; |
| 170 | +use Butschster\Cycle\Facades\ORM; |
| 171 | + |
| 172 | +$user = new User(); |
| 173 | +$repository = app(ORMInterface::class)->getRepository($user); |
| 174 | +// or |
| 175 | +$repository = ORM::getRepository($user); |
| 176 | +// or |
| 177 | +$repository = ORM::getRepository(User::class); |
| 178 | + |
| 179 | +$repository->persist($user); |
| 180 | +``` |
| 181 | + |
| 182 | +#### Update user |
| 183 | +```php |
| 184 | +use Butschster\Cycle\Facades\ORM; |
| 185 | + |
| 186 | +$repository = ORM::getRepository(User::class); |
| 187 | +$user = $repository->findByPK('5c9e177b0a975a6eeccf5960'); |
| 188 | +$user->setAuthPassword('secret'); |
| 189 | + |
| 190 | +$repository->persist($user); |
| 191 | +``` |
| 192 | + |
| 193 | +#### Delete user |
| 194 | +```php |
| 195 | +use Butschster\Cycle\Facades\ORM; |
| 196 | + |
| 197 | +$repository = ORM::getRepository(User::class); |
| 198 | +$user = $repository->findByPK('5c9e177b0a975a6eeccf5960'); |
| 199 | + |
| 200 | +$repository->delete($user); |
| 201 | +``` |
0 commit comments