這是什麼問題呢?我是zend框架中的新成員........................................... .................................................. ..............................ZF - Doctrine2無法檢索知識庫
這是我的文件夾結構
UserControllerFactory.php
<?php
namespace Admin\Controller\Factory;
use Admin\Controller\UserController;
use User\Entity\User;
use Admin\Form\UserForm;
use User\Model\UserTable;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Entity;
use Interop\Container\ContainerInterface;
class UserControllerFactory
{
public function __invoke(ContainerInterface $container)
{
/** @var EntityManager $entityManager */
$entityManager = $container->get(EntityManager::class);
$repository = $entityManager->getRepository(User::class);
$userForm = $container->get(UserForm::class);
return new UserController($entityManager, $repository, $userForm);
}
}
UserController.php
<?php
namespace Admin\Controller;
//use User\Entity\User;
use Admin\Form\UserForm;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Zend\Hydrator\ClassMethods;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Http\Request;
use Zend\View\Model\ViewModel;
class UserController extends AbstractActionController
{
/**
* @var EntityRepository
*/
private $repository;
/**
* @var EntityManager
*/
private $entityManager;
private $form;
public function __constructor(EntityManager $entityManager, EntityRepository $repository, UserForm $form){
$this->form = $form;
$this->repository = $repository;
$this->entityManager = $entityManager;
}
public function indexAction()
{
return new ViewModel([
'users' => $this->repository->fetchAll()
]);
}
public function addAction()
{
$form = $this->form;
$form->get('submit')->setValue('Adicionar');
$request = $this->getRequest();
if (!$request->isPost()) {
return ['form' => $form];
}
$form->setData($request->getPost());
if (!$form->isValid()) {
return ['form' => $form];
}
$post = $form->getData();
$this->entityManager->persist($post);
$this->entityManager->flush();
return $this->redirect()->toRoute('admin/user');
}
public function editAction()
{
$id = (int)$this->params()->fromRoute('id', 0);
if (!$id || !($post = $this->repository->find($id))) {
return $this->redirect()->toRoute('admin/user');
}
$form = $this->form;
$form->bind($post);
$form->get('submit')->setAttribute('value', 'Edit Post');
$request = $this->getRequest();
if (!$request->isPost()) {
return [
'id' => $id,
'form' => $form
];
}
$form->setData($request->getPost());
if (!$form->isValid()) {
return [
'id' => $id,
'form' => $form
];
}
$this->entityManager->flush();
return $this->redirect()->toRoute('admin/user');
}
public function deleteAction()
{
$id = (int)$this->params()->fromRoute('id', 0);
if (!$id || !($post = $this->repository->find($id))) {
return $this->redirect()->toRoute('admin/user');
}
$this->entityManager->remove($post);
$this->entityManager->flush();
return $this->redirect()->toRoute('admin/user');
}
}
UserTable.php
<?php
/**
* Created by PhpStorm.
* User: jho
* Date: 24/06/2017
* Time: 18:55
*/
namespace User\Model\Factory;
use Zend\Db\Exception\RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;
class UserTable
{
private $tableGateway;
public function find($id)
{
$id = (int)$id;
$rowset = $this->tableGateway->select(['id' => $id]);
$row = $rowset->current();
if (!row) {
throw new RuntimeException(sprintf(
'Could not retrieve the row %d', $id
));
}
return $row;
}
public function fetchAll(){
return $this->tableGateway->select();
}
public function save(User $user){
$data = [
'username'=>$user->username,
'fullname'=>$user->fullname,
'password'=>$user->password,
];
$id = (int) $user->id;
if((int)$user->id === 0){
$this->tableGateway->insert($data);
return;
}
if(!$this->find($id)){
throw new RuntimeException(sprintf(
'Could not retrieve the row %d', $id
));
}
$this->tableGateway->update($data, ['id'=>$id]);
}
}
user.php的
<?php
namespace User\Model;
class User
{
public $id;
public $fullname;
public function exchangeArray(array $data){
$this->id = (!empty($data['id'])) ? $data['id']: null;
$this->title = (!empty($data['fullname'])) ? $data['fullname']: null;
}
public function getArrayCopy(){
return[
'id'=>$this->id,
'fullname'=>$this->fullname,
];
}
}
請同時在此處放入User \ Entity \ User的源代碼 –