protected function beforeGetListViewData($callbackRedBean, $callbackSQL, $start = null, $rowPerPage = null, $keyword = null, $sortField = null, $sortOrder = null)
    {
        try {

            // Paging
            if ($start != null && $rowPerPage != null) {
                $limit = " LIMIT $start,$rowPerPage";
            } else {
                $limit = "";
            }

            if ($this->sql != null) {
                // Custom SQL

                $list = [];
                $callbackSQL($this->sql, $limit, $this->bindingData);
            } else {

                $bindingData = $this->bindingData;

                // For Find All Clause
                if ($this->findAllClause != null) {
                    $findClause = " 1 = 1 " . $this->findAllClause;
                } else if ($this->findClause != null) {
                    $findClause = $this->findClause;
                } else {
                    $findClause = " 1 = 1 ";
                }


                // Build a searching clause
                if ($keyword != null) {
                    $searchClause = $this->buildSearchingClause();
                    $searchData = $this->buildSearchingData($keyword);

                    $findClause = $searchClause . $findClause;

                    // Merge Array
                    $bindingData = array_merge($searchData, $bindingData);
                }

                // Sorting
                if ($sortField != null) {
                    $fakeSelect = "SELECT * FROM louislamcrud_fake_table WHERE ";

                    $parser = new PHPSQLParser($fakeSelect . $findClause);

                    $sqlArray = $parser->parsed;

                    $sqlArray["ORDER"][0]["expr_type"] = "colref";
                    $sqlArray["ORDER"][0]["base_expr"] = $sortField;
                    $sqlArray["ORDER"][0]["sub_tree"] = null;
                    $sqlArray["ORDER"][0]["direction"] = $sortOrder;

                    $findClause = str_replace($fakeSelect, "", (new PHPSQLCreator($sqlArray))->created);
                }

                $callbackRedBean($this->tableName, $findClause, $limit, $bindingData);
            }
        } catch (\RedBeanPHP\RedException\SQL $ex) {

                throw $ex;
        } catch(\Exception $ex) {
            // TODO: This should be for not existing table only, not other exceptions.

            // If the table is not existing create one, create the table and run this function again.
            $this->createTable();
            $this->beforeGetListViewData($callbackRedBean, $callbackSQL, $start, $rowPerPage, $keyword, $sortField, $sortOrder);
        }

    }
protected function beforeGetListViewData($callbackRedBean, $callbackSQL, $start = null, $rowPerPage = null, $keyword = null, $sortField = null, $sortOrder = null)
    {
        try {

            // Paging
            if ($start != null && $rowPerPage != null) {
                $limit = " LIMIT $start,$rowPerPage";
            } else {
                $limit = "";
            }

            if ($this->sql != null) {
                // Custom SQL

                $list = [];
                $callbackSQL($this->sql, $limit, $this->bindingData);
            } else {

                $bindingData = $this->bindingData;

                // For Find All Clause
                if ($this->findAllClause != null) {
                    $findClause = " 1 = 1 " . $this->findAllClause;
                } else if ($this->findClause != null) {
                    $findClause = $this->findClause;
                } else {
                    $findClause = " 1 = 1 ";
                }


                // Build a searching clause
                if ($keyword != null) {
                    $searchClause = $this->buildSearchingClause();
                    $searchData = $this->buildSearchingData($keyword);

                    $findClause = $searchClause . $findClause;

                    // Merge Array
                    $bindingData = array_merge($searchData, $bindingData);
                }

                // Sorting
                if ($sortField != null) {
                    $fakeSelect = "SELECT * FROM louislamcrud_fake_table WHERE ";

                    $parser = new PHPSQLParser($fakeSelect . $findClause);

                    $sqlArray = $parser->parsed;

                    $sqlArray["ORDER"][0]["expr_type"] = "colref";
                    $sqlArray["ORDER"][0]["base_expr"] = $sortField;
                    $sqlArray["ORDER"][0]["sub_tree"] = null;
                    $sqlArray["ORDER"][0]["direction"] = $sortOrder;

                    $findClause = str_replace($fakeSelect, "", (new PHPSQLCreator($sqlArray))->created);
                }

                $callbackRedBean($this->tableName, $findClause, $limit, $bindingData);
            }
        } catch (\RedBeanPHP\RedException\SQL $ex) {

                throw $ex;
        } catch(\Exception $ex) {
            // TODO: This should be for not existing table only, not other exceptions.

            // If the table is not existing create one, create the table and run this function again.
            $this->createTable();
            $this->beforeGetListViewData($callbackRedBean, $callbackSQL, $start, $rowPerPage, $keyword, $sortField, $sortOrder);
        }

    }
protected function beforeGetListViewData ( $ callbackRedBean , $ callbackSQL , $ start = null , $ rowPerPage = null , $ keyword = null , $ sortField = null , $ sortOrder = null ) { try { // Paging if ( $ start != null && $ rowPerPage != null ) { $ limit = " LIMIT $start,$rowPerPage" ; } else { $ limit = "" ; } if ( $ this -> sql != null ) { // Custom SQL $ list = [ ] ; $ callbackSQL ( $ this -> sql , $ limit , $ this -> bindingData ) ; } else { $ bindingData = $ this -> bindingData ; // For Find All Clause if ( $ this -> findAllClause != null ) { $ findClause = " 1 = 1 " . $ this -> findAllClause ; } else if ( $ this -> findClause != null ) { $ findClause = $ this -> findClause ; } else { $ findClause = " 1 = 1 " ; } // Build a searching clause if ( $ keyword != null ) { $ searchClause = $ this -> buildSearchingClause ( ) ; $ searchData = $ this -> buildSearchingData ( $ keyword ) ; $ findClause = $ searchClause . $ findClause ; // Merge Array $ bindingData = array_merge ( $ searchData , $ bindingData ) ; } // Sorting if ( $ sortField != null ) { $ fakeSelect = "SELECT * FROM louislamcrud_fake_table WHERE " ; $ parser = new PHPSQLParser ( $ fakeSelect . $ findClause ) ; $ sqlArray = $ parser -> parsed ; $ sqlArray [ "ORDER" ] [ 0 ] [ "expr_type" ] = "colref" ; $ sqlArray [ "ORDER" ] [ 0 ] [ "base_expr" ] = $ sortField ; $ sqlArray [ "ORDER" ] [ 0 ] [ "sub_tree" ] = null ; $ sqlArray [ "ORDER" ] [ 0 ] [ "direction" ] = $ sortOrder ; $ findClause = str_replace ( $ fakeSelect , "" , ( new PHPSQLCreator ( $ sqlArray ) ) -> created ) ; } $ callbackRedBean ( $ this -> tableName , $ findClause , $ limit , $ bindingData ) ; } } catch ( \ RedBeanPHP \ RedException \ SQL $ ex ) { throw $ ex ; } catch ( \ Exception $ ex ) { // TODO: This should be for not existing table only, not other exceptions. // If the table is not existing create one, create the table and run this function again. $ this -> createTable ( ) ; $ this -> beforeGetListViewData ( $ callbackRedBean , $ callbackSQL , $ start , $ rowPerPage , $ keyword , $ sortField , $ sortOrder ) ; } }
Prepare the SQL or parameter for RedBean

TODO: Sort by multiple fields?

@param int $start
@param int $rowPerPage
@param string $keyword
@param string $sortField
@param null $sortOrder ASC/DESC
@param callable $callbackRedBean
@param callable $callbackSQL
@throws \RedBeanPHP\RedException\SQL
Prepare the SQL or parameter for RedBean