ZF2 TableGateway 连接 [英] ZF2 TableGateway join

查看:32
本文介绍了ZF2 TableGateway 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am in the process of trying to learn OO/Zend Framework over standard PHP.. I want to scream and write a mysql query instead of using the TableGateway method.

我一直在学习教程并成功打印出一个表格和一些字段,尽管按照我这样做的方式,我完全不知道如何将其与另一个表格连接并打印出一些字段

I have been following tutorials and have successfully printed out a table and some fields, although with the way I have gone about doing this, I am totally lost in how I should make this a join with another table and print out some fields there.

例如.

表格字段客户 IDx,公司联系 IDx,名字

Table Fields customer Idx, Company contact Idx, First_Name

这是我的customersController,我认为工作是在那里进行的

This is my customersController where I assume the work is carried out

      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;


    }

   }

我在这里走对了吗?

推荐答案

如果您需要进行连接,请阅读 Zend\Db\Sql 和 Zend\Db\Select你可以在这里阅读

If you need to make joins read about Zend\Db\Sql and Zend\Db\Select which you can read about here

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

一个例子是:

在您的模型中(扩展 TableGateway 或 AbstractTableGateway)
在某些功能中,您可以拥有类似的东西(这是来自项目):

In your model(that extends the TableGateway or the AbstractTableGateway)
in Some function you can have something like(this is from a project) :

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

然后您可以遍历结果并执行您需要的操作.

Then you can loop over the results and do what you need to.

看看像 Doctrine 或 Propel 这样更强大的 ORM 也可能有所帮助,但对于小型/业余项目来说可能有点矫枉过正.

Taking a look at more powerful ORM like Doctrine or Propel may also help, but may be an overkill for a small/hobby project.

回答评论中提出的问题

对于直接使用表达式(if, case etc),你可以使用类似的东西:

For Using expression(if, case etc) directly you can use something like :

 $sql->select()
    ->from('table')
    ->columns(array(
        'sorter' => new Expression('(IF ( table.`something` >= 'otherthing',  1,  0))'),
    'some_count' => new Expression('(count(*))'),
    )
)

用 SQL 术语解释最后一行,它将是:count(*) AS some_count

Explaining the last line in SQL terms, it would be: count(*) AS some_count

这篇关于ZF2 TableGateway 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆