1

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
)
hakre
  • 193,403
  • 52
  • 435
  • 836
Richard Knop
  • 81,041
  • 149
  • 392
  • 552

1 Answers1

5

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);
}
vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • +1 @vascowhite: I've deleted my answer since yours explains this much better. – Chris Laplante Feb 05 '12 at 18:03
  • @vascowhite. I think this might be a bug in Zend. Because in some of my unit tests $_protected is array, in others it is a string... – Richard Knop Feb 05 '12 at 18:36
  • $_protected? do u mean $_primary? If you have extended Zend_Db_Table_Abstract then $_primary is an array. Check the actual code and you will see. – vascowhite Feb 05 '12 at 18:44
  • You are obviously getting a mix of behaviours, you could always declare your primary keys as arrays to keep things consistent. I don't think what you are seeing is a bug as it is clearly described in the docs. – vascowhite Feb 05 '12 at 18:47
  • Extending Zend_Db_Table like this is not good for unit testing. You should be injecting an instance of it as a dependancy. – vascowhite Feb 05 '12 at 18:56
  • @vascowhite Ok. Now it works. But I still don't understand why $_primary was switching from array to string between my unit tests. – Richard Knop Feb 05 '12 at 18:59
  • Take a look at the _setupPrimaryKey() method in Zend/Db/Table/Abstract.php and you will see it gets set there, but it shouldn't if already set, so without seeing all of your code I am unable to explain it, sorry. – vascowhite Feb 05 '12 at 19:03