【发布时间】:2015-01-02 04:04:47
【问题描述】:
我在我的 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。我从应用程序配置中删除了 Doctrine ORM。我的问题可能是由项目中残留的 Doctrine 代码引起的吗?如果是这样,需要删除哪些文件和文件夹才能从应用程序中完全删除 Doctrine 2?
【问题讨论】:
-
“不渲染”是什么意思?你得到一个空白页吗? 404 页面?
标签: php doctrine-orm zend-framework2