public function linkInDb($alias, $ids)
{
$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->getLocalFieldName();
$localFieldDef = $rel->getAssociationTable()->getColumnDefinition($localFieldName);
if ($localFieldDef['type'] == 'integer') {
$identifier = (int) $identifier;
}
$foreignFieldName = $rel->getForeignFieldName();
$foreignFieldDef = $rel->getAssociationTable()->getColumnDefinition($foreignFieldName);
if ($foreignFieldDef['type'] == 'integer') {
foreach ($ids as $i => $id) {
$ids[$i] = (int) $id;
}
}
foreach ($ids as $id) {
$record = new $modelClassName();
$record[$localFieldName] = $identifier;
$record[$foreignFieldName] = $id;
$record->save();
}
} else {
if ($rel instanceof Doctrine_Relation_ForeignKey) {
$q = $rel->getTable()->createQuery()->update()->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 = $this->getTable()->createQuery()->update()->set($rel->getLocalFieldName(), '?', $ids);
if (count($ids) > 0) {
$q->whereIn($rel->getTable()->getIdentifier(), array_values($this->identifier()));
}
$q->execute();
}
}
}
return $this;
}