Cakephp 3 inner join with count and group by not working - postgresql

$options = array(
'fields' => array(
'item_requirements.*',
'COUNT(`item_requirements`.`quantity_required`) as count'
),
'joins' => array(
'INNER JOIN `items` AS item_requirements ON `item_requirements`.`item_id` = `items`.`id`'
),
'group' => '`item_requirements`.`item_id`',
'contain' => array(
'items' => array('fields' => array('name', 'specification'))
)
);
$query = $this->Indents->ItemRequirements->find('all', $options);
Error: SQLSTATE[42601]: Syntax error: 7 ERROR: zero-length delimited identifier at or near """" LINE 1: ...item_requirements`.`quantity_required`) AS "counts"" AS "COU... ^
the above error occurs.. any solutions to solve this..

The issue is with
item_requirements.*.
.* is not supported by cake ORM.
You can use ->autoFields(true) option. Refer this answer

Related

How to use where/whereDate in mongodb aggregate functions using Jessengers package in Laravel?

How to use where/whereDate in aggregate mongodb query ?
I am using aggregate query in laravel and jessenger-mongodb to group by fields.
The problem here is, I need to add a condition to fetch grouped data of a particular date i.e created_at.
Following code worked perfectly if I do not use pass $date i.e $date = null:
public static function getUsersByTypeLogin($date = null)
{
$q = self::raw()->aggregate(
[
array(
'$match' => array(
'type' => self::LOGIN,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]
);
if(true == $date){
$q = $q->whereDate('created_at','=',$date);
}
return $q;
}
but If I pass $date it returns an error, it says:
message: "Call to undefined method MongoDB\Driver\Cursor::whereDate()"
$date is a Carbon object e.g:
Carbon #1549843200 {#791
date: 2019-02-11 00:00:00.0 +00:00
}
Please help me in this regards.
Try this.
$q = self::raw(function($collection) use($date) {
return $collection->aggregate([
array(
'$match' => array(
'type' => self::LOGIN,
'created_date' => $date,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]);
});

How to get distinct documents by some field in mongodb phalcon?

In mongo shell the query:
db.collections.distinct('user_id');
simply gives the distinct documents.
I am working on phalcon and there is no option like
Collections::distinct()
So how do i query distinct in phalcon. Please help me
Perhaps Phalcon's answer in the forum could help: https://forum.phalconphp.com/discussion/6832/option-for-function-distinct-phalconmvccollection
They suggest using aggregations:
$data = Article::aggregate(
array(
array(
'$project' => array('category' => 1)
),
array(
'$group' => array(
'_id' => array('category' => '$category'),
'id' => array('$max' => '$_id')
)
)
)
);

Zend multiCheckbox default values bug

I don't know. Simply I don't know. Why does the second codeblock work and check the checkboxes by default, but the first block isn't?
I need to pre-check bitmask flags and I can't/don't want to append strings or something.
// THIS isn't working?!!
$test1 = array(
2 => 'tomato',
4 => 'bitmask problem'
);
$test2 = array(2, 4);
$form->addElement('multiCheckbox', 'flags', array(
'label' => 'Flags',
'value' => $test2,
'multiOptions' => $test1,
)
);
// THIS IS WORKING:
$form->addElement (
'multiCheckbox', 'servers2',
array (
'label' => 'test',
'value' => array('a', 'b'), // select these 2 values
'multiOptions' => array(
'a' => 'aaaaa',
'b' => 'aaaaa',
'c' => 'aaaa',
)
)
);
$form->addElement('multiCheckbox', 'flags', array(
This is causing the error. flags is kinda reserved word in Zend I guess. But I didn't get an error message and I have no other form elements or even variables called flags.
When I rename this, it works!
$form->addElement('multiCheckbox', 'matchingFlags', array(

MongoDB keeps giving me null

I have some difficulty in getting Mongodb aggregate to work. It keeps giving me null. Please help. Below are the codes written in php. Thanks.
What I want to do is to sum up the values of 2 fields, Requests and Responses, between 2 particular dates
try {
$mongodb = new MongoClient("mongodb://ad:pass2word1#localhost");
$database = $mongodb->selectDB('backend');
$collection = new MongoCollection($database, 'RequestSummary');
$pipeline = array(
array(
'$group' => array(
'_id' => array(
'request' => array('$sum' => '$Requests'),
'response' => array('$sum' => '$Responses')
)
)
),
array(
'$match' => array(
'RequestDate' => array(
'$gte' => intval($_SESSION['range_from']),
'$lte' => intval($_SESSION['range_to'])
)
)
)
);
$collection->aggregate($pipeline);
var_dump($g);
} catch (MongoConnectionException $exc) {
echo $exc->getTraceAsString();
}
The _id of your $group can't contain aggregation operators like $sum. Those sums need to be defined as fields at the same level as _id. If you don't want to group on a specific field you can use NULL for the _id like this:
array(
'$group' => array(
'_id' => NULL,
'request' => array('$sum' => '$Requests'),
'response' => array('$sum' => '$Responses')
)
),

How to customize Zend_Form_Element_Select

How to put space in between option value of Zend_Form_Element_Select
Expected result as follows,
http://jsfiddle.net/HLbQE/
Tried as follows,
$this->addElement('Select', 'parent_id', array(
'label' => 'Select Category',
'multiOptions' => array('0'=>'Gents','1'=>' Jeans','2'=>' Sunglass','3'=>'Ladies','4'=>' Jeans','5'=>' Sunglass')
));
but fails,
Any help please
Why not just use optgroups? ZF handles this natively by using a nested array for the multi-options, eg
$options = array(
'Gents' => array(
1 => 'Jeans',
2 => 'Sunglass'
),
'Ladies' => array(
3 => 'Jeans',
4 => 'Sunglass'
)
);
Updated demo here - http://jsfiddle.net/HLbQE/1/
Try this:
$this->addElement('Select', 'parent_id', array(
'label' => 'Select Category',
'multiOptions' => array('0'=>'Gents','1'=>' Jeans','2'=>' Sunglass','3'=>'Ladies','4'=>' Jeans','5'=>' Sunglass'),
'escape' => false // <-- added
));
I seems this will not work indeed, as reported in this, still unresolved, issue:
http://framework.zend.com/issues/browse/ZF-5351