Basic question How can I fetch the 'type' column as integer value from inside the table mapper?
基本问题如何从表映射器中获取“类型”列作为整数值?
I have a PHP Zend Framework 1.12 application running a website. Inside MySQL are multiple tables with multiple columns. Inside two tables I use the SET type. The column is named 'type' and as 'set('LOCAL','EXTERNAL')'. Don't mix up this field type with the ENUM please!
我有一个运行网站的PHP Zend Framework 1.12应用程序。 MySQL内部是多个包含多列的表。在两个表中我使用SET类型。该列被命名为'type'和'set('LOCAL','EXTERNAL')'。请不要将此字段类型与ENUM混淆!
So far no problems, querying the table and fetching the type column as INT or STRING is not a problem:
到目前为止没有问题,查询表并将类型列提取为INT或STRING不是问题:
$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)
But, in this application also has table mappers that extend Zend_Db_Table_Abstract. Inside the mapper resides the 'find()' method. Default built in into the abstract to find records by their primary key. But.. When I use the object to fetch a record , I find the following response inside my populate method:
但是,在此应用程序中还有扩展Zend_Db_Table_Abstract的表映射器。映射器内部存在'find()'方法。默认内置于摘要中以按主键查找记录。但是..当我使用该对象获取记录时,我在populate方法中找到以下响应:
array([type] => LOCAL,EXTERNAL)
Querying it by hand (and defining the columns myself) would be an options ($this->select()->from...), but isn't there a more elegant way?
手动查询(并自己定义列)将是一个选项($ this-> select() - > from ...),但是不是更优雅的方式吗?
(I know that I am using an older version of ZF, but upgrading would cost too much time at this moment.)
(我知道我使用的是旧版ZF,但此时升级会耗费太多时间。)
1 个解决方案
#1
0
After the bounty was started I noticed that there wasn't a really simple anwer, so I began looking deeper into Zend Framework 1.12 and the mapper objects that I use. I noticed that the 'find()' method just uses the primary key columns to build a query. So starting with that knowledge I built my own 'find()' method which resides in the 'abstract model mapper' and uses the 'find()' mapper inside the class that extends \Zend_Db_Table_Abstract
赏金开始后,我注意到没有一个非常简单的anwer,所以我开始深入研究Zend Framework 1.12以及我使用的映射器对象。我注意到'find()'方法只使用主键列来构建查询。因此,从这些知识开始,我构建了自己的'find()'方法,该方法位于“抽象模型映射器”中,并使用扩展\ Zend_Db_Table_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;
}
}
Now I need to add a method that overrides the default columns. So I created a method called 'overrideColumns()' that return an array filled with column names that need to be selected or must be overriden.
现在我需要添加一个覆盖默认列的方法。所以我创建了一个名为'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')];
}
And from that point I only needed to adjust the $Select query so it would use the 'overrideColumns()' method. So the full class becomes something like:
从那时起,我只需要调整$ 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;
}
}
So.. after a while I found the answer I was looking for, without having to declare all columns.
所以...过了一会儿我找到了我想要的答案,而不必声明所有列。
#1
0
After the bounty was started I noticed that there wasn't a really simple anwer, so I began looking deeper into Zend Framework 1.12 and the mapper objects that I use. I noticed that the 'find()' method just uses the primary key columns to build a query. So starting with that knowledge I built my own 'find()' method which resides in the 'abstract model mapper' and uses the 'find()' mapper inside the class that extends \Zend_Db_Table_Abstract
赏金开始后,我注意到没有一个非常简单的anwer,所以我开始深入研究Zend Framework 1.12以及我使用的映射器对象。我注意到'find()'方法只使用主键列来构建查询。因此,从这些知识开始,我构建了自己的'find()'方法,该方法位于“抽象模型映射器”中,并使用扩展\ Zend_Db_Table_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;
}
}
Now I need to add a method that overrides the default columns. So I created a method called 'overrideColumns()' that return an array filled with column names that need to be selected or must be overriden.
现在我需要添加一个覆盖默认列的方法。所以我创建了一个名为'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')];
}
And from that point I only needed to adjust the $Select query so it would use the 'overrideColumns()' method. So the full class becomes something like:
从那时起,我只需要调整$ 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;
}
}
So.. after a while I found the answer I was looking for, without having to declare all columns.
所以...过了一会儿我找到了我想要的答案,而不必声明所有列。