CakePHP Paginate() the results from a find subquery? - php

This is a follow-up to an earlier question here: CakePHP find statement for a sql query involving two models
I wasn't sure whether to add it on to there, but I figured this is a new question in itself. My apologies if this isn't correct!
Given a cakephp find subquery, is it possible to use paginate() on the results of the find? from the cake documentation, it seems that the paginate() function only works on Models, not on an array of data.
Thank you!

if you already have all the data in the array, I suggest output all that to your page (not the same as showing all the data) and use javascript to paginate it (just use a javascript grid plugin out there): more responsive app and less load on the server.

Related

How to implement pagination along with sorting results in Laravel

I'm pretty new to Laravel, and still exploring it's features and possibilities, and I've came across a strange issue that I don't quite understand.
First of all, I'm using Laravel 5.0 and I'm trying to make pagination of results that came from model alongside with sorting them.
Here is the code that works, but without sorting:
News::paginate(5);
And here is what I need, but doesn't work:
News::paginate(5)->orderByDesc('published_at');
I've tried doing it like so:
News::all()->sortByDesc('published_at')->paginate(5);
But then, when I call render() method inside view it throws error. I would like to use model's approach since it's handy and few more things depend on it, and not query builder method. Also, would like, if possible to avoid manual or any kind of custom pagination. Thanks
You should write this. This will solve your problem
News::orderBy('published_at','desc')->paginate(5);

Cakephp Find Convert to Sql Syntax

Cakephp find data in controller also can with sql syntax,
example :
$this->Post->find('all');
in sql :
select * from posts
convert query to cakephp find syntax is available at http://dogmatic69.com/sql-to-cakephp-find-converter
but whether there is any link for me if I wanna convert my cakephp find to query..
Thanks in advance...
If I got what you are asking, here is the answer
SQL calls that you can’t or don’t want to make via other model
methods can be made using the model’s query() method (though this
should only rarely be necessary).
query() uses the table name in the query as the array key for the returned data, rather than the model name. For example:
$this->POST->query("SELECT* from posts;");
Here is the link for cakephp documentation

Laravel ORM - where, with and paginate

can someone help me with the following laravel ORM questions
I have 2 models ProductGroup and Product. ProductGroup hasMany Products and is linked via productGroup.product_id and product.id
I want to return a set of results with pagination. Quite simple wish, isn't it?
My current code is
$result = ProductGroup::where("name", "=", "laptop")
->with(array("product"=>function($q){
$q->select("id","name");
}))
->get(array("id","product_id","name","created_timestamp"));
It works but I cannot add ->paginate() otherwise it will throw the following error
Call to undefined method Illuminate\Database\Eloquent\Collection::paginate()
What is the best way of doing the pagination? I can create the pagination manually (via Paginator::make )but I don't think it's good because it still returns the whole set of data (slowing down the traffic). I can use raw query to limit the result but it is also troublesome because I need to again manually calculate the total result
I am using Laravel 4.2 with MySQL
Thanks for any help
I found a solution, which could help others
Wrong way
->get((array("id","product_id","name","created_timestamp"))
->paginate(3)
Correct way
->paginate(3, (array("id","product_id","name","created_timestamp"))
Feel free to downvote it if it is wrong

How to sort/order Redbean sharedList

In my application, member entities choose a from a pre-defined set of question entities.
I save and iterate over them as a shared list ($member->sharedQuestion).
Now I need to rank them, so I add another column via the link bean (member_question) called 'position'.
My question is - can I make redbean retrieve the questions ORDERed by the column 'position'?
I currently do a
foreach($member->sharedQuestion as $question){.......}
I know I could get the array property and run it through a custom sort handler before I start iterating, but that seems expensive.
Does anyone know of a Redbean method to append some sql (i.e. "ORDER BY position") to a sharedList for example?
Despite having read the Redbean documentation many times, I had missed the (very simple) solution.
Prepending the ->with() method applies extra sql to the query. So what I need to do is;
foreach($member->with("ORDER BY position")->sharedQuestion as $question){.......}
and my problem is elegantly solved!

Doctrine - getChildren() with conditions?

I'm trying to write a basic plugin for the Symfony based CMS Diem.
I'm trying to list of the child pages for the current page, which I have managed to do:
$page = $this->getPage();
$this->subpages = $page->getNode()->getChildren();
However, I'm unsure of the syntax to use in order to filter the child records with conditions.
I would actually like to get just the records where the is_active field == 1.
I've looked at the documentation and I think I need to use the setBaseQuery method, but I could really do with an example to get me started.
Can anyone help?
Any advice appreciated.
Thanks.
Use DQL and prefetch what you need. I strongly recommend using DQL for everything accept a simple find
Or you can simple get it from the repository using the build in "magic" functions.
Something like:
Doctrine::em()->getRepository('Models\SubPage')->findByIsActive(1);
Check out the documentation

Categories