5. Pentru a gestiona baze de date va trebui sa cream
un model.
Pentru aceasta, vom crea directorul:
Module/database/src/Model
si in acesta fisierele Flower.php si FlowerTable.php
6. Flower.php
<?php
namespace DataModel;
class Flower
{
protected $id;
protected $nume;
protected $culoare;
protected $marime;
protected $pret;
public function exchangeArray(array $data)
{
$this->id=$data['id'];
$this->nume=$data['nume'];
$this->culoare=$data['culoare'];
$this->marime=$data['marime'];
$this->pret=$data['pret'];
}
7. public function getArrayCopy()
{
return [
'id'=>$this->id,
'nume'=>$this->nume,
'culoare'=>$this->culoare,
'marime'=>$this->marime,
'pret'=>$this->pret
];
}
public function getId()
{
return $this->id;
}
public function getNume()
{
return $this->nume;
}
public function getCuloare()
{
return $this->culoare;
}
10. module/Database/src/Module.php
<?php
namespace Data;
use ZendDbAdapterAdapterInterface;
use ZendDbTableGatewayTableGateway;
use ZendDbResultSetResultSet;
use ZendModuleManagerFeatureConfigProviderInterface;
class Module implements ConfigProviderInterface
{
const VERSION = '3.0.3-dev';
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
11. public function getServiceConfig()
{
return [
'factories'=>[
ModelFlowerTable::class=>function($container){
$tableGateway=$container-
>get(ModelFlowerTableGateway::class);
return new ModelFlowerTable($tableGateway);
},
ModelFlowerTableGateway::class=>function($container){
$adapter=$container->get(AdapterInterface::class);
$resultSetPrototype=new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new
ModelFlower);
return new
TableGateway('flower',$adapter,null,$resultSetPrototype);
}
]
];
}
13. IndexController
namespace DataController;
use ZendMvcControllerAbstractActionController;
use ZendViewModelViewModel;
class IndexController extends AbstractActionController
{
protected $table;
public function __construct($table)
{
$this->table=$table;
}
public function indexAction()
{
$flower=$this->table->fetchAll();
return new ViewModel(array('flower'=>$flower));
}