/** * Add a relationship to the model * * @param One_Model $model * @param One_Relation_Adapter $link */ public function addRelations(One_Model $model, One_Relation_Adapter $link) { $added = $model->getAddedRelations(); if (isset($added[$link->getName()])) { // @todo - this probably isn't the correct way to get to the db object we need? // the db object should be based on the info in the $link, not the $model ... $scheme = One_Repository::getScheme($model->getSchemeName()); $db = $this->db($scheme); $table = $link->meta['table']; $localKey = $link->fk('local'); $remoteKey = $link->fk('remote'); $localId = $model->getIdentityName(); $localValue = $model->{$localId}; // Insert the new (modified) relations in the given model $values = array(); foreach ($added[$link->getName()] as $remoteValue) { if (is_array($remoteValue)) { foreach ($remoteValue as $rVal) { $values[] = '( "' . mysql_real_escape_string($localValue, $db) . '", "' . mysql_real_escape_string($rVal, $db) . '") '; } } else { $values[] = '( "' . mysql_real_escape_string($localValue, $db) . '", "' . mysql_real_escape_string($remoteValue, $db) . '") '; } } // only run the insert query if we actually received new values if (count($values)) { $sql = 'INSERT INTO `' . $table . '` (`' . $localKey . '`, `' . $remoteKey . '`) ' . 'VALUES ' . implode(", ", $values); mysql_query($sql, $db); } } }