Php Zend框架获取单行的当前方法
我是zend framework的新手,在这个简单的函数中,我希望获得一个“post”,然后在相关表中查找所有注释Php Zend框架获取单行的当前方法,php,zend-framework,zend-db,zend-db-table,Php,Zend Framework,Zend Db,Zend Db Table,我是zend framework的新手,在这个简单的函数中,我希望获得一个“post”,然后在相关表中查找所有注释 public function getPost($idPost) { $db= Zend_Registry::get('db'); $select=$db->select() ->from($this->_name, '*') ->where("idPost= ".$db->quote
public function getPost($idPost)
{
$db= Zend_Registry::get('db');
$select=$db->select()
->from($this->_name, '*')
->where("idPost= ".$db->quote($idPost, 'INTEGER'));
$stmt=$select->query();
$rowset=$stmt->fetchAll();
$post=$rowset->current();
//ora devo aggiungerci i commenti che questo post ha ricevuto
$comm=$post->findDependentRowset('commenti');
$ris=array($post, $comm);
return $ris;
}
在我的索引控制器中,我只调用此函数,但我得到以下错误:
Call to a member function current() on a non-object in C:\xampp\htdocs\...
错误在哪里?要获得行集和相关行集,必须使用
Zend\u Db\u Table
。
您只能将Zend\u Db\u适配器
与Zend\u Db\u选择一起使用
阅读。
因此,您必须定义一个从Zend\u Db\u Table\u Abstract
扩展而来的类
例如:
class Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bugs';
protected $_primary = 'bug_id';
}
要获取Zend\u Db\u Table\u行集
对象,请使用:
$bugs = new Bugs();
$rowset = $bugs->fetchAll("bug_status = 'NEW'");
要查找依赖行集,必须在表类中定义关系。查看如何定义关系。要获得行集和相关行集,必须使用
Zend\u Db\u Table
。
您只能将Zend\u Db\u适配器
与Zend\u Db\u选择一起使用
阅读。
因此,您必须定义一个从Zend\u Db\u Table\u Abstract
扩展而来的类
例如:
class Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bugs';
protected $_primary = 'bug_id';
}
要获取Zend\u Db\u Table\u行集
对象,请使用:
$bugs = new Bugs();
$rowset = $bugs->fetchAll("bug_status = 'NEW'");
要查找依赖行集,必须在表类中定义关系。看看如何定义关系。我认为您对如何使用Zend_Db有一些误解 1.您没有使用ORM,只是使用PDO包装器 这意味着,您的查询不会返回Zend行集和行,因此您不能使用可以在这些行上使用的方法 2.默认的获取模式 Zend_Db_语句fetchAll()方法的默认获取模式为array,如果希望它返回对象(stdClass),请在获取数据之前更改获取模式:
$stmt->setFetchMode(Zend_Db::FETCH_OBJ);
3.实际需要一行时使用fetchAll()
如果你只想要一排,那就不要拿整张桌子!对于Zend_Db_语句,请使用以下示例:
$row = $stmt->fetch();
或
。。。同样,这不是zend row对象,只是一个stdClass实例,但您可以执行以下操作:
$rowObj->some_field;
在上面
另一方面,如果这是您的Post模型中的一种方法,它应该类似于:
public function getPost($idPost)
{
return $this->getRow($idPost);
}
这将返回帖子,然后,如果您正确设置了表关系,您还可以查询相关数据或单独获取带有该id的所有注释。我认为您对如何使用Zend_Db有一些误解 1.您没有使用ORM,只是使用PDO包装器 这意味着,您的查询不会返回Zend行集和行,因此您不能使用可以在这些行上使用的方法 2.默认的获取模式 Zend_Db_语句fetchAll()方法的默认获取模式为array,如果希望它返回对象(stdClass),请在获取数据之前更改获取模式:
$stmt->setFetchMode(Zend_Db::FETCH_OBJ);
3.实际需要一行时使用fetchAll()
如果你只想要一排,那就不要拿整张桌子!对于Zend_Db_语句,请使用以下示例:
$row = $stmt->fetch();
或
。。。同样,这不是zend row对象,只是一个stdClass实例,但您可以执行以下操作:
$rowObj->some_field;
在上面
另一方面,如果这是您的Post模型中的一种方法,它应该类似于:
public function getPost($idPost)
{
return $this->getRow($idPost);
}
这将返回post,然后,如果您正确设置了表关系,您还可以查询相关数据或单独获取具有该id的所有注释。问题是,除非您如前所述,否则无法使用相关行集或父行集 要使当前功能正常工作,最好使用两个功能,并保持简单:
public function getPost($idPost)
{
$db= new Zend_Db_Table($this->_name);
$select=$db->select()
->where("idPost= ?", $idPost);
/*Fetch just the row you want, or use fetchAll() if you need to match return types*/
$row = $db->fetchRow($select);
return $row;
}
public function getComments($table='comments', $id) {
$db = new Zend_Db_table($table);
$select = $db->select()->where('post_id = ?', $id)->order('date ASC');
$rowset = $db->fetchAll($select);
return $rowset/* or you could return an array ->$rowset->toArray() */
}
Zend_Db_Table
将尝试使用当前的数据库适配器,因此只需传入tablename即可
还有一个注意事项:使用select()
时,不需要使用任何quote()
函数
但非常重要的是,如果要使用Zend_Db,您需要学习“定义表类”。至少足够在你自己的课堂上使用它们
我希望这有帮助 问题在于,除非如前所述,否则不能使用依赖行集或父行集 要使当前功能正常工作,最好使用两个功能,并保持简单:
public function getPost($idPost)
{
$db= new Zend_Db_Table($this->_name);
$select=$db->select()
->where("idPost= ?", $idPost);
/*Fetch just the row you want, or use fetchAll() if you need to match return types*/
$row = $db->fetchRow($select);
return $row;
}
public function getComments($table='comments', $id) {
$db = new Zend_Db_table($table);
$select = $db->select()->where('post_id = ?', $id)->order('date ASC');
$rowset = $db->fetchAll($select);
return $rowset/* or you could return an array ->$rowset->toArray() */
}
Zend_Db_Table
将尝试使用当前的数据库适配器,因此只需传入tablename即可
还有一个注意事项:使用select()
时,不需要使用任何quote()
函数
但非常重要的是,如果要使用Zend_Db,您需要学习“定义表类”。至少足够在你自己的课堂上使用它们
我希望这有帮助 我做到了,我写了:protected$_name='post';受保护的$_primary='idPost';受保护的$_referenceMap=array('post'=>array('columns'=>'post','refTableClass'=>'post','refColumns'=>'idPost'),'autore'=>array('columns'=>'autore','refTableClass'=>'tenti','refColumns'=>'idtenti');但它不起作用..我怀疑这是错误..我做到了,我写了:protected$_name='post';受保护的$_primary='idPost';受保护的$_referenceMap=array('post'=>array('columns'=>'post','refTableClass'=>'post','refColumns'=>'idPost'),'autore'=>array('columns'=>'autore','refTableClass'=>'tenti','refColumns'=>'idtenti');但是它不起作用。我怀疑这是错误。非常感谢你的回答!!现在我认为我使用Zend db表的方式是错误的,我已经再次阅读了官方文档,但我不明白我遗漏了哪一段。正确的使用方法是什么?现在我尝试使用$stmt=$select->query()$stmt->setFetchMode(Zend_Db::FETCH_OBJ)$行集=$stmt->fetchAll();但我也有同样的错误