doctrine-orm - ZF2 学说实体 findAll

标签 doctrine-orm zend-framework2

能够使用 Doctrine 可以加快很多事情的速度,但是对于我来说,必须在所有 Controller 中设置/使用实体管理器感觉有点笨拙。我希望将所有数据库逻辑放在 1 个特定模块中。也许我只是以错误的方式思考这个问题,有人可以指出我正确的方向。

目前我的实体运行良好,我可以使用以下命令很好地插入数据库

namespace Manage\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;


class ViewController extends AbstractActionController {
    public function somethingAction(){
        $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        $user = new \Manage\Entity\User();
        $user->setname('foo');
        $user->settitle('bar');        
        $objectManager->persist($user);
        $objectManager->flush();
    }
}

但是,每当我想从数据库中选择某些内容时,我必须确保添加

 use Doctrine\ORM\EntityManager;

然后是以下 Controller 功能列表...

/**
 * @var EntityManager
 */
protected $entityManager;

/**
 * Sets the EntityManager
 *
 * @param EntityManager $em
 * @access protected
 * @return PostController
 */
protected function setEntityManager(EntityManager $em) {
    $this->entityManager = $em;
    return $this;
}

/**
 * Returns the EntityManager
 *
 * Fetches the EntityManager from ServiceLocator if it has not been initiated
 * and then returns it
 *
 * @access protected
 * @return EntityManager
 */
protected function getEntityManager() {
    if (null === $this->entityManager) {
        $this->setEntityManager($this->getServiceLocator()->get('Doctrine\ORM\EntityManager'));
    }
    return $this->entityManager;
}

添加完所有内容后,我现在可以在 getsomethingAction 中进行查询,如下所示...

public function getsomethingAction() {
    $repository = $this->getEntityManager()->getRepository('Manage\Entity\User');
    $list = $repository->findAll();
    var_dump($list);
    return new ViewModel();
}

对我来说,这感觉非常笨重...我可以进行插入而不需要所有额外的功能,但我不能进行选择?是否可以扩展实体类以获得通过调用 $repository = $this->getEntityManager()->getRepository('Manage\Entity\User'); 提供的 find/findAll 等函数;直接在实体内部?

我的意思是我更愿意能够直接在实体上运行查找,就像我设置数据时一样......如下所示:

public function getsomethingAction(){
    $list = new \Manage\Entity\User();
    $l = $list->findAll();
    var_dump($l);
    return new ViewModel();
}

最佳答案

好吧,到目前为止我的主要目标是将复杂的逻辑从 Controller 中移出到可重用的模型中。因此,通过这个示例答案,我正在创建一个接口(interface),其中包含复杂的逻辑,但它也允许我仍然使用 Controller 中的模型从数据库中获取数据...这是模型...

namespace Manage\Model;

use Doctrine\ORM\EntityManager;

class ApiInterface {

    /**
     * @var EntityManager
     */
    protected $entityManager;
    protected $sl;

    /**
     * Sets the EntityManager
     *
     * @param EntityManager $em
     * @access protected
     * @return PostController
     */
    protected function setEntityManager(EntityManager $em) {
        $this->entityManager = $em;
        return $this;
    }

    /**
     * Returns the EntityManager
     *
     * Fetches the EntityManager from ServiceLocator if it has not been initiated
     * and then returns it
     *
     * @access protected
     * @return EntityManager
     */
    protected function getEntityManager() {
        if (null === $this->entityManager) {
            $this->setEntityManager($this->sl->get('Doctrine\ORM\EntityManager'));
        }
        return $this->entityManager;
    }

    public function __construct($ServiceLocator) {
        $this->sl = $ServiceLocator;
    }

    public function get() {
        $repository = $this->getEntityManager()->getRepository('Manage\Entity\ApiList');
        return $repository;
    }

    public function set() {
        return new \Manage\Entity\ApiList();
    }

    public function save($data) {
        $objectManager = $this->sl->get('Doctrine\ORM\EntityManager');
        $objectManager->persist($data);
        $objectManager->flush();
    }

    public function doComplexLogic($foo,$bar){
        // Can now use both set() and get() to inspect/modify/add data
    }

}

所以现在在我的 Controller 中我可以做一些从表中获取一些基本数据的事情,例如:

public function getapiAction() {
    $api = new \Manage\Model\ApiInterface($this->getServiceLocator());
    var_dump($api->get()->findAll());
    return new ViewModel();
}

要从 Controller 快速设置数据,我可以这样做:

public function setapiAction() {
    $apiInterface = new \Manage\Model\ApiInterface($this->getServiceLocator());
    $api= $apiInterface->set();
    $user->setfoo('blah');
    $user->setbar('moo');
    $apiInterface->save($api);
    return new ViewModel();
}

它还允许我通过像这样消除 Controller 的复杂性来从 Controller 运行复杂的逻辑......

public function complexAction(){
    $foo = $this->params()->fromQuery();
    $bar = $this->params()->fromPost();
    $apiInterface = new \Manage\Model\ApiInterface($this->getServiceLocator());
    $apiInterface->doComplexLogic($foo, $bar);
}

请在评论中告诉我这个答案是否是正确的处理方法,我意识到它非常简单且通用,但我想保持这种方式,以便其他人可以理解什么/为什么以及这是否是一个好方法/不是等。

关于doctrine-orm - ZF2 学说实体 findAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15395497/

相关文章:

zend-framework2 - ZF2 自定义查询

php - 在加入 zend 框架时应用 AND 条件

symfony - Doctrine 2 如何防止丢失外键

json - Doctrine查询postgres json(包含)json_array

php - Doctrine2 一对多/多对一关系

php - DOMPDFModule 无法正确呈现字段

php - 为什么我在远程服务器上运行 Zend Framework 2 应用程序时收到错误 : table doesn't exist,?

php - Doctrine 迁移是否可用于生产应用程序?

mysql - 计算满足条件的记录

zend-framework2 - BjyAuthorize 设置文件连接数据库表