Using the PHP Bigcommerce library, I want to get a list of all my orders with the newest order on top. This is what I'm using to get my orders:
$filter = ['status_id' => 11];
$orders = Bigcommerce::getOrders($filter);
It's listing all the orders from oldest to newest. On the API documentation, it says that there is an order filter called 'date_created':
"If your application relies on the arrival of new orders you may need to check both date_created and status fields (or status_id)."
I've tried passing dates through the filter like so:
$filter = ['date_created' => '01/04/2013', 'status_id' => 11];
And other formats, like 01-04-13, etc. But none of them return any orders for me.
Actually, it seems to be a bug in the way it is documented. You need to use the min_date_created field for filtering instead of date_created. The date should be RFC format "Tue, 20 Nov 2012 00:00:00 +0000"
For instance -
$filter = array('min_date_created' => 'Tue, 20 Nov 2012 00:00:00 +0000');
$orders = Bigcommerce::getOrders($filter);
print_r($orders);
This will work. Cheers!
How about using the sort attribute?
$filter = ['sort' => 'date_created:desc'];
Source: Pagination session of List Orders documentation.
$orders = Bigcommerce::getOrders(array('sort' => 'date_created:desc'));
print_r($orders);
This Will Work To show order in descending way.
Related
I'm sure I have done something like this before, but can't find it and google not being helpful.
Using Phalcon model if possible, I want to select the items from a table whose ID appears the most - i.e. 10 most popular items ordered by popularity. Is this possible using Model::find("conditions")? I do I have to use PHQL for this?
using model::find
Model::find([
'columns' => 'id,count(id) as counter',
'group' => 'id',
'order' => 'counter DESC'
]);
PHQL:
$this->modelsManager->executeQuery('SELECT count(id) AS counter,id FROM ModelName GROUP BY id ORDER BY counter DESC');
find() does have a group clause, but I don't think it's possible to do what you want because you also need to do a count.
Talal's answer is close, but won't work if you want a list of model objects.
Something like this should work:
$Results = $this->modelsManager->executeQuery('SELECT * FROM ModelName GROUP BY id ORDER BY count(id) DESC LIMIT 10');
$Results->setHydrationMode(\Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS);
Setting the hydration mode may not be necessary, as Phalcon may default to that mode based on the fact the query is asking for *.
I've been playing around with the RavenDB Northwind database, and am having trouble getting orders sorted by Freight.
My index:
Map = orders => from o in orders
select new {
o.Freight
};
IndexSortOptions.Add(x => x.Freight, SortOptions.Double);
Indexes.Add(x => x.Freight, FieldIndexing.NotAnalyzed);
My query:
return sess.Query<Order>("Orders/ByFreight")
.OrderByDescending(x => x.Freight)
.Select(x => x.Freight);
It get the following order back:
[
32.38,
11.61,
65.83,
41.34 ... ]
Which is clearly not correct. In the studio, I can define the order to be by Freight, and it orders just fine. However, if in the studio I specify the range as over Freight_Range, I get these same results back. It appears to me that Raven is selecting the Freight_Range field to sort by rather than the Freight field. Why?
Remove the line starting with Indexes.Add. You don't need to do that.
Use this syntax instead of the IndexSortOptions:
Sort(x=> x.Freight, SortOptions.Double);
Make sure your Freight field is indeed a double as defined on your Order class.
I have a problem concerning CakePHP SQL-queries. I need to fetch products from the database where shop_id is given and then count the products. All I need is Product.url and its count.
This will do the trick in plain SQL:
SELECT url,COUNT(*) as count FROM products GROUP BY url ORDER BY count DESC;
This one I used to get all products relating to shops:
$this->Product->find('all', array('conditions' => array('Product.shop_id'=>$id)));
That works correctly, but I need to convert that SQL-query above to CakePHP.
I tried something like this:
$this->Product->find('count', array('conditions' => array('Product.shop_id'=>$id),
'fields'=>array('Product.url','Product.id'),
'order'=>'count DESC',
'group'=>'Product.url'));
That returns only an int. But if I run that SLQ-query presented above in mysql server, I get two columns: url and count. How do I get the same results with CakePHP?
You can try this:
$data = $this->Post->query(
"SELECT COUNT(id),MONTH(created) FROM posts GROUP BY YEAR(created), MONTH(created);"
);
The most easiest way to do this:
$this->Product->query("SELECT url,COUNT(*) as count FROM products GROUP BY url ORDER BY count DESC;");
...at least for me.
Try the following code:
$this->Product->virtualFields['CNT_PRODUCTS'] = 0;
$this->Product->find('count', array('conditions' => array('Product.shop_id' => $id),
'fields' => array('Product.id', 'Product.url', 'count(*) as CNT_PRODUCTS'),
'order' => array('CNT_PRODUCTS' => 'DESC'),
'group' => 'Product.url'));
I'm using group_by in a DBIx::Class resultset search. The result returned for each group is always the row in the group with the lowest id (i.e the oldest row in the group). I'm looking for a way to get the row with the highest id (i.e. the newest row in the group) instead.
The problem is fundamentally the same as this:
Retrieving the last record in each group
...except that I'm using DBIx::Class not raw SQL.
To put the question in context:
I have a table of music reviews
review
------
id
artist_id
album_id
pub_date
...other_columns...
There can be multiple reviews for any given artist_id/album_id.
I want the most recent reviews, in descending date order, with no more than one review per artist_id/album_id.
I tried to do this using:
$schema->resultset('Review')->search(
undef,
{
group_by => [ qw/ artist_id album_id / ],
order_by => { -desc => 'pub_date' },
}
);
This nearly works, but returns the oldest review in each group instead of the newest.
How can I get the newest?
For this to work you are relying on broken database behaviour. You should not be able to select columns from a table when you use group by unless they use an aggregate function (min, max etc.) or are specified in the group by clause.
In MySQL, even the manual admits this is wrong - though it supports it.
What I think you need to do is get the latest dates of the reviews, with max(pub_date):
my $dates = $schema->resultset('Review')->search({},
{
select => ['artist_id', 'album_id', {max => 'pub_date'}],
as => [ qw(artist_id album_id recent_pub_date) ],
group_by => [ qw(artist_id album_id) ],
}
);
Then loop through to get the review:
while (my $review_date = $dates->next) {
my $review = $schema->resultset('Review')->search({
artist_id => $review_date->artist_id,
album_id => $review_date->album_id,
pub_date => $review_date->get_column('recent_pub_date'),
})->first;
}
Yep - it's more queries but it makes sense - what if two reviews are on the same date - how should the DB know which one to return in the select statement?
I have a PrivateMessage class and I want to get list of PMs for user sorted chronologically either by CreationDate or LastAnswerDate (depending on which is more recent) using Criteria API.
How to sort by max of these two properies in Criteria API? My code looks similar to following:
var dc = DetachedCriteria.For<PrivateMessage>();
...
dc.AddOrder(new Order("???");
return (IList<PrivateMessage>)FindAll(typeof(PrivateMessage), dc);
CreationDate is DateTime and LastAnswerDate is DateTime?.
Thanks!
Order.Desc(
Projections.Conditional(
Restrictions.GtProperty("CreationDate", "LastAnswerDate"),
Projections.Property("CreationDate"),
Projections.Property("LastAnswerDate"))))