Skip to content

Instantly share code, notes, and snippets.

@pedrorvidal
Last active September 22, 2015 21:37
Show Gist options
  • Save pedrorvidal/8e09b07fecbe5245533e to your computer and use it in GitHub Desktop.
Save pedrorvidal/8e09b07fecbe5245533e to your computer and use it in GitHub Desktop.
Caso Symfony2 Not working consulta a banco
/**
*
* @return \Entity\BackgroundHome
*/
public function getBackgroundHome()
{
$hash = $this->getParam()->get('hash');
$backgroundId = $this->getParam()->getInt('id');
$isBackgroundPreview = ($this->getParam()->get('preview') == 'background');
if ($isBackgroundPreview) {
if ($this->verifyHash($hash)) {
$this->getTpl()->addGlobal('pre_visualizacao_bg', true);
return $this->getEm()
->getRepository('Entity\BackgroundHome')
->find($backgroundId);
} else {
throw new \Exception\NotFoundException;
}
}
return $this->getEm()
->getRepository('Entity\BackgroundHome')
->getBackgroundHomeFunc(1);
// ->findOneBy(array('publicado' => 1));
}
/**
*
* @param $publicado
* @return array
*/
public function getBackgroundHomeFunc($publicado = 1)
{
$dql = "SELECT e FROM {$this->entity} e WHERE 1 = 1";
$parametros = array();
$dql .= " AND (now() BETWEEN :data_inicial AND :data_final )";
if ($publicado !== "") {
$dql .= " AND e.publicado = 1";
$parametros['publicado'] = 1;
}
//Executa a query e passa os parâmetros
$query = $this->getEntityManager()->createQuery($dql);
$query->setParameters($parametros);
return $query->getResult();
}
$query = $this->createQueryBuilder('b')
->andWhere('b.publicado = 1')
->andWhere('b.dataInicial < :today')
->andWhere('b.dataFinal > :today OR b.dataFinal IS NULL');
$today = new \DateTime('now');
return $query->orderBy('b.dataInicial', 'DESC')
->setParameter('today', $today)
->getQuery()
->useQueryCache(TRUE)
->useResultCache(TRUE, CACHE_LIFE_TIME);
@luismulinari
Copy link

public function getBackgroundHomeFunc($publicado = 1)
    {
        $qb = $this->createQueryBuilder('e')
            ->andWhere('e.publicado = :publicado')
            ->andWhere('e.dataInicial >= CURRENT_DATE()')
            ->andWhere('e.dataFinal <= CURRENT_DATE()')
            ->setParameter('publicado', $publicado);

        return $qb->getQuery()->getResult();
    }

Pedro, acredito que o que você esteja procurando seja algo nessa linha. Vale lembrar que 'dataInicial' e 'dataFinal' são o nome do atributo da entidade. Uma sugestão que eu faço seria substituir o 'e' do alias da query por o nome real da entidade. Espero ter ajudado

@pedrorvidal
Copy link
Author

CREATE TABLE tb_background_home
(
id_background_home bigserial NOT NULL,
dt_inicial timestamp without time zone NOT NULL,
dt_final timestamp without time zone,
dt_cadastro timestamp without time zone NOT NULL,
st_publicado integer NOT NULL DEFAULT 0,
no_nome character varying(100) NOT NULL,
id_imagem bigint NOT NULL,
CONSTRAINT pk_background_home PRIMARY KEY (id_background_home),
CONSTRAINT fk_background_home_imagem FOREIGN KEY (id_imagem)
REFERENCES tb_imagem (id_imagem) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT uk_background_home_background_home UNIQUE (id_background_home),
CONSTRAINT ck_background_home_publicado CHECK (st_publicado = ANY (ARRAY[0, 1]))
)
WITH (
OIDS=FALSE
);
ALTER TABLE tb_background_home
OWNER TO postgres;

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