'$project' not working in my mongo aggregate query - mongodb-query

This is my Documentation format
{
"_id" : ObjectId("5af56909b8be3925cf199924"),
"pid" : "9354496457",
"vid" : NumberLong("34756636617"),
"title" : "Ascolour Barnard Stripe Tank Tee-(5032)",
"handle" : "ascolour-barnard-stripe-tank-tee-5032",
"image" : "https://cdn.shopify.com/s/files/1/1919/3401/products/5032_barnard_stripe_tank_natural_black_5.jpg?v=1493879380",
"color" : "XL",
"size" : "NATURAL/BLACK",
"width" : null,
"activity" : "",
"brand" : "Ascolour",
"style" : "",
"feature" : "",
"type" : "Adult Singlets",
"price" : 1100,
"compare_at_price" : 1100,
"qty" : 1,
"sku" : "5032",
"visible" : 1
}
{
"_id" : ObjectId("5af56909b8be3925cf199925"),
"pid" : "9354496457",
"vid" : NumberLong("34756636681"),
"title" : "Ascolour Barnard Stripe Tank Tee-(5032)",
"handle" : "ascolour-barnard-stripe-tank-tee-5032",
"image" : "https://cdn.shopify.com/s/files/1/1919/3401/products/5032_barnard_stripe_tank_natural_black_5.jpg?v=1493879380",
"color" : "2XL",
"size" : "NATURAL/BLACK",
"width" : null,
"activity" : "",
"brand" : "Ascolour",
"style" : "",
"feature" : "",
"type" : "Adult Singlets",
"price" : 1100,
"compare_at_price" : 1100,
"qty" : 1,
"sku" : "5032",
"visible" : 1
}
here i search "type"=>"adult Singlets" with distinct "pid" and project all columns in the documents
so i write query:
$cursor = $songs->aggregate([
['$project' => ['_id' => 1, 'title' => 1, 'size' => 1]],
['$match' =>
['$and' => [
['$or' => [
['title' => $regex],
['size' => $regex],
['color' => $regex],
['brand' => $regex],
['activity' => $regex],
['style' => $regex],
['type' => $regex],
['feature' => $regex],
['width' => $regex],
]
],
['visible' => ['$eq' => 1]],
['qty' => ['$gt' => 0]],
]
]
],
['$group' => ["_id" => '$pid']],
['$limit' => 10]
]);
but its not show me anything and if i remove this line
['$project' => ['_id' => 1, 'title' => 1, 'size' => 1]]
then its show me all distinct pid. but i need to show all column w.r.t distinct pid
Thanks in advance :)

by this query i solved it
$cursor = $songs->aggregate([
['$match' =>
['$and' => [
['$or' => [
['title' => $regex],
['size' => $regex],
['color' => $regex],
['brand' => $regex],
['activity' => $regex],
['style' => $regex],
['type' => $regex],
['feature' => $regex],
['width' => $regex],
]
],
['visible' => ['$eq' => 1]],
['qty' => ['$gt' => 0]],
]
]
],
['$group' =>
['_id' => '$pid',
'originalId' => ['$first' => '$_id'],
'title' => ['$first' => '$title'],
'brand' => ['$first' => '$brand'],
'pid' => ['$first' => '$pid'],
'image' => ['$first' => '$image'],
'size' => ['$first' => '$size'],
'color' => ['$first' => '$color'],
'activity' => ['$first' => '$activity'],
'style' => ['$first' => '$style'],
'type' => ['$first' => '$type'],
'feature' => ['$first' => '$feature'],
'width' => ['$first' => '$width'],
'handle' => ['$first' => '$handle'],
'sku' => ['$first' => '$sku'],
'visible' => ['$first' => '$visible'],
'qty' => ['$first' => '$qty'],
'compare_at_price' => ['$first' => '$compare_at_price'],
]
],
['$project' => [
'_id' => '$pid',
'title' => 1,
'brand' => 1,
'pid' => 1,
'image' => 1,
'size' => 1,
'color' => 1,
'activity' => 1,
'style' => 1,
'type' => 1,
'feature' => 1,
'width' => 1,
'handle' => 1,
'image' => 1,
'pid' => '$_id',
'sku' => 1,
'visible' => 1,
'qty' => 1,
'compare_at_price' => 1,
]
],
['$limit' => 10]
]);

Related

CakePHP4 - How to create multiple user login with the Authentication plugin?

There are two types of users in the application I am developing. Users (table users.sql) for frontend users and AdminUsers (table admin_users.sql) for administration.
In CakePHP3, I solved this problem as follows with AuthComponent in AppController:
public function initialize()
{
parent::initialize();
// ...
//user login
if (!empty($this->request->params['prefix']) AND
$this->request->params['prefix'] == 'admin'
) {
$this->setAdminLogin();
}else{
$this->setUserLogin();
$this->Auth->allow();
}
// ...
}
//frontend users
public function setUserLogin()
{
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'edit'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login'
],
'authError' => false,
'authenticate' => [
'Xety/Cake3CookieAuth.Cookie' => [
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => ['username' => 'email','password' => 'password'],
],
'Form' => [
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => ['username' => 'email','password' => 'password'],
'passwordHasher' => [
'className' => 'Fallback',
'hashers' => ['Default']
]
],
],
'storage' => ['className' => 'Session', 'key' => 'Auth.User']
]);
}
//admin users
public function setAdminLogin()
{
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'loginAction' => [
'controller' => 'AdminUsers',
'action' => 'login',
],
'loginRedirect' => [
'controller' => 'AdminHelps',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'AdminUsers',
'action' => 'login'
],
'authError' => false,
'authenticate' => [
'Form' => [
'userModel' => 'AdminUsers',
'scope' => ['AdminUsers.active' => 1],
'fields' => ['username' => 'email','password' => 'password'],
'passwordHasher' => [
'className' => 'Fallback',
'hashers' => ['Default']
]
],
],
'storage' => ['className' => 'Session', 'key' => 'Auth.AdminUser']
]);
}
How can I do the same thing in CakePHP4 version with Authentication plugin? How can I create multiple user login?

How can I set up a proxy to get the requests from selenium using Browsermob::Proxy in perl?

I have been the whole day working on this and this and I got a little bit frustrated for not being able to make it work.
I started using a standalone selenium server as follows:
java -Dwebdriver.chrome.driver="C:\some_path\chromedriver.exe" -jar "C:\some_path\selenium-server-standalone-3.141.59.jar"
Then I downloaded the browsermob-proxy-2.1.4 and just executed the ./bin/browsermob-proxy.bat
Then I battled for some hours to find the properties in chrome that would not complain because I am using a proxy and showed the exception NET::ERR_CERT_AUTHORITY_INVALID:
So I ended up with this code:
use strict;
use warnings;
use Data::Dumper;
use Browsermob::Proxy;
use Browsermob::Server;
use Selenium::Remote::Driver;
use Selenium::Waiter qw/wait_until/;
my $server = Browsermob::Server->new(
server_port => 8080
);
my $proxy = $server->create_proxy;
my $driver = Selenium::Remote::Driver->new('browser_name' =>'chrome',
proxy => $proxy->selenium_proxy(1),
'extra_capabilities' => {
'goog:chromeOptions' => {
'args' => [
'ignore-ssl-errors=yes',
'proxy-auto-detect',
],
'prefs' => {
'profile' => {
'accept_ssl_certs' => 'true',
'accept_insecure_certs' => 'true',
},
'session' => {
'restore_on_startup' => 4,
'urls_to_restore_on_startup' => [
'http://www.google.com',
'http://docs.seleniumhq.org'
]},
'first_run_tabs' => [
'http://www.google.com',
'http://docs.seleniumhq.org'
]
}
}
});
$proxy->create_new_har(
payload => {
initialPageRef => 'payload is optional'
},
captureHeaders => 'true',
captureContent => 'true',
captureBinaryContent => 'true'
);
print("Getting stackoverflow\n");
wait_until{$driver->get("https://www.stackoverflow.com")};
validate_site($driver);
sleep(20);
$driver->quit;
sub validate_site{
my ($driver) = #_;
print "Har:\n";
print Dumper($proxy->har);
}
But now I just get the following output, which looks like if the proxy would not capture anything:
Getting stackoverflow
Har:
$VAR1 = {
'log' => {
'creator' => {
'version' => '2.1.4',
'name' => 'BrowserMob Proxy',
'comment' => ''
},
'entries' => [],
'version' => '1.2',
'comment' => '',
'pages' => [
{
'comment' => '',
'pageTimings' => {
'comment' => ''
},
'startedDateTime' => '2020-10-01T18:21:20.171+02:00',
'id' => 'Page 0',
'title' => 'Page 0'
}
]
}
};
What is funny, is that if I comment the line autodected proxy, I get the following:
Getting stackoverflow
Har:
$VAR1 = {
'log' => {
'pages' => [
{
'comment' => '',
'id' => 'Page 0',
'title' => 'Page 0',
'startedDateTime' => '2020-10-01T18:19:15.850+02:00',
'pageTimings' => {
'comment' => ''
}
}
],
'entries' => [
{
'serverIPAddress' => 'some_ip',
'startedDateTime' => '2020-10-01T18:19:15.900+02:00',
'response' => {
'comment' => '',
'status' => 0,
'headers' => [],
'content' => {
'comment' => '',
'size' => 0,
'mimeType' => ''
},
'bodySize' => -1,
'statusText' => '',
'cookies' => [],
'redirectURL' => '',
'httpVersion' => 'unknown',
'_error' => 'Unable to connect to host',
'headersSize' => -1
},
'pageref' => 'Page 0',
'timings' => {
'connect' => 257,
'wait' => 0,
'ssl' => -1,
'receive' => 0,
'send' => 0,
'blocked' => 0,
'comment' => '',
'dns' => 0
},
'time' => 258,
'request' => {
'comment' => '',
'queryString' => [],
'cookies' => [],
'headers' => [],
'headersSize' => 0,
'bodySize' => 0,
'url' => 'https://www.stackoverflow.com',
'method' => 'CONNECT',
'httpVersion' => 'HTTP/1.1'
},
'cache' => {},
'comment' => ''
},
{
'time' => 258,
'cache' => {},
'request' => {
'httpVersion' => 'HTTP/1.1',
'bodySize' => 0,
'headersSize' => 0,
'url' => 'https://www.stackoverflow.com',
'method' => 'CONNECT',
'headers' => [],
'cookies' => [],
'comment' => '',
'queryString' => []
},
'comment' => '',
'serverIPAddress' => 'some_ip',
'response' => {
'_error' => 'Unable to connect to host',
'headersSize' => -1,
'httpVersion' => 'unknown',
'cookies' => [],
'redirectURL' => '',
'content' => {
'size' => 0,
'comment' => '',
'mimeType' => ''
},
'bodySize' => -1,
'statusText' => '',
'headers' => [],
'status' => 0,
'comment' => ''
},
'startedDateTime' => '2020-10-01T18:19:15.900+02:00',
'pageref' => 'Page 0',
'timings' => {
'dns' => 0,
'blocked' => 0,
'comment' => '',
'receive' => 0,
'send' => 0,
'connect' => 257,
'wait' => 0,
'ssl' => -1
}
},
{
'cache' => {},
'request' => {
'cookies' => [],
'comment' => '',
'queryString' => [],
'httpVersion' => 'HTTP/1.1',
'bodySize' => 0,
'headersSize' => 344,
'url' => 'http://www.gstatic.com/generate_204',
'method' => 'GET',
'headers' => [
{
'name' => 'Host',
'value' => 'www.gstatic.com'
},
{
'name' => 'Proxy-Connection',
'value' => 'keep-alive'
},
{
'value' => 'no-cache',
'name' => 'Pragma'
},
{
'name' => 'Cache-Control',
'value' => 'no-cache'
},
{
'value' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
'name' => 'User-Agent'
},
{
'name' => 'Accept-Encoding',
'value' => 'gzip, deflate'
},
{
'value' => 'en-US,en;q=0.9',
'name' => 'Accept-Language'
}
]
},
'time' => 161,
'comment' => '',
'serverIPAddress' => 'some_ip',
'timings' => {
'connect' => 46,
'ssl' => -1,
'wait' => 21,
'receive' => 38,
'send' => 1,
'blocked' => 0,
'comment' => '',
'dns' => 53
},
'startedDateTime' => '2020-10-01T18:19:16.176+02:00',
'response' => {
'cookies' => [],
'redirectURL' => '',
'comment' => '',
'status' => 204,
'headers' => [
{
'name' => 'Content-Length',
'value' => '0'
},
{
'value' => 'Thu, 01 Oct 2020 16:19:16 GMT',
'name' => 'Date'
}
],
'httpVersion' => 'HTTP/1.1',
'content' => {
'comment' => '',
'size' => 0,
'encoding' => 'base64',
'text' => '',
'mimeType' => ''
},
'bodySize' => 0,
'statusText' => 'No Content',
'headersSize' => 85
},
'pageref' => 'Page 0'
}
],
'creator' => {
'name' => 'BrowserMob Proxy',
'comment' => '',
'version' => '2.1.4'
},
'version' => '1.2',
'comment' => ''
}
};
So although I get the exception in the browser and I can'T do anything with selenium it seems to work.
Anyone can give me a hint on what is going on?
Thank you!

Not added to data to table

I'm trying to validate the form.
I do not understand why form validation does not take place. Logically, it should work with the simple presence of symbols, but in fact, a simple test:
if (! $form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
returned " echo 'not valid in check' "
What is the problem?
Input filter are very simple:
public function getInputFilter()
{
if ($this->inputFilter) {
return $this->inputFilter;
}
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'id',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
]);
$inputFilter->add([
'name' => 'text',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 1000,
],
],
],
]);
$inputFilter->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}
Form? for added post:
class PostForm extends Form
{
public function __construct($name = null)
{
// We will ignore the name provided to the constructor
parent::__construct('post');
$this->add([
'name' => 'id',
'type' => 'hidden',
]);
$this->add([
'name' => 'title',
'type' => 'text',
'options' => [
'label' => 'Title',
],
]);
$this->add([
'name' => 'text',
'type' => 'textarea',
'options' => [
'label' => 'Text',
],
]);
$this->add([
'name' => 'submit',
'type' => 'submit',
'attributes' => [
'value' => 'Go',
'id' => 'submitbutton',
],
]);
}
}
Add action, if it need:
public function addAction()
{
$form = new PostForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if (! $request->isPost()) {
return ['form' => $form];
}
// echo 'test';
$post = new Post();
$form->setInputFilter($post->getInputFilter());
$form->setData($request->getPost());
// echo 'before valid';
if (! $form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
echo '\n';
echo 'valid';
$post->exchangeArray($form->getData());
$this->table->savePost($post);
return $this->redirect()->toRoute('post');
}
public function addAction()
{
---------------
if (!$form->isValid()) {
echo 'not valid in check';
return ['form' => $form];
}
---------
}
Validation:
public function getInputFilter()
{
if ($this->inputFilter) {
return $this->inputFilter;
}
$inputFilter = new InputFilter();
$inputFilter->add([
'name' => 'id',
'required' => true,
'filters' => [
['name' => ToInt::class],
],
]);
$inputFilter->add([
'name' => 'text',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 1000,
],
],
],
]);
$inputFilter->add([
'name' => 'title',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$inputFilter->add([
'name' => 'disc',
'required' => true,
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
],
],
],
]);
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}

Kartik Grid Editable Column with expandrow styling not applying

When I pull in a kartik expandable row grid, using pjax the editablecolumn styling is not being applied. What can I do to apply css to this column?
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax' => false,
'columns' => [
[
'class' => 'kartik\grid\ExpandRowColumn',
'value' => function ($model, $key, $index, $column){
return GridView::ROW_COLLAPSED;
},
'detailUrl' => 'index.php?r=controller/detail'
],
Expanded View
<?php echo GridView::widget([
'dataProvider' => $dataProvider,
'pjax'=> true,
'columns' => [
[
'attribute' => 'brand_name',
'value' => function($model,$key,$idx,$col){
return $model['brand_name'];
},
],
[
'class'=>'kartik\grid\EditableColumn',
'hAlign'=>'center',
'vAlign'=>'middle',
'value' => function($model,$key,$idx,$col){
return 100;
},
'editableOptions' => [
'name' => 'Test',
'header' => 'Test22',
'inputType' => Editable::INPUT_TEXT,
'formOptions' => ['action' => ['/book/editbook']],
'options' => [
'convertFormat'=>true,
'pluginOptions' => ['format' => 'php:Y-m-d']
]
]
],
Maybe you should try to set HTML with containerOptions or contentOptions attribute in editableOptions.

in yii2 : why my default language is en

how to change default language from en to another language
$config = [
'on beforeAction' => function ($event)
{
Yii::$app->language = 'fa';
},
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'language' => '/fa',
'components' => [
'jdate' => [
'class' => 'jDate\DateTime'
],
'mycomponent' => [
'class' => 'app\components\MyComponent',
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '******',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'urlManager' => [
'class' => 'codemix\localeurls\UrlManager',
// Disable index.php
'languages' => ['fa', 'en'], // List all supported languages here
'showScriptName' => true,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
// '' => 'site/index/fa',
// '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
// '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'params' => $params,
];
this is my $config in web.php
i want set default to 'fa' in root site !
my site automatic change lang to 'en' in first time in any browser!
i try to change lang with
'on beforeAction' => function ($event)
{
Yii::$app->language = 'fa';
},
but that is dosent correct work!
Remove this ridiculous 'on beforeAction' thing and just set
'language' => 'fa', // NOT '/fa'!
سلام
ویدئو های آقای صیف زاده رو مشاهده کردین؟
لینک زیر آموزش های ویدئویی ایشون هست . در شماره های 17 و 18 و 19
این موضوع رو آموزش دادن.
https://drive.google.com/drive/folders/0B4ZlNlar4Ij6XzJrbVZOejRCcGM