Laravel ORM - where, with and paginate - php

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

Related

What is a simple process to define contain with find function

Care to put light to the subject. Issue is that when I do what ever to code I find that code does not go further to retrieve data from table Sectors and table courses. I have applied find() I use CakePHP 4.x to make clear that it is latest version of CakePHP called Strawberry. I also do not understand that why does my code get affected while I apply simple code structure to run my code.
Take a look at code snippet to run a simple find query with CakePHP Strawberry.
$Sectors is not defined but I need to know a way to define and apply $Sectors variable so that I have given value for relative table structure.
Table Does not load because of a problem I do not understand yet but guys help me out I'll update my code and make use of real code that is to determine a structure for code that will work for CakePHP Strawberry.
code:
$this->loadModel('Sectors');
$this->loadModel('Courses');
$Sectors = $Sectors->find('all')->contain('Sectors', 'Courses');
pr($sectors);
$this->contain(['Sectors', 'Courses']);
$this->set(compact('sectors'));
To retrieve data from table so that I get data relation from Sectors and Courses table.
error:
Call to a member function find() on null
Answer is simple but problem is CakePHP would keep single model with multiple query but not multiple model with single query.

Using with and withCount together in laravel eloquent

withCount is a new method of laravel to return count of the relations. I'm trying to use with and withCount together. For example:
Article::with('Comments')->withCount('Comments')->paginate();
Problem I'm facing is, in the results is see for example:
comments_count = 10
comments = []
It returns comments array null. I don't know is it possible to get both the results or not. In some articles on different sites, i see withCount still has some restrictions. But not sure what I'm trying to do is possible or not.
Please help me to know about this issue.
Thank you,
No Need to write with.
Article::withCount('Comments')->paginate();

apigility - What alternatives I can use for Zend\Paginator\Paginator in Collections?

In several endpoints I'm doing with Apigility, I want retrieve all the possible results without pagination. Is there a class to replace Paginator in Collection?
To receive a response in Hal without pagination you should simply return an array from your queries instead of an instance of Zend\Paginator\Paginator.
For those who happen to find this question just as I did. You can edit the number of items per page in the module.config.php file. It's set as the value "page_size". Changing it to a number larger than the total amount in your collection will let you receive all the results in one page.
Admittedly not the cleanest of solutions, but it worked for me.

CakePHP Paginate() the results from a find subquery?

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.

Using Magento's addFieldToFilter to filter by existing columns

Hypothetical situation. I want to populate/load a sales/order collection with every order where the grand_total is equal to the total_paid. I know I can use addFieldToFilter to filter by a specific value, but is it possible to use this method to filter by other database values. If not, is there any data access object in the Magento system that allows this.
$orders = Mage::getModel('sales/order');
$orders = $orders->getCollection();
$orders->addFieldToFilter('total_paid',Array('eq'=>30)); //would find all the orders that were 30
//syntax to find all the orders whose total_paid value is equal to it's grand_total attribute
//????????
The non-eav SQL concept I’m grasping for is:
SELECT * FROM Orders o WHERE o.total_paid = o.grand_total
Is this possible to do purely with object method calls, or do I need to do post-load filtering?
How do other ORM systems handle this?
I was waiting for a definite answer to this, but seeing as nothing's come up I might as well share what I've found out.
I traced addFieldToFilter and apparently every value you use ends up quoted so you can't really "hack" this by using the name of a column.
In fact I don't see any way to do this, at least not within this class.
Realistically I think you'll need to go with post-load filtering, at least for now. This of course is the very definition of inefficient code, but then again if computational efficiency was a priority, you wouldn't be using Magento in the first place...
Just had to address this in one of my projects and came across this question while searching for a better resolution. My hack looks like this:
$orders = Mage::getModel('sales/order')->getCollection();
$orders->getSelect()
->where('total_paid=grand_total');
It annoys me that such a basic functionality of databases is overlooked by Magento's ORM.
There's a "polite" workaround for that.
See my example below where I am trying to filter where created_at <= expire_at.
Mage::getSingleton('foo/bar')->getCollection()
->addExpressionFieldToSelect('expired', '(created_at >= expire_at)', ['created_at' , 'expire_at'])
->addFilter('(created_at >= expire_at)', 0)
It's stupid, but still solves the problem using the class.
PS: I hope that 11 years after publishing this question you've solved the problem. :D (Just a joke. I love your work.)

Categories