PHP code example of nattreid / orm
1. Go to this page and download the library: Download nattreid/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/ */
nattreid / orm example snippets
namespace App\Model;
/**
* @property-read ExampleRepository $example
*/
class Orm extends \Nextras\Orm\Model\Model {
}
class ExampleRepository extends \NAttreid\Orm\Repository {
public static function getEntityClassNames() {
return [Example::class];
}
}
class ExampleMapper extends \NAttreid\Orm\Mapper {
protected function createTable(\NAttreid\Orm\Structure\Table $table) {
$table->setDefaultDataFile(__DIR__.'/import.sql');
$table->addPrimaryKey('id')
->int()
->setAutoIncrement();
$table->addForeignKey('someId', SomeMapper::class);
$table->addForeignKey('parentId', $table)
->setDefault(NULL);
$table->addColumn('pa')
->varChar(20);
$table->addColumn('allowed')
->boolean()
->setDefault(1)
->setKey();
$table->addUnique('someId', 'parentId');
$table->addFulltext('pa');
$relationTable = $table->createRelationTable(OtherMapper::class);
$relationTable->addForeignKey('exampleId', $table);
$relationTable->addForeignKey('otherId', OtherMapper::class);
$relationTable->setPrimaryKey('exampleId', 'otherId');
// migrace
if (!$relationTable->exists) {
$table->migration[] = function (Row $row, Connection $connection) use ($relationTable) {
if (isset($row->oldColumnId)) {
$connection->query('INSERT INTO %table %values', $relationTable->name, [
'exampleId' => $row->id,
'otherId' => $row->oldColumnId
]);
}
};
}
$this->onCreateTable[] = function () {
$this->insert([
[
'id' => 1,
'someId' => 1,
'parentId' => 1,
'pa' => 'test',
// ...
]
]);
};
}
}
/**
* @property int $id {primary}
* @property Some $some {m:1 Some, oneSided=true}
* @property Example|NULL $parent {m:1 Example::$children}
* @property OneHasMany|Example[] $children {1:m Example::$parent, orderBy=id}
* @property string $pa
* @property boolean $allowed {default TRUE}
*/
class Example extends \Nextras\Orm\Entity\Entity {
}