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

How Can I Pass Dynamic limit to model in phalcon controller

Here is my phalcon controller Action in this function i m using phalcon-datatable my query is i want to pass dynamic limit argument to this model $jangads::ViewJangads() Currently I m Passing static limit "limit=>100" i want it as a dynamic that response to server side processing

public function phalconDatatableLoadAction() {
    if ($this->request->isAjax()) {
        $this->setJsonResponse();
        $request = $this->request;
        if (!$request->isPost()) {
            $this->invalid_request = true;
            return;
        }

        $type = $request->getPost("id_type");
        $from_date = $request->getPost("from_date");
        $to_date = $request->getPost("to_date");
        $from_carat = $request->getPost("from_carat");
        $to_carat = $request->getpost("to_carat");
        $machine_id = $request->getpost("machine_id");
        $process_id = $request->getPost("process_id");
        $client_id = $request->getPost("client_id");
        $payment_type = $request->getPost("payment_type");
        $condition = "";

        if ($from_carat === NULL && $to_carat === NULL && $from_date === NULL && $to_date = NULL && $machine_id === NULL && $process_id === NULL && $client_id === NULL) {
            $this->invalid_request = true;
            $jangads = ViewJangads::find(array("limit"=>100, "order" => "id DESC"));  //Here I want to Pass Dynamic Limit in array
            return array('jangads' => $jangads);
        }
        if ($from_date != NULL && $to_date != NULL) {
            $condition = "date_in BETWEEN '$from_date' AND '$to_date'";
        }

        if ($type != NULL) {
            if ($condition != NULL) {
                $condition = " and " . $condition;
            }
            $condition = "type='$type'" . $condition;
        }

        if ($from_carat != NULL && $to_carat != NULL) {
            if ($condition != NULL) {
                $condition = " and " . $condition;
            }
            $condition = "total_weight BETWEEN '$from_carat' and '$to_carat'" . $condition;
        }

        if ($client_id != NULL) {
            if ($condition != NULL) {
                $condition = " and " . $condition;
            }
            $condition = "client_id='$client_id'" . $condition;
        }

        if ($machine_id != NULL) {
            if ($condition != NULL) {
                $condition = " and " . $condition;
            }
            $condition = "find_in_set('$machine_id',machine_id)<>0" . $condition;
        }

        if ($process_id != NULL) {
            if ($condition != NULL) {
                $condition = " and " . $condition;
            }
            $condition = "find_in_set('$process_id',process_id)<>0" . $condition;
        }

        if ($payment_type != NULL) {
            if ($condition != NULL) {
                $condition = " and " . $condition;
            }
            $condition = "payment_type='$payment_type'" . $condition;
        }

        $jangads = ViewJangads ::find(array("conditions" => $condition, "limit"=>100, "order" => "id DESC"));  //Here Also I want to pass dynamic        limit
        $dataTables = new DataTable();
        $dataTables->fromResultSet($jangads)->sendResponse();
        return array('jangads' => $jangads, 'condition' => $condition);
    }
}

if i m not include limit argument in model then it show mysql memory issue bcz i have large database and i don't want to increase memory size so help needed

What"s the problem ? Set your limit in a variable from your post data ...



13.8k
edited Aug '18

When I implemented datatables I also had issues with using normal models. Might be a bug but I"m not on a high enough level yet to make the distinct. So for implementing datatables I used a handwriten query in my controller. The below code is unrelated to yours but it might help you as Im guessing you are trying to get dataTables running.

I used this adapter https://github.com/m1ome/phalcon-datatables

#controller

use \DataTables\DataTable;

public function indexAction()
{
        if($this->request->isAjax()) {
            $query = "  SELECT a.*,
                            b.name AS timetypename,
                            c.name AS username,
                            d.id AS projectid,
                            d.name AS projectname,
                            e.name AS clientname,
                            DATE_FORMAT(a.created_at,'%d-%m-%Y') AS createddate,
                            DATE_FORMAT(a.modified,'%d-%m-%Y') AS modifieddate
                        FROM \PhalconTime\Models\TimeRegistration AS a
                            LEFT JOIN \PhalconTime\Models\TimeType AS b ON a.time_type_id = b.id
                            LEFT JOIN \PhalconTime\Models\User AS c ON a.user_id = c.id
                            LEFT JOIN \PhalconTime\Models\Project AS d ON a.project_id = d.id
                            LEFT JOIN \PhalconTime\Models\Client AS e ON d.client_id = e.id
                        ORDER BY a.id DESC";

            $resultset  = $this->modelsManager->createQuery($query)->execute();

            $dataTables = new DataTable();
            $dataTables->fromResultSet($resultset)->sendResponse();
        }
}

And then in volt I used just javascript.

{% block javascripts %}
<script type="text/javascript">
    $(document).ready(function() {
        $('#timeregistrationTable').DataTable({
            serverSide: true,
            ajax: {
                url: 'https://x.nl/timeregistration/index',
                method: 'POST'
            },
            columns: [
                {data: 'a.id'},
                {data: 'clientname'},
                {
                    'render': function ( data, type, full, meta )
                    {
                        var projectName = full.projectname;
                        var projectId = full.projectid;
                        return '<a href="../project/edit/'+projectId+'" title="update" >'+projectName+'</a>';
                    }
                },
                {data: 'timetypename'},
                {
                    'render': function ( data, type, full, meta )
                    {
                        var bookDate = full.a.book_date.split('-');

                        if(bookDate[0] != 0000) {
                            return bookDate[2]+'-'+bookDate[1]+'-'+bookDate[0];
                        } else {
                            return full.createddate;
                        }
                    }
                },
                {data: 'a.start_time'},
                {data: 'a.end_time'},
                {data: 'a.total_time'},
                {data: 'username'},
                {data: 'modifieddate'},
                {
                    sortable: false,
                    'render': function ( data, type, full, meta )
                    {
                        var registrationid = full.a.id;
                        return '<a class="btn btn-default btn-sm" href="../timeregistration/edit/'+registrationid+'" title="update" ><i class="fa fa-pencil"></i></a> {% if role == 'administrator' %}<a class="btn btn-default btn-sm" href="../timeregistration/confirm/'+registrationid+'" title="delete" ><i class="fa fa-trash"></i></a>{% endif %}';
                    }
                }
            ]
        });
    });
</script>
{% endblock %}

Hope this helps!