1. Go to this page and download the library: Download butschster/cycle-orm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$dbal = $this->app->get(\Spiral\Database\DatabaseManager::class);
// Or
$dbal = $this->app->get(\Spiral\Database\DatabaseProviderInterface::class);
...
'directories' => [
app_path(),
],
...
EntityManager::persist($entity);
// Or
app(\Butschster\Cycle\Contracts\EntityManager::class)->persist($entity);
// Or
use Butschster\Cycle\Contracts\EntityManager;
class ExampleController extends Controller
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
}
$article = EntityManager::findByPK('App\Article', 1);
$article->setTitle('Different title');
$article2 = EntityManager::findByPK('App\Article', 1);
if ($article === $article2) {
echo "Yes we are the same!";
}
$article = new Article;
$article->setTitle('Let\'s learn about persisting');
EntityManager::persist($article);
EntityManager::delete($article);
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
/**
* @ORM\Entity(
* table="users",
* repository="App\UserRepository",
* )
*/
class User implements Authenticatable
{
/** @ORM\Column(type="uuid", primary=true) */
protected string $id;
/** @ORM\Column(type="string") */
protected string $password;
/** @ORM\Column(type="string") */
protected string $rememberToken = '';
public function getId(): string
{
return $this->id;
}
/**
* @return string
*/
public function getAuthIdentifierName(): string
{
return 'id';
}
/**
* @return string
*/
public function getAuthIdentifier(): string
{
return $this->getId();
}
/**
* @return string
*/
public function getAuthPassword(): string
{
return $this->password;
}
/**
* @param string $password
*/
public function setAuthPassword(string $password): void
{
$this->password = $password;
}
/**
* @return string
*/
public function getRememberToken(): string
{
return $this->rememberToken;
}
/**
* @param string $value
*/
public function setRememberToken($value): void
{
$this->rememberToken = $value;
}
/**
* @return string
*/
public function getRememberTokenName(): string
{
return 'rememberToken';
}
}
namespace App;
use Butschster\Cycle\Repository;
class UserRepository extends Repository
{
/** @inheritDoc */
public function findByUsername(string $username): ?User
{
return $this->findOne(['username' => $username]);
}
}
use Cycle\ORM\ORMInterface;
use Butschster\Cycle\Facades\ORM;
use Butschster\Cycle\Facades\EntityManager;
$user = new User();
$repository = app(ORMInterface::class)->getRepository($user);
// or
$repository = ORM::getRepository($user);
// or
$repository = ORM::getRepository(User::class);
$repository->persist($user);
// or
EntityManager::persist($user);
use Butschster\Cycle\Facades\ORM;
use Butschster\Cycle\Facades\EntityManager;
$repository = ORM::getRepository(User::class);
$user = $repository->findByPK('5c9e177b0a975a6eeccf5960');
$user->setAuthPassword('secret');
$repository->persist($user);
// or
EntityManager::persist($user);
use Butschster\Cycle\Facades\ORM;
use Butschster\Cycle\Facades\EntityManager;
$repository = ORM::getRepository(User::class);
$user = $repository->findByPK('5c9e177b0a975a6eeccf5960');
$repository->delete($user);
// or
EntityManager::delete($user);