Last active
December 25, 2015 04:19
-
-
Save hrach/6916185 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace model; | |
use DateTime; | |
use Nextras\Orm\Entity\Entity; | |
/** | |
* @property Salary[] $salaries {1:m SalariesRepository $employee} | |
* @property Department[] $departments {m:n DepartmentsRepository $employee} | |
* @property DateTime $birthDate | |
* @property string $firstName | |
* @property string $lastName | |
* @property string $gender | |
* @property DateTime $hireDate | |
*/ | |
class Employee extends Entity | |
{ | |
protected $salaries; | |
protected $departments; | |
protected $birthDate; | |
protected $firstName; | |
protected $lastName; | |
protected $gender; | |
protected $hireDate; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace model; | |
use Nextras\Orm\Mapper\NetteMapper; | |
use Nextras\Orm\StorageReflection\UnderscoredDatabaseReflection; | |
class EmployeesMapper extends NetteMapper | |
{ | |
public function findOverview($limit) | |
{ | |
return $this->findAll()->applyLimit($limit); | |
} | |
protected function createStorageReflection() | |
{ | |
$reflection = new UnderscoredDatabaseReflection($this, $this->reflection, $this->connection); | |
$reflection->addMapping('id', 'emp_no'); | |
return $reflection; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$refProvider = new ReflectionProviderNette21($selectionFactory); | |
$model = new StaticModel; | |
$model->addRepository('employees', new model\EmployeesRepository(new model\EmployeesMapper($connection, $refProvider))); | |
$model->addRepository('salaries', new model\SalariesRepository(new model\SalariesMapper($connection, $refProvider))); | |
$model->addRepository('departments', new model\DepartmentsRepository(new model\DepartmentsMapper($connection, $refProvider))); | |
$employees = $model->employees->findOverview($limit); | |
foreach ($employees as $employee) { | |
echo "$employee->firstName $employee->lastName ($employee->id)\n"; | |
echo "Salaries:\n"; | |
foreach ($employee->salaries as $salary) { | |
echo $salary->salary, "\n"; | |
} | |
echo "Departments:\n"; | |
foreach ($employee->departments as $department) { | |
echo $department->name, "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment