我正在尝试通过标准PHP学习OO/Zend框架。我想尖叫和写一个mysql查询,而不是使用TableGateway方法。
我一直在学习教程,并且已经成功地打印出了一个表格和一些字段,尽管我已经这样做了,我完全不知道该如何将它与另一张表连接起来,并在那里打印出一些字段。
例如。
表字段客户标识符,公司联系地址,First_Name
这是我的customersController,我假设工作是在这里进行的
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;
}
}我在这里走得对吗?
发布于 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)
在某些功能中,您可以得到类似于(这是来自项目的)的内容:
$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等),可以使用以下内容:
$sql->select()
->from('table')
->columns(array(
'sorter' => new Expression('(IF ( table.`something` >= 'otherthing', 1, 0))'),
'some_count' => new Expression('(count(*))'),
)
)用SQL术语解释最后一行时,应该是: count(*)作为some_count
发布于 2014-01-10 09:20:15
这是我的控制器,基本上来自相册示例,但现在它将显示来自customer表的客户。
<?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)并在那里执行“查询”。
<?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中吗?谢谢你的帮助。
$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);https://stackoverflow.com/questions/21024047
复制相似问题