我在ZF2应用程序中创建了一个客户端模块,并按照ZF2 Read The Docs中的建议使用ServiceManager。我的模型类如下:
<?php
namespace Client'Model;
class Client
{
public $id;
public $familyname;
public $businessname;
public function exchangeArray($data)
{
$this->id =(!empty(['id'])) ? $data['id'] : null;
$this->familyname=(!empty(['familyname'])) ? $data['familyname'] : null;
$this->businessname(!empty(['businessname'])) ? $data['businessname'] : null;
}
}
我的ClientTable.php
包含:
namespace Client'Model;
use Zend'Db'TableGateway'TableGateway;
class ClientTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getClient($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new 'Exception("Could not find row $id");
}
return $row;
}
public function saveClient(Client $client)
{
$data = array(
'familyname' => $client->familyname,
'businessname' => $client->businessname,
);
$id = (int) $client(id);
if($id==0){
$this->tableGateway->insert($data);
} else {
if($this->getClient($id)){
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new 'Exception('Client id does not exist');
}
}
}
}
我的ClientController.php
如下:
namespace Client'Controller;
use Zend'Mvc'Controller'AbstractActionController;
use Zend'View'Helper'ViewModel;
class ClientController extends AbstractActionController
{
protected $clientTable;
public function getClientTable()
{
if(!$this->clientTable){
$sm = $this->getServiceLocator();
$this->clientTable = $sm->get('Client'Model'ClientTable');
}
return $this->clientTable;
}
public function indexAction()
{
return new ViewModel(array(
'client' => $this->clientTable->fetchAll(),
));
}
public function addAction()
{
return array();
}
public function editAction()
{
return array();
}
}
最后但同样重要的是,视图类如下:
<?php
$title = 'Client List';
$this->headtitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p>
<a href="<?php echo $this->url('client', array('action'=>'add'));?>">Add New Client</a>
</p>
<table class="table">
<tr>
<th>Family Name</th>
<th>Business Name</th>
<th> </th>
</tr>
<?php foreach ($client as $client) : ?>
<tr>
<td><?php echo $this->escapehtml($client->familyname);?></td>
<td><?php echo $this->escapehtml($client->busienssname);?></td>
<td>
<a href="<?php echo $this->url('client', array('action'=>'edit',
'id'=>$client->$id));?>">Edit</a>
</td>
</tr>
<?php endforeach; ?>
</table>
我还将客户端模块添加到了application.config.php
中。有人能提出一些方法来解决我的问题吗?在客户端索引视图没有呈现的情况下?
更新:注意layout.phtml
的内容正在渲染。但是,我的/module/client/view/client/client
中的index.phtml
包含用于添加或编辑未呈现的客户端的标记。
更新2:我没有提到我使用了Skeleton应用程序作为这个应用程序的基础。不确定是否有导致我问题的配置更改?
更新3:我启动这个应用程序的目的是使用Doctrine2 ORM。然而,已经决定只使用ZendDbAdpater。我从应用程序配置中删除了条令ORM。我的问题可能是由项目中残留的条令代码引起的吗?如果是,需要删除哪些文件和文件夹才能从应用程序中完全删除条令2?
<?php foreach ($client as $client) : ?>
您再次使用相同的变量。
您的视图脚本中可能有错误。您可以将它们封装在try块中,然后查看错误。
<?php
try {
$title = 'Client List';
$this->headtitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p>
<a href="<?php echo $this->url('client', array('action'=>'add'));?>">Add New Client</a>
</p>
<table class="table">
<tr>
<th>Family Name</th>
<th>Business Name</th>
<th> </th>
</tr>
<?php foreach ($client as $client) : ?>
<tr>
<td><?php echo $this->escapehtml($client->familyname);?></td>
<td><?php echo $this->escapehtml($client->busienssname);?></td>
<td>
<a href="<?php echo $this->url('client', array('action'=>'edit',
'id'=>$client->$id));?>">Edit</a>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php } catch ('Exception $e) {var_dump($e);} ?>