I'm still in the middle of moving onto a ledger based system. However, I'm am now changing how transactions and entries relate back to the customer. I'll be using a ledger for each lease (for rent, late charges, security deposits, etc), and a ledger for each customer (for POS, non-specific deposits such as reservations or covering mulitple units, bad debt writeoff, and possibly customer credits, when not obviously lease specific). This coming change might not be in the right direction, so I want to capture the work as is right now. This change set is not fully functional. Many operations do work, but there are obviously transaction problems with units and customers.
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605@71 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -82,23 +82,51 @@ class LinkableBehavior extends ModelBehavior {
|
||||
/* pr("_Model: $options[class] : $_Model->name ($_Model->alias)"); */
|
||||
/* pr("Reference: $options[reference] : $Reference->name ($Reference->alias)"); */
|
||||
$db =& $_Model->getDataSource();
|
||||
$associations = $_Model->getAssociated();
|
||||
if (isset($associations[$Reference->alias])) {
|
||||
|
||||
$associatedThroughReference = 0;
|
||||
$association = null;
|
||||
|
||||
// Figure out how these two models are related, creating
|
||||
// a relationship if one doesn't otherwise already exists.
|
||||
if (($associations = $Reference->getAssociated()) &&
|
||||
isset($associations[$_Model->alias])) {
|
||||
/* pr("Reference defines association to _Model"); */
|
||||
$associatedThroughReference = 1;
|
||||
$type = $associations[$_Model->alias];
|
||||
$association = $Reference->{$type}[$_Model->alias];
|
||||
}
|
||||
elseif (($associations = $_Model->getAssociated()) &&
|
||||
isset($associations[$Reference->alias])) {
|
||||
/* pr("_Model defines association to Reference"); */
|
||||
$type = $associations[$Reference->alias];
|
||||
$association = $_Model->{$type}[$Reference->alias];
|
||||
} else {
|
||||
$_Model->bind($Reference->alias);
|
||||
}
|
||||
else {
|
||||
// No relationship... make our best effort to create one.
|
||||
/* pr("No assocation between _Model and Reference"); */
|
||||
$type = 'belongsTo';
|
||||
$_Model->bind($Reference->alias);
|
||||
// Grab the association now, since we'll unbind in a moment.
|
||||
$association = $_Model->{$type}[$Reference->alias];
|
||||
$_Model->unbindModel(array('belongsTo' => array($Reference->alias)));
|
||||
}
|
||||
|
||||
// Determine which model holds the foreign key
|
||||
if (($type === 'hasMany' || $type === 'hasOne') ^ $associatedThroughReference) {
|
||||
$primaryModel = $Reference;
|
||||
$foreignModel = $_Model;
|
||||
} else {
|
||||
$primaryModel = $_Model;
|
||||
$foreignModel = $Reference;
|
||||
}
|
||||
|
||||
/* pr("primaryModel: $primaryModel->name ($primaryModel->alias)"); */
|
||||
/* pr("foreignModel: $foreignModel->name ($foreignModel->alias)"); */
|
||||
/* pr($type); */
|
||||
/* pr($association); */
|
||||
|
||||
if (empty($options['conditions'])) {
|
||||
if ($type === 'belongsTo') {
|
||||
$modelKey = $_Model->escapeField($association['foreignKey']);
|
||||
$referenceKey = $Reference->escapeField($Reference->primaryKey);
|
||||
$options['conditions'] = "{$referenceKey} = {$modelKey}";
|
||||
} elseif ($type === 'hasAndBelongsToMany') {
|
||||
if ($type === 'hasAndBelongsToMany') {
|
||||
if (isset($association['with']))
|
||||
$Link =& $_Model->{$association['with']};
|
||||
else
|
||||
@@ -139,17 +167,10 @@ class LinkableBehavior extends ModelBehavior {
|
||||
// be concerned with whether or not the join has any results.
|
||||
// They control that with the 'type' parameter which will be at
|
||||
// the top level join.
|
||||
$options['joins'][] = array('type' => 'INNER',
|
||||
'alias' => $options['alias'],
|
||||
'conditions' => "{$modelKey} = {$modelLink}",
|
||||
'table' => $db->fullTableName($_Model, true));
|
||||
|
||||
// The user may have specified conditions directly in the model
|
||||
// for this join. Make sure to adhere to those conditions.
|
||||
if (isset($association['conditions']) && is_array($association['conditions']))
|
||||
$options['conditions'] = $association['conditions'];
|
||||
elseif (!empty($association['conditions']))
|
||||
$options['conditions'] = array($association['conditions']);
|
||||
$options['joins'][] = array('type' => 'INNER',
|
||||
'alias' => $options['alias'],
|
||||
'conditions' => "{$modelKey} = {$modelLink}",
|
||||
'table' => $db->fullTableName($_Model, true));
|
||||
|
||||
// Now for the top level join. This will be added into the list
|
||||
// of joins down below, outside of the HABTM specific code.
|
||||
@@ -157,10 +178,28 @@ class LinkableBehavior extends ModelBehavior {
|
||||
$options['table'] = $Link->getDataSource()->fullTableName($Link);
|
||||
$options['conditions'][] = "{$referenceLink} = {$referenceKey}";
|
||||
} else {
|
||||
$referenceKey = $Reference->escapeField($association['foreignKey']);
|
||||
$modelKey = $_Model->escapeField($_Model->primaryKey);
|
||||
$options['conditions'] = "{$modelKey} = {$referenceKey}";
|
||||
$foreignKey = $primaryModel->escapeField($association['foreignKey']);
|
||||
$primaryKey = $foreignModel->escapeField($foreignModel->primaryKey);
|
||||
|
||||
// Only differentiating to help show the logical flow.
|
||||
// Either way works and this test can be tossed out
|
||||
if (($type === 'hasMany' || $type === 'hasOne') ^ $associatedThroughReference)
|
||||
$options['conditions'][] = "{$primaryKey} = {$foreignKey}";
|
||||
else
|
||||
$options['conditions'][] = "{$foreignKey} = {$primaryKey}";
|
||||
}
|
||||
|
||||
// The user may have specified conditions directly in the model
|
||||
// for this join. Make sure to adhere to those conditions.
|
||||
if (isset($association['conditions']) && is_array($association['conditions']))
|
||||
$options['conditions'] = array_merge($options['conditions'], $association['conditions']);
|
||||
elseif (!empty($association['conditions']))
|
||||
$options['conditions'][] = $association['conditions'];
|
||||
|
||||
/* pr(array("Link ", $options['conditions'])); */
|
||||
//pr($modelKey);
|
||||
//pr($referenceKey);
|
||||
|
||||
}
|
||||
if (empty($options['table'])) {
|
||||
$options['table'] = $db->fullTableName($_Model, true);
|
||||
|
||||
Reference in New Issue
Block a user