【问题标题】:ZF - Doctrine2 Cannot Retrieve RepositoryZF - Doctrine2 无法检索存储库
【发布时间】:2017-07-21 00:23:06
【问题描述】:

这有什么问题吗?我是 zend 框架的新手.......................... ..................................................... ....................

这是我的文件夹结构

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);
    }


}

用户控制器.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]);

    }
}

用户.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的源代码放在这里

标签: php zend-framework doctrine-orm zend-framework2 zend-framework3


【解决方案1】:

您正在混合从数据库中检索数据的两种不同方法。

您可以使用 TableGateway 中的 Zend 方法,也可以使用 Doctrine (EntityManager/Repositories)。

因此,您必须在要在控制器中使用的方法之一之间做出选择。

所以如果你想坚持 Doctrine,你可以看看 Ocramius 的以下幻灯片:http://ocramius.github.io/presentations/doctrine-orm-and-zend-framework-2/#/59

所以你几乎必须更新你的 User 模型:

namespace User\Model;

use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity()
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    public $id;

    /** @ORM\Column(type="string") */
    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,
        ];
    }
}

更新您的用户模块的以下文件module.config.php,并将以下内容添加到您的配置中:

array(
'doctrine' => array(
  'driver' => array(
    'application_entities' => array(
      'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
      'cache' => 'array',
      'paths' => array(__DIR__ . '/../src/User/Model')
    ),
    'orm_default' => array(
      'drivers' => array(
        'User\model' => 'application_entities'
      )
    ),
  )
),

请注意,这需要 Doctrine-ORM 模块。见:https://github.com/doctrine/DoctrineORMModule

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 2012-07-08
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    相关资源
    最近更新 更多