while loop in wp query array - arrays

I got this wordpress array and need a while loop inside for every 'AND' relation:
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'foo_0_start',
'compare' => '>=',
'value' => '$start'
),
array(
'key' => 'foo_0_end',
'compare' => '<=',
'value' => '$end'
)
),
array(
'relation' => 'AND',
array(
'key' => 'foo_1_start',
'compare' => '>=',
'value' => '$start'
),
array(
'key' => 'foo_1_end',
'compare' => '<=',
'value' => '$end'
)
)
)
);
I was searching for hours and tried to build a function without success. How can I accomplish this issue? And what happens with the "'relation' => 'OR',"?
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array()
);
$i = 1;
while ($i<=5;) :
$i++
$query_args['meta_query'][] = array (
'relation' => 'AND',
array(
'key' => 'foo_$i_start',
'compare' => '>=',
'value' => '$start'
),
array(
'key' => 'foo_$i_end',
'compare' => '<=',
'value' => '$end'
)
);
endwhile;
Help would be highly appreciated.

ok. Found the solution:
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array('relation' => 'OR')
);
$i = 1;
while ($i<=5) :
$i++;
$query_args['meta_query'][] = array (
'relation' => 'AND',
array(
'key' => 'foo_' . $i . '_start',
'compare' => '>=',
'value' => '$start'
),
array(
'key' => 'foo_' . $i . '_end',
'compare' => '<=',
'value' => '$end'
)
);
endwhile;

Related

meta_query , why does not it work relation = or?

<?php
$param_1 = array( 'key' => 'width_picture', 'value' => array( $_GET['width_min'], $_GET['width_max'] ), 'compare' => 'BETWEEN');
$param_2 = array( 'key' => 'height_picture', 'value' => array( $_GET['height_min'], $_GET['height_max'] ), 'compare' => 'BETWEEN');
$param_3 = array( 'key' => '_price', 'value' => array( $_GET['price_min'], $_GET['price_max'] ), 'compare' => 'BETWEEN');
$massSort['relation']= 'OR';
$massSort[] = array($param_1, $param_2, $param_3);
var_dump($massSort);
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' =>'date',
'order' => 'DESC',
'meta_query' => array($massSort)
);
More info
$meta_query = array('relation' => 'OR');
if (!empty($_GET['price_min']) && !empty($_GET['price_max'])):
$meta_query[] = array(
array(
'key' => '_price',
'value' => array( $_GET['price_min'], $_GET['price_max'] ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
);
endif;
if (!empty($_GET['height_min']) && !empty($_GET['height_max'])):
$meta_query[] = array(
array(
'key' => 'height_picture',
'value' => array( $_GET['height_min'], $_GET['height_max']),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
);
endif;
if (!empty($_GET['width_min']) && !empty($_GET['width_max'])):
$meta_query[] = array(
array(
'key' => 'width_picture',
'value' => array( $_GET['width_min'], $_GET['width_max'] ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
);
endif;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' =>'date',
'order' => 'DESC',
'meta_query' => meta_query
);

How to get rid of null associated model in cakephp 2.x

I am trying to get the item types that Order.Item.ItemType.show_type = 1. I have written the query but I want to show only Items that their ItemType.show_type = 1 not all items.
$brief = $this->Order->find('first', array(
'fields' => array(
'Order.*'
),
'conditions' => array(
'Order.order_id' => $orderId,
),
'contain' => array(
'Item' => array(
'fields' => array(
'Item.*', 'CHAR(64 + Item.num) AS letter'
),
'conditions' => array(
'Item.deleted' => 0,
),
'ItemType' => array(
'conditions' => array(
'ItemType.show_type' => 1
),
)
),
)
));
The query shouldn't show Item id = 25741
Associations:
// Order
public $hasMany = array(
'BriefInstalment' => array(
'foreignKey' => 'order_id'
)
);
// Item Model
public $belongsTo = array(
'Order',
'ItemType' => array(
'type' => 'inner'
)
);
// ItemType Model
public $hasMany = array('Item');
Print:
array(
'Order' => array(
'order_id' => '67817',
'service' => '',
),
'Item' => array(
(int) 0 => array(
'id' => '25741',
'order_id' => '67817',
'num' => '2',
'item_type_id' => '8',
'name' => '3-5 titles active',
'deleted' => false,
'ItemType' => array(), // <= how to remove this empty model
'Item' => array(
(int) 0 => array(
'letter' => 'B'
)
)
),
(int) 1 => array(
'id' => '25742',
'order_id' => '67817',
'num' => '3',
'item_type_id' => '2',
'name' => '1,000 pro active',
'deleted' => false,
'ItemType' => array(
'id' => '2',
'name' => 'Part Instalment',
'show_type' => true,
'deleted' => false
),
'Item' => array(
(int) 0 => array(
'letter' => 'C'
)
)
)
)
)
This could not be done using Countaible behaviour, but iwth the joins method, set the recursive to -1
$brief = $this->Order->find('first', array(
'recursive' => -1,
'fields' => array(
'Order.*'
),
'conditions' => array(
'Order.order_id' => $orderId,
),
'joins' => array(
array(
'table' => 'items',
'alias' => 'Item',
'type' => 'inner',
'conditions' => array(
'Order.id = Item.order_id'
)
),
array(
'table' => 'item_types',
'alias' => 'ItemType',
'type' => 'inner',
'conditions' => array(
'ItemType.id = Item.item_type_id'
)
),
)
));
You have to check if the table names are correct and also the foreign keys names.
Another solution would be to go through your results, and unset the empty ones
foreach($brief as $k => $v){
foreach($v['Item'] as $kk => $vv){
if(empty($vv['ItemType'])){
unset($brief[$k]['Item'][$kk];
}
}
}
debug($brief);

CakePHP 2.x retriving data with i18n in a loop

I'm looping trough Features (belongTo FeatureType) and find() FeatureType.name for each Feature.feature_type_id.
I'm getting first record correct (with i18n translation) but in all the rest the i18n translation is not in place, but given as separate record.
What I am doing wrong?
this is the debug of my result table:
array(
'FeatureType' => array(
'id' => '28',
'name' => 'kolor suwaka',
'comment' => 'kolor suwaka etui notebook',
'locale' => 'pol'
)
)
array(
'FeatureType' => array(
'id' => '7',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor materiału',
'FeatureType__i18n_comment' => 'gra w klasy'
)
)
array(
'FeatureType' => array(
'id' => '11',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor',
'FeatureType__i18n_comment' => 'kółko i krzyżyk (jasnoszare i ciemnoszare)'
)
)
array(
'FeatureType' => array(
'id' => '27',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'obwód głowy',
'FeatureType__i18n_comment' => 'rozmiar kapelusza czarownicy'
)
)
This is the code:
$features_str = "";
if (!empty($product['Features'])) {
foreach ($product['Features'] as $featureIndex => $feature) {
$featureTypeModel = new FeatureType();
$feature_type = $featureTypeModel->find("first", array("conditions" => array("FeatureType.id" => $feature['feature_type_id'])));
if (strlen($features_str) > 0) $features_str .= ", ";
$features_str .= $feature_type['FeatureType']['name'] . ': ' . $feature['name'];
unset($featureTypeModel);
}
}

How to assign mysql functions on join fields in Cakephp

I want to get last_deadline and the count of instalments of all instalments but obviously this query will show me 1 order and the last_deadline.
$orders = $this->find('all', array(
'fields' => array(
'Order.order_id', 'Order.summary', 'Order.fee',
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline'
),
'conditions' => $conditions,
'joins' => array(
array(
'table' => $this->getTableName('default', 'brief_instalments'),
'alias' => 'BriefInstalment',
'type' => 'RIGHT',
'conditions' => array(
'Order.order_id = BriefInstalment.order_id',
'BriefInstalment.deleted' => 0,
),
'order' => 'BriefInstalment.deadline ASC',
)
),
'order' => 'BriefInstalment.deadline ASC'
));
I have tried 'contain' and doesn't work.
'contain' => array(
'BriefInstalment' => array(
'fields' => 'BriefInstalment.id',
'fields' => array(
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline', 'COUNT(`BriefInstalment`.`id`) AS total_instalments'
),
'conditions' => array(
'BriefInstalment.deleted' => 0
)
)
),
By the way I don't want to use loop to get last_intalments and cout brief_instalments. e.g.
// Determine deadlines
foreach ($orders as $i => $order) {
$deadline = $this->BriefInstalment->getLastDeadline($order['Order']['order_id']);
$orders[$i] += array(
...
'last-deadline' => $deadline,
'total-instalments' => count($order['BriefInstalment'])
);
}
The reason is it decrease the speed of loading.
Any help plz
Here is how to create custom query
$conditionsSubQuery['"User2"."status"'] = 'B';
$db = $this->User->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('"User2"."id"'),
'table' => $db->fullTableName($this->User),
'alias' => 'User2',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->User
);
$subQuery = ' "User"."id" NOT IN (' . $subQuery . ') ';
$subQueryExpression = $db->expression($subQuery);
$conditions[] = $subQueryExpression;
$this->User->find('all', compact('conditions'));
Reference:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries

cakephp contain - condition

my problem is that can i give condition [ 'BookTitleMaster.id' => $xtitid, ] as like below
$bbookinfs = $this->BookStockin->BookIssue->find('all', array(
'conditions' => array('return_status' => 2),
'contain' => array(
'BookStockin' => array(
'BookTitleMaster' => array(
'BookTitleMaster.id' => $xtitid,
'fields' => array('id','title','sub_title','book_material_type_id','book_author_id','course_detail_id','isbn_no','book_publisher_id','pub_year','pub_place','desc','no_pages','volume'),
'BookMaterialType' => array('name'),
'CourseDetail' => array('name'),
'BookPublisher' => array('name'),
'BookAuthor' => array('name')
)
)
)
));
I believe that you miss is:
'conditions' => array('BookTitleMaster.id' => $xtitid),
So, your final code should be:
$bbookinfs = $this->BookStockin->BookIssue->find('all', array(
'conditions' => array('return_status' => 2),
'contain' => array(
'BookStockin' => array(
'BookTitleMaster' => array(
'conditions' => array('BookTitleMaster.id' => $xtitid),
'fields' => array('id','title','sub_title','book_material_type_id','book_author_id','course_detail_id','isbn_no','book_publisher_id','pub_year','pub_place','desc','no_pages','volume'),
'BookMaterialType' => array('name'),
'CourseDetail' => array('name'),
'BookPublisher' => array('name'),
'BookAuthor' => array('name')
)
)
)
));
HTH
Shouldn't it be:
'conditions' => array('BookTitleMaster.id' => $xtitid),

Resources