Zend_Db_Table_Row_Abstract 扩展行类不起作用
我正在使用 Zend_Db 框架,但遇到了障碍。我试图在行级别向列命名添加自定义处理,但由于某种原因无法调用我的函数。
我已经简化了这个问题,只是尝试找出底层的“Row”类是否被创建过。据我所知,事实并非如此。
这是我得到的:
// this class functions correctly; I get "table" written to my output
class DHR_Table extends Zend_Db_Table_Abstract
{
protected $_rowClass = 'DHR_Row';
function __construct(){
echo "table";
parent::__construct();
}
}
// this class never gets called, at least not that is evident from the constructor echo
class DHR_Row extends Zend_Db_Table_Row_Abstract
{
protected $inflector = null;
function __construct(){
echo "row";
parent::__construct();
}
}
// this is the actual implementation class that uses these two:
class Application_Model_DbTable_Applicants extends DHR_Table
{
protected $_name = 'applicants';
}
我的输出包括一些数据(本文中排除,但属于“申请人”类的一部分)和“表”,但没有“行”。任何想法为什么会发生这种情况? Zend 框架版本 1.11.11。
[编辑] 这是用法:
class ApplicantsController extends DHR_RestController
{
public function indexAction()
{
$applicants = new Application_Model_DbTable_Applicants();
$result = $applicants->fetchAll();
$this->success($result);
}
protected function success($data, $code = 200)
{
if(is_a($data, 'Zend_Db_Table_Rowset')){
// we could do some pagination work here
$data = $data->toArray();
}
$this->setResponseCode($code)->appendBody(Zend_Json::encode(array(
'success'=>true,
'data' => $data
)));
}
}
我希望在返回序列化结果时至少在行类上调用一些方法...
[更新] 如果我使用“fetchRow”,一切都会按预期工作; fetchAll 根本不进行到底层对象类型的转换。
I'm using the Zend_Db framework, and I've run into a snag. I'm trying to add custom handling to the column naming at the row level, but it's failing to invoke my function for some reason.
I've stripped down the problem to simply try and figure out if the underlying "Row" class is ever even created. From what I can tell, it isn't.
Here's what I've got:
// this class functions correctly; I get "table" written to my output
class DHR_Table extends Zend_Db_Table_Abstract
{
protected $_rowClass = 'DHR_Row';
function __construct(){
echo "table";
parent::__construct();
}
}
// this class never gets called, at least not that is evident from the constructor echo
class DHR_Row extends Zend_Db_Table_Row_Abstract
{
protected $inflector = null;
function __construct(){
echo "row";
parent::__construct();
}
}
// this is the actual implementation class that uses these two:
class Application_Model_DbTable_Applicants extends DHR_Table
{
protected $_name = 'applicants';
}
My output includes some data (excluded from this post, but part of the "Applicants" class) and "table", but no "row". Any ideas why this might be happening? Version 1.11.11 of the Zend framework.
[Edit]
Here's the usage:
class ApplicantsController extends DHR_RestController
{
public function indexAction()
{
$applicants = new Application_Model_DbTable_Applicants();
$result = $applicants->fetchAll();
$this->success($result);
}
protected function success($data, $code = 200)
{
if(is_a($data, 'Zend_Db_Table_Rowset')){
// we could do some pagination work here
$data = $data->toArray();
}
$this->setResponseCode($code)->appendBody(Zend_Json::encode(array(
'success'=>true,
'data' => $data
)));
}
}
I would expect to at least have some method on the row class invoked when returning the serialized results...
[Update]
If I use "fetchRow" everything works as expected; fetchAll simply does't do the conversion to the underlying object type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是在查看 row/abstract 类的代码。
尝试为 $_tableClass 设置一个值。
$_tableClass = 'DHR_Table';
恐怕这不起作用,因为它看起来像 Zend/Db/Table/Row/Abstract无论如何,.php 都会查找表定义,因此如果不进一步扩展,您似乎追求的抽象级别可能无法实现。
I was just looking at the code for the row/abstract class.
Try setting a value for $_tableClass.
$_tableClass = 'DHR_Table';
I'm afraid that won't work as it looks like Zend/Db/Table/Row/Abstract.php is going to look for a table definition no matter what, so the level of abstraction you seem to be after may not be possible without further extending.