We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

NOT IN

hi


$day1 = date("m-j-y", strtotime("-7 days"));
        $day2 = date("m-j-y", strtotime("-1 days"));

         $weekvideos = Videos::find(array(
            "adddate BETWEEN :date: AND :date2:",
            'category NOT IN (5)',
            "bind" => array(
                'date' => $day1,
                'date2'=> $day2 
            ),
            "order" => "RAND()",
            "limit" => 52
        ));

'category NOT IN (5)' does not work and why?



93.7k
Accepted
answer

The first parameter of find() is the where clause. You can use 'conditions' key also. Full spec here: https://docs.phalcon.io/en/latest/reference/models.html#finding-records

To make your query work change to:

         $weekvideos = Videos::find(array(
            "adddate BETWEEN :date: AND :date2: AND category NOT IN (5)",
            // 'category NOT IN (5)', // this belongs above
            "bind" => array(
                'date' => $day1,
                'date2'=> $day2 
            ),
            "order" => "RAND()",
            "limit" => 52
        ));

Thank u


        $day1 = date("m-j-y", strtotime("-7 days"));
        $day2 = date("m-j-y", strtotime("-1 days"));

        $categorynum = ["5"];
        $arraycatnum = array_values($categorynum);

         $weekvideos = Videos::find(array(
            "adddate BETWEEN :date: AND :date2: AND category NOT IN ({category:array})",
            "bind" => array(
                'date'  => $day1,
                'date2' => $day2,
                'category' => $arraycatnum
            ),
            "order" => "RAND()",
            "limit" => 52
        ));

The first parameter of find() is the where clause. You can use 'conditions' key also. Full spec here: https://docs.phalcon.io/en/latest/reference/models.html#finding-records

To make your query work change to:

        $weekvideos = Videos::find(array(
           "adddate BETWEEN :date: AND :date2: AND category NOT IN (5)",
           // 'category NOT IN (5)', // this belongs above
           "bind" => array(
               'date' => $day1,
               'date2'=> $day2 
           ),
           "order" => "RAND()",
           "limit" => 52
       ));

Good job on binding array values for the IN clause.