db = DatabaseFactory::content(); } /** * {@inheritDoc} */ public function findFiltered(string $type = '', int $limit = 100): array { $sql = 'SELECT er.*, es.name as source_name, es.type as source_type, et.name as target_name, et.type as target_type FROM entity_relations er JOIN entities es ON er.source_entity_id = es.id JOIN entities et ON er.target_entity_id = et.id WHERE 1=1'; $params = []; if ($type !== '') { $sql .= ' AND er.relation_type = :type'; $params['type'] = $type; } $sql .= ' ORDER BY er.strength DESC LIMIT ' . $limit; $stmt = $this->db->prepare($sql); $stmt->execute($params); return $stmt->fetchAll(); } /** * {@inheritDoc} */ public function getTypes(): array { return $this->db->query( 'SELECT relation_type, COUNT(*) as count FROM entity_relations GROUP BY relation_type ORDER BY count DESC' )->fetchAll(); } /** * {@inheritDoc} */ public function getStats(): array { $result = $this->db->query( 'SELECT (SELECT COUNT(*) FROM entity_relations) as total, (SELECT COUNT(DISTINCT source_entity_id) FROM entity_relations) as sources, (SELECT COUNT(DISTINCT target_entity_id) FROM entity_relations) as targets' )->fetch(); return $result !== false ? $result : ['total' => 0, 'sources' => 0, 'targets' => 0]; } /** * {@inheritDoc} */ public function find(int $id): ?array { $stmt = $this->db->prepare( 'SELECT er.*, es.name as source_name, es.type as source_type, et.name as target_name, et.type as target_type FROM entity_relations er JOIN entities es ON er.source_entity_id = es.id JOIN entities et ON er.target_entity_id = et.id WHERE er.id = :id' ); $stmt->execute(['id' => $id]); $result = $stmt->fetch(); return $result === false ? null : $result; } /** * {@inheritDoc} */ public function create(int $sourceId, int $targetId, string $type, float $strength = 1.0): int { $stmt = $this->db->prepare( 'INSERT INTO entity_relations (source_entity_id, target_entity_id, relation_type, strength, created_at) VALUES (:source, :target, :type, :strength, NOW())' ); $stmt->execute([ 'source' => $sourceId, 'target' => $targetId, 'type' => $type, 'strength' => $strength, ]); return (int) $this->db->lastInsertId(); } /** * {@inheritDoc} */ public function update(int $id, string $type, float $strength): bool { $stmt = $this->db->prepare( 'UPDATE entity_relations SET relation_type = :type, strength = :strength WHERE id = :id' ); return $stmt->execute(['id' => $id, 'type' => $type, 'strength' => $strength]); } /** * {@inheritDoc} */ public function delete(int $id): bool { $stmt = $this->db->prepare('DELETE FROM entity_relations WHERE id = :id'); return $stmt->execute(['id' => $id]); } /** * {@inheritDoc} */ public function getTypesList(): array { return ['RELATED_TO', 'PART_OF', 'DEVELOPED_BY', 'INFLUENCED_BY', 'USED_IN', 'WORKS_WITH', 'TEACHES', 'CREATED']; } }