Php 使用ResultSet Zend Framework 2的Where条件执行查询?
我有一个标签“cms”,其中包括3个单独的父类别(类型)的cms页面。当我调用fetchAll()函数时,该函数将获取所有数据,但我只希望获取父类别(类型)=“1”表示我希望在fetchAll()函数中传递where条件和orderby条件的数据 我的Cms.php页面是:Php 使用ResultSet Zend Framework 2的Where条件执行查询?,php,mysql,zend-framework,zend-db-table,Php,Mysql,Zend Framework,Zend Db Table,我有一个标签“cms”,其中包括3个单独的父类别(类型)的cms页面。当我调用fetchAll()函数时,该函数将获取所有数据,但我只希望获取父类别(类型)=“1”表示我希望在fetchAll()函数中传递where条件和orderby条件的数据 我的Cms.php页面是: <?php namespace Front\Model; use Zend\Db\TableGateway\AbstractTableGateway; class Cms extend
<?php
namespace Front\Model;
use Zend\Db\TableGateway\AbstractTableGateway;
class Cms extends AbstractTableGateway {
public function __construct($adapter) {
$this->table = 'cms';
$this->adapter = $adapter;
}
public function fetchAll($id) {
return $this->select();
}
public function getCmsContent($id){
$id = (int) $id;
$rowset = $this->select(array('id'=>$id));
if (!$row = $rowset->current()){
throw new \Exception ('Row not found');
}
return $row;
}
}
我的模型是:
namespace Front;
class Module
{
public function getAutoloaderConfig()
{
return array('Zend\Loader\StandardAutoloader' =>
array('namespaces' =>
array(__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(
'Front\Model\Cms' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new \Front\Model\Cms($dbAdapter);
return $table;
},
),
);
}
}
?>
我的视图:index.php页面是:
<?php print_R($cms_data); ?>
你的问题不清楚。您是否在询问如何将Where子句添加到查询中?如果是,那么就是这样:
use Zend\Db\Sql\Predicate\Operator;
public function fetchAll($id) {
return $this->select()
->where(new Operator('type', Operator::OPERATOR_EQUAL_TO, $id);
}
谢谢你的重播,但是这个函数帮助我解决了我的问题。公共函数fetchData($vname){$this->table='videos';$rowset=$this->select(数组('page_type'=>$vname));返回$rowset;}
use Zend\Db\Sql\Predicate\Operator;
public function fetchAll($id) {
return $this->select()
->where(new Operator('type', Operator::OPERATOR_EQUAL_TO, $id);
}