Last active
September 22, 2015 21:37
-
-
Save pedrorvidal/8e09b07fecbe5245533e to your computer and use it in GitHub Desktop.
Caso Symfony2 Not working consulta a banco
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * @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)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * @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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;