/** * Delete a relationship from the model * * @param One_Model $model * @param One_Relation_Adapter $link */ public function deleteRelations(One_Model $model, One_Relation_Adapter $link) { $deleted = $model->getDeletedRelations(); if (isset($deleted[$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 ($deleted[$link->getName()] as $remoteValue) { if (is_array($remoteValue)) { foreach ($remoteValue as $rVal) { $values[] = '`' . $remoteKey . '` = "' . mysql_real_escape_string($rVal, $db) . '"'; } } else { $values[] = '`' . $remoteKey . '` = "' . mysql_real_escape_string($remoteValue, $db) . '"'; } } // only run the insert query if we actually received new values if (count($values)) { $sql = 'DELETE FROM `' . $table . '` ' . 'WHERE `' . $localKey . '` = "' . mysql_real_escape_string($localValue, $db) . '"' . 'AND ( ' . implode(' OR ', $values) . ' )'; mysql_query($sql, $db); } } }