/** * Persist relationships to data store. * * @param \Percy\Entity\EntityInterface $entity * @param array $rels * @param array $map * * @return void */ public function relationships(EntityInterface $entity, array $rels, array $map) { $this->dbal->beginTransaction(); foreach ($rels as $rel) { $exists = $this->dbal->fetchOne(sprintf("select * from %s where %s = '%s' and %s = '%s'", $map['defined_in']['table'], $map['defined_in']['primary'], $entity[$map['defined_in']['entity']], $map['target']['relationship'], $rel)); if ($exists !== false) { continue; } $data = [$map['defined_in']['primary'] => $entity[$map['defined_in']['entity']], $map['target']['relationship'] => $rel]; $insert = $this->query->newInsert(); $insert->into($map['defined_in']['table']); $insert->cols($data); $this->dbal->perform($insert->getStatement(), $insert->getBindValues()); } $this->dbal->commit(); }
/** * @param string $statement * @param array $values * @return array */ public function fetchOne($statement, array $values = []) { $return = parent::fetchOne($statement, $values); return is_array($return) ? $return : []; }
/** * Returns a single result based on the criteria * Criteria must be an array, with the DB column the key and the DB value the value * * @param array $criteria Search criteria * @param string $table Table to search against * @return array */ public function find($criteria, $table) { $select = $this->buildSelectQuery($table, $criteria); return $this->db->fetchOne($select->__toString(), $select->getBindValues()); }