/** * Remove relationship. * * @param \Percy\Entity\EntityInterface $entity * @param array $rels * @param array $map * * @return void */ public function deleteRelationship(EntityInterface $entity, array $rels, array $map) { $this->dbal->beginTransaction(); foreach ($rels as $rel) { $delete = $this->query->newDelete(); $delete->from($map['defined_in']['table']); $delete->where(sprintf('%s = :%s', $map['defined_in']['primary'], $map['defined_in']['entity'])); $delete->where(sprintf('%s = :%s', $map['target']['relationship'], 'relationship')); $delete->limit(1); $delete->bindValue('uuid', $entity[$map['defined_in']['entity']]); $delete->bindValue('relationship', $rel); $this->dbal->perform($delete->getStatement(), $delete->getBindValues()); } $this->dbal->commit(); }
/** * Saves a set of data to the table * This function will either insert or update, depending on if the entity passed already has an identifier set. The * generated/passed ID will be returned. * * @param object|array $data Data to save * @param string $table Table to save to * @param string $identifierColumn Identifier column to work against * @return int|string */ public function save($data, $table, $identifierColumn = 'id') { $data = $this->convertToArray($data); if (!empty($data[$identifierColumn])) { $update = $this->queryHandler->newUpdate(); $update->table($table)->cols(array_keys($data))->where($identifierColumn . ' = :' . $identifierColumn)->bindValues($data); $this->db->perform($update->__toString(), $update->getBindValues()); return $data[$identifierColumn]; } else { $insert = $this->queryHandler->newInsert(); $insert->into($table)->cols(array_keys($data))->bindValues($data); $this->db->perform($insert->__toString(), $insert->getBindValues()); $name = $insert->getLastInsertIdName($identifierColumn); return $this->db->lastInsertId($name); } }
/** * @param string $table * @param array $where * * @return bool */ protected final function delete($table, $where = array()) { $sql = 'DELETE FROM ' . $table . ' WHERE 1 '; $binds = array(); if (is_array($where)) { if (empty($where)) { return false; } else { foreach ($where as $key => $value) { $sql .= ' AND ' . $key . ' = :' . $key; $binds[$key] = $value; } } } elseif (is_int($where)) { $sql .= ' AND Id = :Id'; $binds['Id'] = $where; } $this->pdo->perform($sql, $binds); }