1. Go to this page and download the library: Download nixphp/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/ */
class Product extends AbstractModel
{
protected ?int $id = null;
protected string $name = '';
protected ?Category $category = null;
protected array $tags = [];
public function getTags(): array
{
if ($this->tags === []) {
$this->tags = (new TagRepository())->findByPivot(Product::class, $this->id);
}
return $this->tags;
}
public function getCategory(): ?Category
{
if ($this->category === null && $this->category_id) {
$this->category = (new CategoryRepository())->findOneBy('id', $this->category_id);
}
return $this->category;
}
}
$category = (new CategoryRepository())->findOrCreateByName('Books');
$tagA = (new TagRepository())->findOrCreateByName('Bestseller');
$tagB = (new TagRepository())->findOrCreateByName('Limited');
$product = new Product();
$product->name = 'NixPHP for Beginners';
$product->addCategory($category);
$product->addTag($tagA);
$product->addTag($tagB);
em()->save($product);
$product = (new ProductRepository())->findOneBy('id', 1);
echo $product->name;
print_r($product->getCategory());
print_r($product->getTags());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.