Vederea inregistrarilor
Creati modulul Data:
si apoi efectuati toate configurarile necesare.
config/autoload/global.php
return [
'db'=>[
'driver'=>'Pdo',
'dsn'=>'mysql:dbname=flowers;host=localhost;charset=utf8',
'username'=>'root',
'password'=>''
]
];
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
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'];
}
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;
}
public function getMarime()
{
return $this->marime;
}
public function getPret()
{
return $this->pret;
}
}
FlowerTable.php
<?php
namespace DataModel;
use ZendDbTableGatewayTableGatewayInterface;
class FlowerTable
{
protected $tableGateway;
public function __construct(TableGatewayInterface $tableGateway)
{
$this->tableGateway=$tableGateway;
}
public function fetchAll()
{
return $this->tableGateway->select();
}
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';
}
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);
}
]
];
}
public function getControllerConfig()
{
return[
'factories'=>[
ControllerIndexController::class=>function($container)
{
return new ControllerIndexController(
$container->get(ModelFlowerTable::class)
);
}
]
];
}
}
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));
}
Data/config/module.config.php
Comentati linia:
'controllers' => [
'factories' => [
// ControllerIndexController::class => InvokableFactory::class,
],
],
view/data/index/index.phtml
<table>
<tr>
<th style="width:100px">Nume</th>
<th style="width:100px">Culoare</th>
<th style="width:100px">Marime</th>
<th style="width:100px">Pret</th>
<th style="width:100px" colspan="3" align="center">Actions</th>
</tr>
<?php foreach($flower as $var){ ?>
<tr>
<td><?php echo $var->GetNume();?></td>
<td><?php echo $var->GetCuloare();?></td>
<td><?php echo $var->GetMarime();?></td>
<td><?php echo $var->GetPret();?></td>
<td>
<a href="<?php echo $this->url('data',['action'=>'view','id'=>$var->getId()]); ?>">View</a>
<a href="<?php echo $this->url('data',['action'=>'edit','id'=>$var->getId()]); ?>">Edit</a>
<a href="<?php echo $this->url('data',['action'=>'delete','id'=>$var->getId()]); ?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<br/><br/>
<a href="/add">Add another record</a>

8. vederea inregistrarilor