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@148 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-06-16 17:19:07 +00:00
parent 0270e5ea89
commit ce9142ffe2

View File

@@ -80,7 +80,7 @@ class LinkableBehavior extends ModelBehavior {
protected $_options = array(
'type' => true, 'table' => true, 'alias' => true, 'joins' => true,
'conditions' => true, 'fields' => true, 'reference' => true,
'class' => true, 'defaults' => true
'class' => true, 'defaults' => true, 'linkalias' => true,
);
protected $_defaults = array('type' => 'LEFT');
@@ -134,6 +134,7 @@ class LinkableBehavior extends ModelBehavior {
$iterations = Set::normalize($iterator);
/* pr(compact('iterations')); */
foreach ($iterations as $alias => $options) {
$efields = array();
if (is_null($options)) {
$options = array();
}
@@ -218,8 +219,10 @@ class LinkableBehavior extends ModelBehavior {
else
$Link =& $_Model->{Inflector::classify($association['joinTable'])};
if (isset($options['linkalias']))
$linkAlias = $options['linkalias'];
else
$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
@@ -260,7 +263,13 @@ 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.
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['fields'] = array();
$options['table'] = $Link->getDataSource()->fullTableName($Link);
$options['conditions'][] = "{$referenceLink} = {$referenceKey}";
}
@@ -293,11 +302,11 @@ class LinkableBehavior extends ModelBehavior {
}
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']))
$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'])
? array() : $db->fields($_Model, $options['alias'], $association['fields'])));