Zend_Db_Table_Abstract::_primary returns array?

回眸只為那壹抹淺笑 提交于 2019-12-24 06:59:23

问题


So I have defined a model like this:

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);
    }
}

When calling the delete method, I get a warning telling me $this->_primary is an array. Why? I have assigned a string value to $_primary property.

From logs:

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

回答1:


Zend_Db_Table stores primary keys as an array in case a compound key is used, so strictly speaking, it is best (not compulsory) to declare them like this:-

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

From the docblock in Zend_Db_Table_Abstract:-

/**
 * 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;

And from the dockblock for $_identity:-

/**
 * 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;

So you could probably use that instead.
If you have only one column in your primary key then it will be at $_primary[1].

I think this would work for you:-

public function delete($primaryKey)
{

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


来源:https://stackoverflow.com/questions/9151178/zend-db-table-abstract-primary-returns-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!