FaaPz/PDO

Search function trouble

Closed this issue · 1 comments

I am having some problem with my search function. i want each field to be optional but still let the user choose one or all fields when searching. What am i doing wrong here ?

public function search($id = "", $warehouseid = "", $prices = [])
{
    $id              = (int)$this->container->antixss->xss_clean($id);
    $warehouseid     = (int)$this->container->antixss->xss_clean($warehouseid);
    // $stmt   = $this->pdo->select()->from('sitoo_orders')->where('orderid', 'LIKE', '%'.$id.'%');

    $stmt   = $this->pdo->select()->from('orders')
                ->Where('orderid', 'LIKE', '%'.$id.'%')
                ->orWhere('warehouseid', '=',$warehouseid)
                ->orWhereBetween('moneytotal_gross_all', $prices );

    $stmt   = $stmt->execute();
    $data   = $stmt->fetchall();
    if ($data) {
        return $data;
    }else{
        return false;
    }
}
kwhat commented

What you are trying to do is much easier with the new groupings in 2.x. Here is an example from an unrelated project. I have not fully tested this code.

    /**
     * @param int $limit
     * @param int $offset
     * @param Conditional[]|Grouping[]|null $filter
     * @param string[]|null $order
     *
     * @return Pagination
     */
    public function search($limit = 25, $offset = 0, $filter = null, array $order = null)
    {
        $queryData = $this->database
            ->select(array(
                "zip_code"
            ))
            ->from(static::TABLE)
            ->join(new Join(
                "area_codes",
                new Conditional(
                    static::TABLE . "zip_code",
                    "=",
                    "area_codes.zip_code"
                )
            ))
            ->limit(new Limit($limit, $offset));

        $queryTotal = $this->database
            ->select(array(
                "total" => "COUNT(zipcode)"
            ))
            ->from(static::TABLE);

        if (!empty($filter)) {
            if (!is_array($filter)) {
                $filter = array($filter);
            }

            $queryData->where(new Grouping("OR", $filter));
            $queryTotal->where(new Grouping("OR", $filter));
        }

        if (!empty($order)) {
            if (!is_array($order)) {
                throw new InvalidArgumentException(
                    "The order argument must be an associative array of columns and direction."
                );
            }

            foreach ($order as $column => $direction) {
                $queryData->orderBy($column, $direction);
            }
        }

        $json = new Pagination();
        $json->data = $queryData->execute()->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_UNIQUE, "id");
        $json->total = $queryTotal->execute()->fetchColumn(0);

        return $json;
    }