Tak,
"pracuji" na Nette+dibi a dlouho jsem si nerozumněl s písmenkem "M" (Model) ze slavné architektury "MVC", nyní si myslím že jsem konečně našel správnou cestu, jen se potřebuji dostal do cíle. Nyní se to snažím implementovat na svojí srandu "serialspot", takže názvy tříd jsou dosti "osobní". Snad se mi lehounká ukázka střev webu nevymstí :D
Jedná se o tři základní třídy: Entity, Repository, Mapper.
V ukázkách se objevuje namespace Model\Quote, ten je dán adresáři, ve kterých jsou uložené soubory.
1. Mapper
Mapper se mi stará o veškerou práci s databází, nikde jinde se název tabulky či jiný sql dotaz nevyskytuje.PHP kód:<?php
class QuoteDbMapper extends Object{
public function save(Quote $entity){
$data = $this->entityToData($entity);
unset ($data['quote']);
if($entity->id == NULL){//INSERT
\dibi::query('INSERT INTO [quotes]', $data);
$returnId = \dibi::insertId();
}else{//UPDATE
\dibi::query('UPDATE [quotes] SET', $data,' WHERE quote = %i', $entity->id);
$returnId = $entity->id;
}
return $this->find($returnId);
}
public function find($entityId){
$data = \dibi::fetch('SELECT * FROM [quotes] WHERE quote = %i', $entityId);
return $this->load($data);
}
public function findBy($where = NULL, $by = NULL, $limit = NULL, $offset = NULL){
$rows = \dibi::fetchAll('
SELECT * FROM [quotes]
%if', isset($where),'WHERE %and', $where,' %end
%if', isset($by),'ORDER BY %by ', $by,' %end
%if', isset($limit),' %lmt', $limit,' %end
%if', isset($offset),' %ofs', $offset,'%end
');
if(!$rows){
return FALSE;
}
$arr = array();
foreach ($rows as $row){
$arr[] = $this->load($row);
}
return $arr;
}
public function findOneBy($where){
$data = \dibi::fetch('SELECT * FROM [quotes] %if', isset($where),' WHERE %and %end', $where);
return $this->load($data);
}
private function entityToData(Quote $entity){
return array(
'quote' => $entity->id,
'series' => $entity->getSeries(FALSE),
'user' => $entity->getUser(FALSE),
'content' => $entity->content
);
}
private function load($data){
$entity = new Quote();
foreach ($data as $k => $v){
$entity->$k = $v;
}
return $entity;
}
}
2. Entity
Entita je "otiskem" tabulky, ale na některých sloupcích si on-demand vyžádá náležící řádky (Doufám že je to srozumitelné).PHP kód:<?php
class Quote extends Entity{
private $series;
private $user;
public $content;
public function setQuote($quoteId){
$this->setId($quoteId);
}
public function setSeries($seriesId){
$this->series = $seriesId;
}
public function getSeries($full = TRUE){
if($full == FALSE){
return ($this->series instanceof Series)? $this->series->id : $this->series;
}elseif(!$this->series instanceof Series){
$seriesRp = new SeriesRepository();
$this->series = $seriesRp->singleSeries($this->series);
}
return $this->series;
}
public function setUser($userId){
$this->user = $userId;
}
public function getUser($full = TRUE){
if($full == FALSE){
return ($this->user instanceof User)? $this->user->id : $this->user;
}elseif($this->user instanceof User){
$userRp = new UserRepository();
$this->user = $userRp->singleUser($this->user);
}
return $this->user;
}
}
3.Repository
Repozitář si budu následně upravovat (respektive přidávat metody) podle potřeb dalších částí systému.PHP kód:<?php
class QuoteRepository extends Object{
public $mapper;
public function __construct(){
$this->mapper = new QuoteDbMapper();
}
public function randomSeriesQuote($seriesId){
$this->mapper->findBy(array('series%i' => $seriesId), array('%sql' => 'RAND()'), 1);
}
}
Třida entity, od které dědí všechny entity:
Tak co na to říkáte?Vidíte někde nějajé zádrhely? Měl jsem pár otázek, ale ty jsem během psaní zapomněl, snad si později vzpomenu.PHP kód:<?php
namespace Model;
use Nette\Object;
abstract class Entity extends Object{
/** @var integer */
private $id;
/**
*
* @param integer $id
*/
public function setId($id){
$this->id = $id;
}
/**
*
* @return integer
*/
public function getId(){
return $this->id;
}
}
Čerpal jsem z těchto zdrojů:
http://wiki.nette.org/cs/cookbook/mo...ository-mapper
http://www.phpguru.cz/clanky/pet-vrstev-modelu


