Performed cleanup, and started the thought process of figuring out how to use one link table multiple times in the same query. My changes worked actually, but I want to wait on this to see if I can figure out if there is already a core solution (since it seems likely that things behaving like a tree would have one table used multiple times in the same query... although they probably get around that by running 100 different queries come to think of it).
git-svn-id: file:///svn-source/pmgr/branches/initial_20090526@50 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -83,8 +83,6 @@ class LinkableBehavior extends ModelBehavior {
|
|||||||
/* pr("Reference: $options[reference] : $Reference->name ($Reference->alias)"); */
|
/* pr("Reference: $options[reference] : $Reference->name ($Reference->alias)"); */
|
||||||
$db =& $_Model->getDataSource();
|
$db =& $_Model->getDataSource();
|
||||||
$associations = $_Model->getAssociated();
|
$associations = $_Model->getAssociated();
|
||||||
/* pr("Assocations of $_Model->name"); */
|
|
||||||
/* pr($associations); */
|
|
||||||
if (isset($associations[$Reference->alias])) {
|
if (isset($associations[$Reference->alias])) {
|
||||||
$type = $associations[$Reference->alias];
|
$type = $associations[$Reference->alias];
|
||||||
$association = $_Model->{$type}[$Reference->alias];
|
$association = $_Model->{$type}[$Reference->alias];
|
||||||
@@ -101,67 +99,39 @@ class LinkableBehavior extends ModelBehavior {
|
|||||||
$referenceKey = $Reference->escapeField($Reference->primaryKey);
|
$referenceKey = $Reference->escapeField($Reference->primaryKey);
|
||||||
$options['conditions'] = "{$referenceKey} = {$modelKey}";
|
$options['conditions'] = "{$referenceKey} = {$modelKey}";
|
||||||
} elseif ($type === 'hasAndBelongsToMany') {
|
} elseif ($type === 'hasAndBelongsToMany') {
|
||||||
/* pr("Association"); */
|
|
||||||
/* pr($association); */
|
|
||||||
if (isset($association['with']))
|
if (isset($association['with']))
|
||||||
$Link =& $_Model->{$association['with']};
|
$Link =& $_Model->{$association['with']};
|
||||||
else
|
else
|
||||||
$Link =& $_Model->{Inflector::classify($association['joinTable'])};
|
$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
|
// Get the foreign key fields (for the link table) directly from
|
||||||
// the defined model associations, if they exists. This is the
|
// the defined model associations, if they exists. This is the
|
||||||
// users direct specification, and therefore definitive if present.
|
// users direct specification, and therefore definitive if present.
|
||||||
$modelLink = $Link->escapeField($association['foreignKey']);
|
$modelLink = $Link->escapeField($association['foreignKey'], $linkAlias);
|
||||||
$referenceLink = $Link->escapeField($association['associationForeignKey']);
|
$referenceLink = $Link->escapeField($association['associationForeignKey'], $linkAlias);
|
||||||
|
|
||||||
// If we haven't figured out the foreign keys, see if there is a
|
// 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
|
// model for the link table, and if it has the appropriate
|
||||||
// associations with the two tables we're trying to join.
|
// associations with the two tables we're trying to join.
|
||||||
if (empty($modelLink) && isset($Link->belongsTo[$_Model->alias]))
|
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]))
|
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
|
// We're running quite thin here. None of the models spell
|
||||||
// out the appropriate linkages. We'll have to SWAG it.
|
// out the appropriate linkages. We'll have to SWAG it.
|
||||||
if (empty($modelLink))
|
if (empty($modelLink))
|
||||||
$modelLink = $Link->escapeField(Inflector::underscore($_Model->alias) . '_id');
|
$modelLink = $Link->escapeField(Inflector::underscore($_Model->alias) . '_id', $linkAlias);
|
||||||
if (empty($referenceLink))
|
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.
|
// Get the primary key from the tables we're joining.
|
||||||
$referenceKey = $Reference->escapeField();
|
$referenceKey = $Reference->escapeField();
|
||||||
$modelKey = $_Model->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,
|
// 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
|
// as the whole purpose of the linkage table is to make this
|
||||||
// connection. As we are embedding this join, the INNER will not
|
// 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
|
// Now for the top level join. This will be added into the list
|
||||||
// of joins down below, outside of the HABTM specific code.
|
// of joins down below, outside of the HABTM specific code.
|
||||||
$options['alias'] = $Link->alias;
|
$options['alias'] = $linkAlias;
|
||||||
$options['table'] = $Link->getDataSource()->fullTableName($Link);
|
$options['table'] = $Link->getDataSource()->fullTableName($Link);
|
||||||
$options['conditions'][] = "{$referenceLink} = {$referenceKey}";
|
$options['conditions'][] = "{$referenceLink} = {$referenceKey}";
|
||||||
|
|
||||||
/* pr("bf post options:"); */
|
|
||||||
/* pr($options); */
|
|
||||||
} else {
|
} else {
|
||||||
$referenceKey = $Reference->escapeField($association['foreignKey']);
|
$referenceKey = $Reference->escapeField($association['foreignKey']);
|
||||||
$modelKey = $_Model->escapeField($_Model->primaryKey);
|
$modelKey = $_Model->escapeField($_Model->primaryKey);
|
||||||
@@ -214,17 +181,6 @@ class LinkableBehavior extends ModelBehavior {
|
|||||||
if (!empty($options[$this->_key])) {
|
if (!empty($options[$this->_key])) {
|
||||||
$iterators[] = $options[$this->_key] + array('defaults' => array_merge($defaults, array('reference' => $options['class'])));
|
$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));
|
$query['joins'][] = array_intersect_key($options, array('type' => true, 'alias' => true, 'table' => true, 'joins' => true, 'conditions' => true));
|
||||||
}
|
}
|
||||||
++$cont;
|
++$cont;
|
||||||
|
|||||||
Reference in New Issue
Block a user