Added link() functionality to create links between related records. It basically works in the opposite way as unlink().
This commit is contained in:
parent
f1653229ee
commit
c655b6023c
@ -1682,6 +1682,80 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* link
|
||||||
|
* creates links from this record to given records
|
||||||
|
*
|
||||||
|
* @param string $alias related component alias
|
||||||
|
* @param array $ids the identifiers of the related records
|
||||||
|
* @return Doctrine_Record this object
|
||||||
|
*/
|
||||||
|
public function link($alias, array $ids)
|
||||||
|
{
|
||||||
|
if ( ! count($ids)) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
$identifier = array_values($this->identifier());
|
||||||
|
$identifier = array_shift($identifier);
|
||||||
|
|
||||||
|
$rel = $this->getTable()->getRelation($alias);
|
||||||
|
|
||||||
|
if ($rel instanceof Doctrine_Relation_Association) {
|
||||||
|
|
||||||
|
$modelClassName = $rel->getAssociationTable()->getComponentName();
|
||||||
|
$localFieldName = $rel->getLocal();
|
||||||
|
$localFieldDef = $rel->getAssociationTable()->getColumnDefinition($localFieldName);
|
||||||
|
if ($localFieldDef['type'] == 'integer') {
|
||||||
|
$identifier = (integer) $identifier;
|
||||||
|
}
|
||||||
|
$foreignFieldName = $rel->getForeign();
|
||||||
|
$foreignFieldDef = $rel->getAssociationTable()->getColumnDefinition($foreignFieldName);
|
||||||
|
if ($foreignFieldDef['type'] == 'integer') {
|
||||||
|
for ($i = 0; $i < count($ids); $i++) {
|
||||||
|
$ids[$i] = (integer) $ids[$i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach ($ids as $id) {
|
||||||
|
$record = new $modelClassName;
|
||||||
|
$record[$localFieldName] = $identifier;
|
||||||
|
$record[$foreignFieldName] = $id;
|
||||||
|
$record->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if ($rel instanceof Doctrine_Relation_ForeignKey) {
|
||||||
|
|
||||||
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
|
$q->update($rel->getTable()->getComponentName())
|
||||||
|
->set($rel->getForeign(), '?', array_values($this->identifier()));
|
||||||
|
|
||||||
|
if (count($ids) > 0) {
|
||||||
|
$q->whereIn($rel->getTable()->getIdentifier(), $ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
$q->execute();
|
||||||
|
|
||||||
|
} else if ($rel instanceof Doctrine_Relation_LocalKey) {
|
||||||
|
|
||||||
|
$q = new Doctrine_Query();
|
||||||
|
|
||||||
|
$q->update($this->getTable()->getComponentName())
|
||||||
|
->set($rel->getLocal(), '?', $ids);
|
||||||
|
|
||||||
|
if (count($ids) > 0) {
|
||||||
|
$q->whereIn($rel->getTable()->getIdentifier(), array_values($this->identifier()));
|
||||||
|
}
|
||||||
|
|
||||||
|
$q->execute();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* __call
|
* __call
|
||||||
* this method is a magic method that is being used for method overloading
|
* this method is a magic method that is being used for method overloading
|
||||||
|
Loading…
x
Reference in New Issue
Block a user