Php Zend_Db_Table_Abstract::_primary返回数组?
所以我定义了这样一个模型:Php Zend_Db_Table_Abstract::_primary返回数组?,php,zend-framework,Php,Zend Framework,所以我定义了这样一个模型: class Model extends Zend_Db_Table_Abstract { $_primary = 'modelID'; /** * * @param mixed $primaryKey * @return int */ public function delete($primaryKey) { $where = $this->getAdapter()-&
class Model extends Zend_Db_Table_Abstract
{
$_primary = 'modelID';
/**
*
* @param mixed $primaryKey
* @return int
*/
public function delete($primaryKey)
{
$where = $this->getAdapter()->quoteInto($this->_primary.' = ?', $primaryKey);
return parent::delete($where);
}
}
调用delete方法时,我收到一条警告,告诉我$this->\u primary是一个数组。为什么?我已为$\u primary属性指定了一个字符串值
从日志:
2012-02-05T17:41:03+00:00 INFO (6): Array
(
[1] => modelID
)
Zend_Db_Table将主键存储为数组,以防使用复合键,因此严格来说,最好(不是强制)这样声明它们:-
class Model extends Zend_Db_Table_Abstract
{
public function __construct(array $config = null)
{
$this->_primary[1] = 'modelId';
parent::__construct($config);
//.............
从Zend_Db_表_摘要中的docblock:-
/**
* The primary key column or columns.
* A compound key should be declared as an array.
* You may declare a single-column primary key
* as a string.
*
* @var mixed
*/
protected $_primary = null;
从$\u标识的dockblock:-
/**
* If your primary key is a compound key, and one of the columns uses
* an auto-increment or sequence-generated value, set _identity
* to the ordinal index in the $_primary array for that column.
* Note this index is the position of the column in the primary key,
* not the position of the column in the table. The primary key
* array is 1-based.
*
* @var integer
*/
protected $_identity = 1;
所以你可以用它来代替。如果主键中只有一列,则它将位于$\u primary[1] 我认为这会对你有用:-
public function delete($primaryKey)
{
$where = $this->getAdapter()->quoteInto($this->_primary[1] .' = ?', $primaryKey);
return parent::delete($where);
}
Zend_Db_Table将主键存储为数组,以防使用复合键,因此严格来说,最好(不是强制)这样声明它们:-
class Model extends Zend_Db_Table_Abstract
{
public function __construct(array $config = null)
{
$this->_primary[1] = 'modelId';
parent::__construct($config);
//.............
从Zend_Db_表_摘要中的docblock:-
/**
* The primary key column or columns.
* A compound key should be declared as an array.
* You may declare a single-column primary key
* as a string.
*
* @var mixed
*/
protected $_primary = null;
从$\u标识的dockblock:-
/**
* If your primary key is a compound key, and one of the columns uses
* an auto-increment or sequence-generated value, set _identity
* to the ordinal index in the $_primary array for that column.
* Note this index is the position of the column in the primary key,
* not the position of the column in the table. The primary key
* array is 1-based.
*
* @var integer
*/
protected $_identity = 1;
所以你可以用它来代替。如果主键中只有一列,则它将位于$\u primary[1] 我认为这会对你有用:-
public function delete($primaryKey)
{
$where = $this->getAdapter()->quoteInto($this->_primary[1] .' = ?', $primaryKey);
return parent::delete($where);
}
我收到一条警告,告诉我$this->primary
这是打字错误吗?您使用的是\u primary
,而不是primary
@SimpleCoder是的,这是一个打字错误。修正了,嗯。我以前从未见过这个问题。你能在你的delete
方法的第一行之前发布数组的var\u dump
吗?@SimpleCoder我已经发布了$\u primary的打印。我收到一条警告,告诉我$this->primary
这是打字错误吗?您使用的是\u primary
,而不是primary
@SimpleCoder是的,这是一个打字错误。修正了,嗯。我以前从未见过这个问题。你能在你的delete
方法的第一行之前发布数组的var\u dump
吗?@SimpleCoder我已经发布了$\u primary的打印内容。+1@vascowhite:我已经删除了我的答案,因为你的答案解释得更好。@vascowhite。我想这可能是Zend中的一个bug。因为在我的一些单元测试中$\u受保护是数组,而在另一些单元测试中它是字符串…$\u受保护?你的意思是$u小学吗?如果扩展了Zend_Db_Table_Abstract,那么$\u primary是一个数组。检查实际的代码,您将看到。很明显,您得到了多种行为,您可以始终将主键声明为数组,以保持一致性。我不认为您看到的是bug,因为文档中已经清楚地描述了它。像这样扩展Zend_Db_表对于单元测试是不好的。你应该把它的一个实例作为依赖注入。+1@vascowhite:我已经删除了我的答案,因为你的答案解释得更好。@vascowhite。我想这可能是Zend中的一个bug。因为在我的一些单元测试中$\u受保护是数组,而在另一些单元测试中它是字符串…$\u受保护?你的意思是$u小学吗?如果扩展了Zend_Db_Table_Abstract,那么$\u primary是一个数组。检查实际的代码,您将看到。很明显,您得到了多种行为,您可以始终将主键声明为数组,以保持一致性。我不认为您看到的是bug,因为文档中已经清楚地描述了它。像这样扩展Zend_Db_表对于单元测试是不好的。您应该将它的一个实例作为依赖注入。