Php 使用\Zend\u Db\u Table\u Abstract::find($id)。MySQL集合字段返回字符串而不是(想要的)int
基本问题如何从表映射器中获取整型值形式的“type”列? 我有一个运行网站的PHP Zend Framework 1.12应用程序。MySQL中有多个表和多列。 在两个表中,我使用SET类型。该列名为“type”,并命名为“set('LOCAL','EXTERNAL')”。请不要将此字段类型与枚举混淆 到目前为止没有问题,查询表并将类型列提取为INT或STRING不是问题:Php 使用\Zend\u Db\u Table\u Abstract::find($id)。MySQL集合字段返回字符串而不是(想要的)int,php,mysql,zend-framework,zend-db,Php,Mysql,Zend Framework,Zend Db,基本问题如何从表映射器中获取整型值形式的“type”列? 我有一个运行网站的PHP Zend Framework 1.12应用程序。MySQL中有多个表和多列。 在两个表中,我使用SET类型。该列名为“type”,并命名为“set('LOCAL','EXTERNAL')”。请不要将此字段类型与枚举混淆 到目前为止没有问题,查询表并将类型列提取为INT或STRING不是问题: $Sql = $Db->select()->from('tablename', ['type_as_int'
$Sql = $Db->select()->from('tablename', ['type_as_int' => new \Zend_Db_Expr('type+0')]); //returns INT (if both are selected: 3)
$Sql = $Db->select()->from('tablename', ['type']); //returns STRING (if both are selected: LOCAL,EXTERNAL)
但是,在这个应用程序中,还有扩展Zend_Db_table_Abstract的表映射器。
映射器中驻留着“find()”方法。默认内置于摘要中,用于按主键查找记录。
但是当我使用该对象获取记录时,我在我的populate方法中发现以下响应:
array([type] => LOCAL,EXTERNAL)
手动查询(并自己定义列)将是一个选项($this->select()->from…),但是没有更优雅的方法吗
(我知道我使用的是旧版本的ZF,但此时升级会花费太多时间。)在bounty启动后,我注意到没有真正简单的anwer,因此我开始深入研究Zend Framework 1.12和我使用的mapper对象。 我注意到“find()”方法只使用主键列来构建查询。 因此,从这些知识开始,我构建了自己的“find()”方法,该方法位于“抽象模型映射器”中,并在扩展\Zend\u Db\u Table\u abstract的类中使用“find()”映射器
/* sample abstract mapper class with find */
abstract class MapperAbstract {
/*
* Zend db table abstract object
* @var \Zend_Db_Table_Abstract
*/
private $DbTable;
public function find($id, $Model) {
$Select = $this->$DbTable->select(\Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART);
//Fetch record and populate model if we got
//a result
$Row = $this->$DbTable->fetchRow($Select);
//do some extra shizzle
if ($Row !== null) {
return $Model->populate((array)$Row);
}
return;
}
}
现在我需要添加一个覆盖默认列的方法。
因此,我创建了一个名为“overrideColumns()”的方法,该方法返回一个数组,其中填充了需要选择或必须重写的列名
/**
* Returns array with columns that need to be overridden
* or selected as extra
* @return array
*/
public function overrideColumns() {
return ['type' => new \Zend_Db_Expr('type+0')];
}
从那时起,我只需要调整$Select查询,使其使用'overrideColumns()'方法。
因此,整个类变成如下:
/* sample abstract mapper class with find */
abstract class MapperAbstract {
/*
* Zend db table abstract object
* @var \Zend_Db_Table_Abstract
*/
private $DbTable;
/**
* Returns array with columns that need to be overridden
* or selected as extra
* @return array
*/
private function overrideColumns() {
return ['type' => new \Zend_Db_Expr('type+0')];
}
public function find($id, $Model) {
$Select = $this->DbTable->select(\Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART);
//Check if we need to override columns in the select query
$overrideColumns = $this->getOverrideColumns();
if (!empty($overrideColumns)) {
$Select->columns($overrideColumns); //overrides the columns
}
//Add where clause to the query
//I know there can also be a compound primary key, but
//I'm just ignoring that in this example
$Select->where($this->DbTable->getPrimaryKeyColumn() . ' = ?', $id);
//doing some extra shizzle
//that is not important when I want to explain stuff
//Fetch record and populate model if we got a result
$Row = $this->DbTable->fetchRow($Select);
if ($Row !== null) {
return $Model->populate((array)$Row);
}
return;
}
}
所以。。过了一会儿,我找到了我想要的答案,而不必声明所有列。为什么不将字段存储为整数而不是SET,并使用bitmapindex来实现这一目的呢。