symfony2 - query builder add condition on field type array -
symfony2 - query builder add condition on field type array -
my question simple : possible add together statment using doctrine , query builder on field type array ?
inside entity have next :
/** * @var array * * @orm\column(name="weekdays", type="array") */ private $weekdays;
in object view array this:
array( 1 => false, 2 => false, 3 => true, 4 => false, 5 => false, 6 => false, 7 => false );
an it's representation 1 time serialize , inserted database :
a:7:{i:1;b:0;i:2;b:0;i:3;b:1;i:4;b:0;i:5;b:0;i:6;b:0;i:7;b:0;}
what seek accomplish :
$q = $this->em->getrepository('acmebundle:notification') ->createquerybuilder('n') ->andwhere('e.weekdays = :day') <-- wrong ->setparameter('day', date('n')) ;
typically result in sql
select * notification weekdays '%i:1;b:1%' -- if date('n') = 1 (monday) select * notification weekdays '%i:7;b:1%' -- if date('n') = 7 (sunday)
and select * notification weekdays '%i:1;b:0%' in case want set ->andwhere('e.weekdays != :day')
sure alter query to:
$q = $this->em->getrepository('acmebundle:notification') ->createquerybuilder('n') ->andwhere("e.weekdays '%:day%'") ->setparameter('day', 'i:'.date('n').';b:1') ;
if wanted construction info little different separate field 7 since weekdays not going change:
//entity /** * @orm\column(type="boolean") */ protected $monday; /** * @orm\column(type="boolean") */ protected $tuesday; //and on
then like:
$weekday = strtolower(date('l')); //lowercase "l" $q = $this->em->getrepository('acmebundle:notification') ->createquerybuilder('n') ->andwhere('n.'.$weekday. '= 1');
symfony2 doctrine2 query-builder
Comments
Post a Comment