Skip to content

Instantly share code, notes, and snippets.

@milo
Created January 5, 2016 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milo/dc61a7e347bb1632d0f0 to your computer and use it in GitHub Desktop.
Save milo/dc61a7e347bb1632d0f0 to your computer and use it in GitHub Desktop.
<?php
class DomainsMapper extends Nextras\Orm\Mapper\Dbal\DbalMapper
{
public function builder()
{
$builder = new Nextras\Dbal\QueryBuilder\QueryBuilder($this->connection->getDriver());
$builder->from("
(
WITH RECURSIVE cte_host AS(
SELECT
host.id,
host.host_id,
host.domain_id,
host.parent_domain_id,
host.alias_for_id,
host.label,
host.label AS label_expanded
FROM
dns.host host
WHERE
host.parent_domain_id IS NULL
UNION ALL
SELECT
host.id,
host.host_id,
host.domain_id,
host.parent_domain_id,
host.alias_for_id,
host.label,
host.label || '.' || cte.label_expanded
FROM
cte_host cte
JOIN dns.host host ON cte.domain_id = host.parent_domain_id
)
SELECT
cte_host.id,
cte_host.host_id,
cte_host.domain_id,
cte_host.parent_domain_id,
cte_host.alias_for_id,
(
SELECT
cte_host_2.label_expanded
FROM
cte_host cte_host_2
WHERE
cte_host_2.id = cte_host.alias_for_id
) AS alias_for,
cte_host.label,
cte_host.label_expanded
FROM
cte_host
)
", Nextras\Orm\Mapper\Dbal\QueryBuilderHelper::getAlias($this->getTableName()));
return $builder;
}
}
@milo
Copy link
Author

milo commented Jan 5, 2016

Zkouším si Nextras ORM a data sestavuji pomocí CTE. Ale vyskočí na mě výjimka, když se ORM snaží načíst cizí klíče.

Jak se dá snadno zapsat, jaké mají výsledné sloupce cizí klíče?

@hrach
Copy link

hrach commented Jan 5, 2016

Overridni IMaper::createStorageReflection(), vrat vlastni refleciton s pretizenymi metodami. https://github.com/nextras/orm/blob/4a6a26cdf508657f3099e1c4246830ad3a6cd801/src/Mapper/Dbal/StorageReflection/StorageReflection.php#L309-L322
Prave jsem je zmenil na protected O:-)

@milo
Copy link
Author

milo commented Jan 5, 2016

Díky za hint. Chci přetížit jen jednu relaci. Tohle CTE alias domains. U getColumns() vs. getForeignKeys() nevím, jaký je rozdíl $this->storageName vs. $table.

@milo
Copy link
Author

milo commented Jan 5, 2016

Tím chci říct, že v tom zatím dost plavu :o)

@hrach
Copy link

hrach commented Jan 5, 2016

Je to tam nejake zprasene. Oboji je proste defaulni nazev storage, stejny jako mas v mapper $this-getTableName. Kdyz si to pretizis jen v tom jednom konkretnim mapperu, bude to platit jen pro nej. Ten parametr je u obou metod to stejny, jen jednou je predany, podruhe ne. Mel bych to do 2.0 opravit.

@hrach
Copy link

hrach commented Jan 10, 2016

Tridu StorageReflection jsem prepsal. Aktualne by melo stacit podedit 4 metody v ni a vracet prazdne hodnoty.

class MyStrageReflection extends Nextras\Orm\Mapper\Dbal\StorageReflection
{
    public function getStoragePrimaryKey()
    {
        return ['id'];
    }
    protected function getDefaultMappings()
    {
        return [];
    }
    protected function getDefaultModifiers()
    {
        return [];
    }
}

a posleze v tomto tvem mapper

    protected function createStorageReflection()
    {
        return new MyStrageReflection(
            $this->connection,
            $this->getTableName(),
            $this->getRepository()->getEntityMetadata()->getPrimaryKey(),
            $this->cacheStorage
        );
    }

@milo
Copy link
Author

milo commented Jan 15, 2016

@hrach Díky. S tím se dělá lépe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment