diff --git a/models/behaviors/linkable.php b/models/behaviors/linkable.php index 8b6c306..f38c7da 100644 --- a/models/behaviors/linkable.php +++ b/models/behaviors/linkable.php @@ -83,8 +83,6 @@ class LinkableBehavior extends ModelBehavior { /* pr("Reference: $options[reference] : $Reference->name ($Reference->alias)"); */ $db =& $_Model->getDataSource(); $associations = $_Model->getAssociated(); -/* pr("Assocations of $_Model->name"); */ -/* pr($associations); */ if (isset($associations[$Reference->alias])) { $type = $associations[$Reference->alias]; $association = $_Model->{$type}[$Reference->alias]; @@ -101,67 +99,39 @@ class LinkableBehavior extends ModelBehavior { $referenceKey = $Reference->escapeField($Reference->primaryKey); $options['conditions'] = "{$referenceKey} = {$modelKey}"; } elseif ($type === 'hasAndBelongsToMany') { -/* pr("Association"); */ -/* pr($association); */ if (isset($association['with'])) $Link =& $_Model->{$association['with']}; else $Link =& $_Model->{Inflector::classify($association['joinTable'])}; + $linkAlias = $Link->alias; + //$linkAlias = $Link->alias . $options['alias']; + // Get the foreign key fields (for the link table) directly from // the defined model associations, if they exists. This is the // users direct specification, and therefore definitive if present. - $modelLink = $Link->escapeField($association['foreignKey']); - $referenceLink = $Link->escapeField($association['associationForeignKey']); + $modelLink = $Link->escapeField($association['foreignKey'], $linkAlias); + $referenceLink = $Link->escapeField($association['associationForeignKey'], $linkAlias); // If we haven't figured out the foreign keys, see if there is a // model for the link table, and if it has the appropriate // associations with the two tables we're trying to join. if (empty($modelLink) && isset($Link->belongsTo[$_Model->alias])) - $modelLink = $Link->escapeField($Link->belongsTo[$_Model->alias]['foreignKey']); + $modelLink = $Link->escapeField($Link->belongsTo[$_Model->alias]['foreignKey'], $linkAlias); if (empty($referenceLink) && isset($Link->belongsTo[$Reference->alias])) - $referenceLink = $Link->escapeField($Link->belongsTo[$Reference->alias]['foreignKey']); + $referenceLink = $Link->escapeField($Link->belongsTo[$Reference->alias]['foreignKey'], $linkAlias); // We're running quite thin here. None of the models spell // out the appropriate linkages. We'll have to SWAG it. if (empty($modelLink)) - $modelLink = $Link->escapeField(Inflector::underscore($_Model->alias) . '_id'); + $modelLink = $Link->escapeField(Inflector::underscore($_Model->alias) . '_id', $linkAlias); if (empty($referenceLink)) - $referenceLink = $Link->escapeField(Inflector::underscore($Reference->alias) . '_id'); + $referenceLink = $Link->escapeField(Inflector::underscore($Reference->alias) . '_id', $linkAlias); // Get the primary key from the tables we're joining. $referenceKey = $Reference->escapeField(); $modelKey = $_Model->escapeField(); -/* pr("bf pre options:"); */ -/* pr($options); */ - -/* [joins] => Array */ -/* ( */ -/* [0] => Array */ -/* ( */ -/* [type] => LEFT */ -/* [table] => `pmgr_contacts_leases` */ -/* [alias] => ContactsLease */ -/* [joins] => Array */ -/* ( */ -/* [0] => Array */ -/* ( */ -/* [type] => INNER */ -/* [table] => `pmgr_leases` */ -/* [alias] => Lease */ -/* [conditions] => `Lease`.`id` = `ContactsLease`.`lease_id` */ -/* ) */ -/* ) */ -/* [conditions] => `ContactsLease`.`contact_id` = `Contact`.`id` */ -/* ) */ -/* ) */ -/* LEFT JOIN (`pmgr_contacts_leases` AS `ContactsLease` */ -/* INNER JOIN `pmgr_leases` AS `Lease` */ -/* ON (`Lease`.`id` = `ContactsLease`.`lease_id`) */ -/* ) ON (`ContactsLease`.`contact_id` = `Contact`.`id`) */ - - // Join the linkage table to our model. We'll use an inner join, // as the whole purpose of the linkage table is to make this // connection. As we are embedding this join, the INNER will not @@ -183,12 +153,9 @@ class LinkableBehavior extends ModelBehavior { // Now for the top level join. This will be added into the list // of joins down below, outside of the HABTM specific code. - $options['alias'] = $Link->alias; + $options['alias'] = $linkAlias; $options['table'] = $Link->getDataSource()->fullTableName($Link); $options['conditions'][] = "{$referenceLink} = {$referenceKey}"; - -/* pr("bf post options:"); */ -/* pr($options); */ } else { $referenceKey = $Reference->escapeField($association['foreignKey']); $modelKey = $_Model->escapeField($_Model->primaryKey); @@ -214,17 +181,6 @@ class LinkableBehavior extends ModelBehavior { if (!empty($options[$this->_key])) { $iterators[] = $options[$this->_key] + array('defaults' => array_merge($defaults, array('reference' => $options['class']))); } -/* pr("bf pre query:"); */ -/* pr($query); */ -/* $aik = array('type' => true, 'alias' => true, 'table' => true, 'joins' => true, 'conditions' => true); */ -/* pr("aik"); */ -/* pr($aik); */ -/* $ojoin = array_intersect_key($options, $aik); */ -/* pr("ojoin"); */ -/* pr($ojoin); */ -/* $query['joins'][] = $ojoin; */ -/* pr("bf post query:"); */ -/* pr($query); */ $query['joins'][] = array_intersect_key($options, array('type' => true, 'alias' => true, 'table' => true, 'joins' => true, 'conditions' => true)); } ++$cont;