Issues

ZF-3709: Inconsistent behaviour in Zend_Db_Table and supporting classes

Description

I stumbled across this inconsistency while following some of the examples of the framework documentation about Zend_Db_Table relationships

Sample tables:


CREATE TABLE owners (
  id int(10) unsigned NOT NULL auto_increment PRIMARY_KEY,
  name varchar(64) NOT NULL,
);

CREATE TABLE cars (
  id int(10) unsigned NOT NULL auto_increment PRIMARY_KEY,
  model varchar(64) NOT NULL,
);

CREATE TABLE owner_car (
  owner_id int(10) unsigned NOT NULL,
  car_id int(10) unsigned NOT NULL,
  mileage int(10) unsigned NOT NULL,
);

Then the referenceMap for owner_car:


    protected $_referenceMap = array(
            'Owner' => array(
                    'columns'       => array('owner_id'),
                    'refTableClass' => 'owners',
                    'refColumns'    => array('id'),
            ),
            'Car' => array(
                    'columns'       => array('car_id'),
                    'refTableClass' => 'cars',
                    'refColumns'    => array('id'),
            )
    );

Then when you use the findManyToManyRowset() from a Zend_Db_Table_Row object:


$ownerTable = new owners();
$ownerRecord = $ownerTable->fetchRow('id = 1');

$manyToManyRecords = $ownerRecord->findManyToManyRowset('cars', 'owner_car');

The resulting rows in the rowset contain the following columns:

owner_id, car_id, mileage, id, model,

the readOnly value of the row object will be false and the tableClass will be set to cars any subsequent save() will fail with an exception since not all returned columns are present in the cars table.

A variety of solutions are possible depending on the intended use cases but it does need some attention

Comments

Wouldn't


        $select->from(array('i' => $interName), array(), $interSchema)
               ->joinInner(array('m' => $matchName), $joinCond, Zend_Db_Select::SQL_WILDCARD, $matchSchema)
               ->setIntegrityCheck(false);

in place of


        $select->from(array('i' => $interName), Zend_Db_Select::SQL_WILDCARD, $interSchema)
               ->joinInner(array('m' => $matchName), $joinCond, Zend_Db_Select::SQL_WILDCARD, $matchSchema)
               ->setIntegrityCheck(false);

work, so that the join table's columns are ignored.

Fixed in trunk 21100 and in release branch 1.10 in 21102