Timber:get_posts() with orderby meta_value sorting by post type - custom-post-type

I am trying to get a collection of 2 different post types sorted by their start date. The two custom post types are just about identical in structure. They both have a message_date property. When I tried to get them and sort by the message_date, it sorts them first by post_type and then by message_date. Here's the code:
$message_args = array(
'post_type' => array('message', 'podcast'),
'orderby' => array('meta_value' => 'DESC', 'post_type' => 'ASC'),
'meta_key' => 'message_date',
);
$messages = Timber::get_posts( $message_args );
I have double checked the formatting of the values and they are all yyyymmdd. I've tried it without sorting by post_type as well and I get the same result. Is there an easy way to override this?

Related

postgres format() - nodejs pg-format - using with UPDATE

I'm tring to update multiple rows at once with Postgres.
This query works
const updateQuery = `
UPDATE dating.photos
SET
ordering = nv.ordering,
profile = nv.profile
FROM
( VALUES
('ddb73491-b582-4ff5-afb4-e57e31917b03', 4, false),
('9cd57b09-17ff-4b88-b08f-b1ef02ec8c1d', 2, false),
('02d9d7e2-2601-4e6f-b7d4-ddebdbee420d', 3, false),
('5e04ff58-8f85-43f8-a4ca-dfde85da9e6f', 1, true)
) AS nv (photoname, ordering, profile)
WHERE photos.photoname = nv.photoname`
I'm now trying to load data into the UPDATE using pg-format so the data is sanitised.
Note: I've seen pg-format (postgres format()) used with SELECT and INSERT. I'm not 100% sure it can be used with UPDATE.
I've try loading an array like this:
const myNestedArray = [
[
"ddb73491-b582-4ff5-afb4-e57e31917b03",
1,
true
],
[
"9cd57b09-17ff-4b88-b08f-b1ef02ec8c1d",
2,
false
]
]
I've try loading an array like this:
const myNestedArray = [
(
"ddb73491-b582-4ff5-afb4-e57e31917b03",
1,
true
),
(
"9cd57b09-17ff-4b88-b08f-b1ef02ec8c1d",
2,
false
)
]
This is the query I'm using... you can see the %L for the values
// Update Photo Order
const query = format(
`
UPDATE dating.photos
SET
ordering = nv.ordering,
profile = nv.profile
FROM
( VALUES %L
) AS nv (photoname, ordering, profile)
WHERE photos.photoname = nv.photoname`,
myNestedArray
)
I get back the error "Ordering Photos Errors: error: column "ordering" is of type smallint but expression is of type text"
... it appears it not matching up the values to columns correctly.
Any thoughts on how to get this working?
thx Adam
I also had the same problem with pg-format, if it's going to use for formatting nested arrays data that would have a different data type on each value, I think the pg-format couldn't handle it

Multiple values in WHERE clause using sqldf in R

I am trying to query multiple values in the WHERE clause, using sqldf in R. I have the following query, however, it continues to throw an error. Any help would be appreciated.
sqldf("SELECT amount
from df
where category = 'description' and 'original description'")
ERROR: <0 rows> (or 0-length row.names)
You just need to use in condition
sqldf("SELECT amount
from df
where category in ('description','original description')")
If you want to use like operator, you need to use OR instead of AND.(not sure what other entries are in the category, if you don't have any other category that has "description" in its name, the following might be enough
sqldf("SELECT amount from df where category LIKE 'descriptio%'")
You need to define each where clause explicitly, so
SELECT amount FROM df WHERE category = 'description' OR category = 'original description'
You can pass in multiple values, it's done with the IN operator:
SELECT amount FROM df WHERE category IN ( 'description', 'original description' )

Cake PHP Use Conditions and Order By in Paginate

I'm trying to use Conditions and Order By in cake PHP paginate function of the Paginator helper
This is my code which I'm using
$this->set('equipment', $this->Paginator->paginate('Equipment',array('conditions' => array('Equipment.equipment_type_id' => 19), 'order' => array('Equipment.problem_count', 'Equipment.barcode DESC'))));
But it gives me this error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'conditions' in 'where clause'
SQL Query: SELECT Equipment.id, Equipment.equipment_type_id, Equipment.barcode, Equipment.problem_count, Equipment.description, Equipment.created, Equipment.modified, EquipmentType.id, EquipmentType.name, EquipmentType.created, EquipmentType.modified FROM unlibike.equipment AS Equipment LEFT JOIN unlibike.equipment_types AS EquipmentType ON (Equipment.equipment_type_id = EquipmentType.id) WHERE conditions = (19) AND order IN ('Equipment.problem_count', 'Equipment.barcode DESC') LIMIT 20
By looking at the SQL query generated it looks like Cake PHP is interpreting 'conditions' and 'Order' as a column.
Can Anyone Help me?
params in order of Paginator::paginate
* #param Model|string $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
* #param string|array $scope Additional find conditions to use while paginating
* #param array $whitelist List of allowed fields for ordering. This allows you to prevent ordering
* on non-indexed, or undesirable columns. See PaginatorComponent::validateSort() for additional details
* on how the whitelisting and sort field validation works.
http://api.cakephp.org/2.6/class-PaginatorComponent.html#_paginate
Example :
$this->Paginator->settings = array(
'order'=>array('yourfields');
);
$this->Pagniator->paginate('Equipment', $conditionsArray ) ;

HTTP Plain-text data to array

I'm using the iOS device to access data on a local module, and it gets info from http://.../path_to/file.bin, and the response is in plain-text as:
[Header]
Version=1
[Data]
SomeData=aValue
StringKey=Value
TimestampKey=DDD:HH:MM:SS
NumberKey=8321
[Blob]
MoreData=moreValues
[EOF]
Lines=11
(timestamp only occurs once and is a time from the current time)
I would like to put all of the information in the string above into a NSDictionary to be more easily accessible. Something appearing like:
Dictionary (
Header => Dictionary (
Version => 1
)
Data => Dictionary (
SomeData => aValue,
StringKey => anotherValue,
TimestampKey => "DDD:HH:MM:SS", //(format into NSDate if you like)
NumberKey => 8321 //(format into int if you like)
}
Blob => Dictionary (
MoreData => moreValues
)
)
So I would enjoy a function that would format the string input into the dictionary output.
two solutions:
reformat your data (plist or json) to load them with existing frameworks
write your own parser
Here's an example on CocoaWithLove

Magento: addAttributeToFilter but ignore for products that don't have this attribute?

I'm trying to show add some filters on my store, but they have a nasty side effect.
Suppose I have product type A and B. Now I want to only show A where color = blue/red.
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($this)
->addAttributeToFilter(array(
array('attribute' => 'color', 'in' => array(4, 6)),
)
);
This does the trick, but now because product type B has no value assigned to color(since this attribute isn't appointed to it) no products for this type show up.
I had found this code on the forum http://www.magentocommerce.com/boards/viewthread/178309, but it doesn't work:
array('attribute' => 'color', 'is' => new Zend_Db_Expr('null'))
Neither does:
array('attribute' => 'color', 'null' => true),
That actually shows products which have the attribute assigned but with no value declared...
I also tried adding:
array('attribute' => 'price', 'gteq' => 0),
Because I figured these statements were connected with 'OR' (according to the documentation) but even that only adds product types which have the attribute assigned...
Note that these values come from a drop down list, not sure if that matters though.
Maybe it's too late, but this works for me:
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($this)
->addAttributeToFilter(
array(
array('attribute' => 'color', 'null' => true),
array('attribute' => 'color', 'in' => array(4, 6)),
),
'',
'left'
);
Offhand, give this a shot:
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($this)
->addAttributeToFilter(array(array('attribute' => 'color', 'in' => array(4, 6)),'left')
);

Resources