Working version of the Linkable behavior that allows for a HABTM joining table to have an alias specified (thus allowing more than one instance in a query). I don't like the $efields bit though, and will change that on the next checkin. Don't want to lose a working version though!
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@148 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -80,7 +80,7 @@ class LinkableBehavior extends ModelBehavior {
|
|||||||
protected $_options = array(
|
protected $_options = array(
|
||||||
'type' => true, 'table' => true, 'alias' => true, 'joins' => true,
|
'type' => true, 'table' => true, 'alias' => true, 'joins' => true,
|
||||||
'conditions' => true, 'fields' => true, 'reference' => true,
|
'conditions' => true, 'fields' => true, 'reference' => true,
|
||||||
'class' => true, 'defaults' => true
|
'class' => true, 'defaults' => true, 'linkalias' => true,
|
||||||
);
|
);
|
||||||
|
|
||||||
protected $_defaults = array('type' => 'LEFT');
|
protected $_defaults = array('type' => 'LEFT');
|
||||||
@@ -134,6 +134,7 @@ class LinkableBehavior extends ModelBehavior {
|
|||||||
$iterations = Set::normalize($iterator);
|
$iterations = Set::normalize($iterator);
|
||||||
/* pr(compact('iterations')); */
|
/* pr(compact('iterations')); */
|
||||||
foreach ($iterations as $alias => $options) {
|
foreach ($iterations as $alias => $options) {
|
||||||
|
$efields = array();
|
||||||
if (is_null($options)) {
|
if (is_null($options)) {
|
||||||
$options = array();
|
$options = array();
|
||||||
}
|
}
|
||||||
@@ -218,8 +219,10 @@ class LinkableBehavior extends ModelBehavior {
|
|||||||
else
|
else
|
||||||
$Link =& $_Model->{Inflector::classify($association['joinTable'])};
|
$Link =& $_Model->{Inflector::classify($association['joinTable'])};
|
||||||
|
|
||||||
|
if (isset($options['linkalias']))
|
||||||
|
$linkAlias = $options['linkalias'];
|
||||||
|
else
|
||||||
$linkAlias = $Link->alias;
|
$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
|
||||||
@@ -260,7 +263,13 @@ 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.
|
||||||
|
if (!isset($options['fields']) || !is_array($options['fields']))
|
||||||
|
$efields = $db->fields($_Model, $options['alias']);
|
||||||
|
elseif (!empty($options['fields']))
|
||||||
|
$efields = $db->fields($_Model, $options['alias'], $options['fields']);
|
||||||
|
|
||||||
$options['alias'] = $linkAlias;
|
$options['alias'] = $linkAlias;
|
||||||
|
$options['fields'] = array();
|
||||||
$options['table'] = $Link->getDataSource()->fullTableName($Link);
|
$options['table'] = $Link->getDataSource()->fullTableName($Link);
|
||||||
$options['conditions'][] = "{$referenceLink} = {$referenceKey}";
|
$options['conditions'][] = "{$referenceLink} = {$referenceKey}";
|
||||||
}
|
}
|
||||||
@@ -293,11 +302,11 @@ class LinkableBehavior extends ModelBehavior {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($options['fields']) || !is_array($options['fields']))
|
if (!isset($options['fields']) || !is_array($options['fields']))
|
||||||
$options['fields'] = $db->fields($_Model, $options['alias']);
|
$efields = $db->fields($_Model, $options['alias']);
|
||||||
elseif (!empty($options['fields']))
|
elseif (!empty($options['fields']))
|
||||||
$options['fields'] = $db->fields($_Model, $options['alias'], $options['fields']);
|
$efields = $db->fields($_Model, $options['alias'], $options['fields']);
|
||||||
|
|
||||||
$query['fields'] = array_merge($query['fields'], $options['fields'],
|
$query['fields'] = array_merge($query['fields'], $efields,
|
||||||
(empty($association['fields'])
|
(empty($association['fields'])
|
||||||
? array() : $db->fields($_Model, $options['alias'], $association['fields'])));
|
? array() : $db->fields($_Model, $options['alias'], $association['fields'])));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user