首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZF2 TableGateway连接

ZF2 TableGateway连接
EN

Stack Overflow用户
提问于 2014-01-09 15:15:50
回答 2查看 6.6K关注 0票数 0

我正在尝试通过标准PHP学习OO/Zend框架。我想尖叫和写一个mysql查询,而不是使用TableGateway方法。

我一直在学习教程,并且已经成功地打印出了一个表格和一些字段,尽管我已经这样做了,我完全不知道该如何将它与另一张表连接起来,并在那里打印出一些字段。

例如。

表字段客户标识符,公司联系地址,First_Name

这是我的customersController,我假设工作是在这里进行的

代码语言:javascript
复制
      namespace Customers\Controller;

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


class CustomersController extends AbstractActionController
 {

 protected $customersTable;

 public function indexAction()
 {
     return new ViewModel(array('customer' => $this->getCustomersTable()->select()));
     //return new ViewModel(array('customers' => $this->fetchJoin()->select()));
 }

 public function addAction()
 {
 }

 public function editAction()
 {
 }

 public function deleteAction()
 {
 }

 public function getCustomersTable()
 {
        if (!$this->customersTable) {
        $this->customersTable = new TableGateway (
        'customer', //table name
         $this->getServiceLocator()->get('Zend\DB\Adapter\Adapter')
         );
     }
     return $this->customersTable;


    }

   }

我在这里走得对吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-09 18:04:01

如果您需要让joins读取关于Zend\Db\Sql和Zend\Db\Select,您可以在这里阅读

http://framework.zend.com/manual/2.0/en/modules/zend.db.sql.html

一个例子是:

在您的模型中(扩展TableGateway或AbstractTableGateway)

在某些功能中,您可以得到类似于(这是来自项目的)的内容:

代码语言:javascript
复制
$sql = new \Zend\Db\Sql\Sql($this->getAdapter());

$select = $sql->select()
    ->from('event_related_events')
    ->columns(array())
    ->join('event_invitees', 'event_invitees.event_id = 
       event_related_events.related_event_id')
    ->where(array('event_related_events.event_id' => $eventId));

$selectString = $sql->getSqlStringForSqlObject($select); 
$results      = $this->getAdapter()->query($selectString, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);

然后,你可以循环的结果,并做你需要做的。

看一看更强大的ORM,比如Doctrine或Propel,也许也会有所帮助,但对于一个小型/业余爱好项目来说,这可能是一种过分的浪费。

编辑:回答评论中提出的问题

对于直接使用表达式(if、case等),可以使用以下内容:

代码语言:javascript
复制
 $sql->select()
    ->from('table')
    ->columns(array(
        'sorter' => new Expression('(IF ( table.`something` >= 'otherthing',  1,  0))'),
    'some_count' => new Expression('(count(*))'),
    )
)

用SQL术语解释最后一行时,应该是: count(*)作为some_count

票数 3
EN

Stack Overflow用户

发布于 2014-01-10 09:20:15

这是我的控制器,基本上来自相册示例,但现在它将显示来自customer表的客户。

代码语言:javascript
复制
<?php

    namespace Customers\Controller;

     use Zend\Mvc\Controller\AbstractActionController;
     use Zend\View\Model\ViewModel;
     use Customers\Model\Customers;          
     use Customers\Form\CustomersForm; 

     class CustomersController extends AbstractActionController
     {

      protected $customersTable;

         public function indexAction()
         {
             return new ViewModel(array(
                 'customer' => $this->getCustomersTable()->fetchAll(),
             ));
         }

        public function addAction()
        {
        }

        public function editAction()
         {
         }

        public function deleteAction()
        {
        }

         public function getCustomersTable()
         {
             if (!$this->customersTable) {
                 $sm = $this->getServiceLocator();
                 $this->customersTable = $sm->get('Customers\Model\CustomersTable');
             }
             return $this->customersTable;
         }



     }


     ?>

indexAction调用getCustomersTable方法,该方法转到模型(CustomersTable)并在那里执行“查询”。

代码语言:javascript
复制
<?php

namespace Customers\Model;

 use Zend\Db\TableGateway\TableGateway;

 class CustomersTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
         $this->tableGateway = $tableGateway;
     }

     public function fetchAll()
     {
         $resultSet = $this->tableGateway->select();
         return $resultSet;
     }

     public function getCustomers($id)
     {

     }

     public function saveCustomers(customers $customers)
     {

     }


     public function deleteCustomers($id)
     {
     }
 }

 ?>

因此,从您的示例中,我应该尝试将其实现到模型中的fetchAll中吗?谢谢你的帮助。

代码语言:javascript
复制
$sql = new \Zend\Db\Sql\Sql($this->getAdapter());

        $select = $sql->select()
    ->from('customer')
    ->columns(array())
    ->join('contact', 'contact.Idx = customer.Idx')
    ->where(array('contact.Idx' => $eventId));

$selectString = $sql->getSqlStringForSqlObject($select); 
$results      = $this->getAdapter()->query($selectString, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21024047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档