Zend_Db_Table_Abstract :: _ primary返回数组?

Ric*_*nop 1 php zend-framework

所以我定义了一个这样的模型:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

当调用delete方法时,我收到一个警告,告诉我$ this - > _ primary是一个数组.为什么?我已经为$ _primary属性分配了一个字符串值.

来自日志:

2012-02-05T17:41:03+00:00 INFO (6): Array
(
    [1] => modelID
)
Run Code Online (Sandbox Code Playgroud)

vas*_*ite 5

Zend_Db_Table将主键存储为数组,以防使用复合键,严格来说,最好(非强制性)声明它们如下: -

class Model extends Zend_Db_Table_Abstract
{
    public function __construct(array $config = null)
    {
        $this->_primary[1] = 'modelId';
        parent::__construct($config);
        //.............
Run Code Online (Sandbox Code Playgroud)

来自Zend_Db_Table_Abstract中的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;
Run Code Online (Sandbox Code Playgroud)

从$ _identity的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;
Run Code Online (Sandbox Code Playgroud)

所以你可以改用它.
如果主键中只有一列,那么它将位于$ _primary [1].

我认为这对你有用: -

public function delete($primaryKey)
{

    $where = $this->getAdapter()->quoteInto($this->_primary[1] .' = ?', $primaryKey);
    return parent::delete($where);
}
Run Code Online (Sandbox Code Playgroud)