[英]Zend Framework 1.12: _setupPrimaryKey method of Zend Table
I'm working on fixing bug related to Zend_Db_Table models. 我正在修复与Zend_Db_Table模型有关的错误。 The problem that I faced with is that $_primary property of the Zend_Db_Table ancestor mysteriously being changed after you call insert, update etc.
我面临的问题是Zend_Db_Table祖先的$ _primary属性在您调用插入,更新等后神秘地更改了。
Let's say I have the following value in this field: 假设我在此字段中具有以下值:
Class Model_Book extends App_Base_FileForAcs implements App_Interface_OnixDataSource, App_Interface_ApiDataSource, App_Interface_DbGateway
{
protected $_name = 'book';
protected $_primary = 'book_id';
...
}
If I check $_primary after insert operation it will contain: 如果我在插入操作后检查$ _primary,它将包含:
array(1) {
[1]=>
string(7) "book_id"
}
This transformation happens in _setupPrimaryKey method of Zend_Db_Table_Abstract class. 此转换发生在Zend_Db_Table_Abstract类的_setupPrimaryKey方法中。 Could you explain why this field should be transformed to array and why array starts with not 0 index?
您能否解释为什么应将此字段转换为数组,以及为什么数组以非0索引开头?
ZF internally uses an array to define the primary key in order to manage compound keys. ZF在内部使用数组定义主键,以便管理复合键。 It's described here:
在这里描述:
http://framework.zend.com/manual/1.12/en/zend.db.table.html#zend.db.table.defining.primary-key http://framework.zend.com/manual/1.12/en/zend.db.table.html#zend.db.table.defining.primary-key
The code in _setupPrimaryKey either casts your string to an array with index 1, or if it's already an array, does an array_unshift($this->_primary, null)
followed by unsetting the _primary[0] to make it 1-based. _setupPrimaryKey中的代码将您的字符串转换为索引为1的数组,或者如果它已经是数组,则执行
array_unshift($this->_primary, null)
然后取消设置_primary [0]以使其从1开始。
To get the primary key (as an array at all times), you'll find more information here: Get Primary Key out of Zend_Db_Table_Rowset Object - that includes how to get it from the table object too. 要获取主键(始终作为数组),您将在这里找到更多信息: 从Zend_Db_Table_Rowset对象中获取主键 -包括如何从表对象中获取主键 。
So, in short - the primary key is always an array, but ZF allows you to define it as a string, for convenience. 因此,简而言之-主键始终是数组,但是ZF允许您为方便起见将其定义为字符串。 You never described your bug, but if you rely on the primary key being a string, reading the $_primary directly, you should probably refactor to get the key using
$objZenDbTable->info('primary')
instead, fully expecting an array. 您从未描述过您的错误,但是如果您依赖主键为字符串,直接读取$ _primary,则可能应该改用
$objZenDbTable->info('primary')
来获取键,从而完全期待一个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.