@@ -103,16 +103,23 @@ public function findBySectionCode(string $sectionCode, ?string $localeCode): arr
103103 ;
104104 }
105105
106- public function findByProduct (ProductInterface $ product , string $ channelCode ): array
106+ public function findByProduct (ProductInterface $ product , string $ channelCode, ? \ DateTimeInterface $ date = null ): array
107107 {
108- return $ this ->createQueryBuilder ('o ' )
108+ $ qb = $ this ->createQueryBuilder ('o ' )
109109 ->innerJoin ('o.products ' , 'product ' )
110110 ->innerJoin ('o.channels ' , 'channel ' )
111111 ->where ('o.enabled = true ' )
112112 ->andWhere ('product = :product ' )
113113 ->andWhere ('channel.code = :channelCode ' )
114114 ->setParameter ('product ' , $ product )
115115 ->setParameter ('channelCode ' , $ channelCode )
116+ ;
117+
118+ if (!empty ($ date )) {
119+ $ this ->addDateFilter ($ qb );
120+ }
121+
122+ return $ qb
116123 ->getQuery ()
117124 ->getResult ()
118125 ;
@@ -121,9 +128,10 @@ public function findByProduct(ProductInterface $product, string $channelCode): a
121128 public function findByProductAndSectionCode (
122129 ProductInterface $ product ,
123130 string $ sectionCode ,
124- string $ channelCode
131+ string $ channelCode ,
132+ ?\DateTimeInterface $ date = null
125133 ): array {
126- return $ this ->createQueryBuilder ('o ' )
134+ $ qb = $ this ->createQueryBuilder ('o ' )
127135 ->innerJoin ('o.products ' , 'product ' )
128136 ->innerJoin ('o.sections ' , 'section ' )
129137 ->innerJoin ('o.channels ' , 'channel ' )
@@ -134,8 +142,26 @@ public function findByProductAndSectionCode(
134142 ->setParameter ('product ' , $ product )
135143 ->setParameter ('sectionCode ' , $ sectionCode )
136144 ->setParameter ('channelCode ' , $ channelCode )
137- ->getQuery ()
145+ ;
146+
147+ if (!empty ($ date )) {
148+ $ this ->addDateFilter ($ qb , $ date );
149+ }
150+
151+ return $ qb ->getQuery ()
138152 ->getResult ()
139153 ;
140154 }
155+
156+ private function addDateFilter (QueryBuilder $ qb , \DateTimeInterface $ date ): void
157+ {
158+ $ qb
159+ ->andWhere (
160+ $ qb ->expr ()->orX (
161+ 'o.publishAt is NULL ' ,
162+ 'o.publishAt <= :date '
163+ )
164+ )
165+ ->setParameter ('date ' , $ date );
166+ }
141167}
0 commit comments