Zend_Db_Table_Abstract :: _ первичный возвращает массив? - PullRequest
1 голос
/ 05 февраля 2012

Итак, я определил модель следующим образом:

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 -> _ primary - это массив. Зачем? Я присвоил строковое значение свойству $ _primary.

Из журналов:

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

1 Ответ

5 голосов
/ 05 февраля 2012

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_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;

И из док-блока для $ _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;

Так что вы, вероятно, можете использовать это вместо этого.
Если у вас только один столбец в первичном ключетогда он будет равен $ _primary [1].

Я думаю, это будет работать для вас: -

public function delete($primaryKey)
{

    $where = $this->getAdapter()->quoteInto($this->_primary[1] .' = ?', $primaryKey);
    return parent::delete($where);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...