I need to create mongodb collection while executing code, by checking whether the collection is exists or not.
I tried below things
Yii::$app->db->createCommand()-> createCollection("collection_name");
but collection not created.
Please help.
Issue resolved..
Yii::$app->db->createCommand()->createCollection("collection_name")->execute();
and index will added for collection as,
$collectionName = Yii::$app->db->getCollection("collection_name");
$collectionName->createIndexes([
'key' => ['id' => 'int'],
'name' => 'id_index'
],
[
'key' => ['id' => 'int', 'category' => 'text'],
'name' => 'id_category_index',
]);
Related
I created a new factory in Laravel called "CarFactory.php" and I want to use https://github.com/pelmered/fake-car.
This is the fake data that I will insert into my database using Tinker:
return [
'name' => $this->faker->vehicleBrand(),
'founded' => $this->faker->biasedNumberBetween(1998,2017, 'sqrt'),
'description' => $this->faker->paragraph()
];
In the Laravel usage code, I'm not sure where to put this (e.g. Car Model, CarFactory):
$faker->addProvider(new \Faker\Provider\Fakecar($faker));
$v = $faker->vehicleArray();
return [
'vehicle_type' => 'car',
'vin' => $faker->vin,
'registration_no' => $faker->vehicleRegistration,
'type' => $faker->vehicleType,
'fuel' => $faker->vehicleFuelType,
'brand' => $v['brand'],
'model' => $v['model'],
'year' => $faker->biasedNumberBetween(1998,2017, 'sqrt'),
];
Help is needed to know how to set this up.
I have a model for a collection which contains assets.
I wish to return the asset count with the collection resource though as a result this causes the whenLoaded function to be called which as a result loads all of the assets within the collection. I do not wish for this to occur.
I'm not sure how to go about creating a work around which will continue to allow me to use the resource, count the assets, but not call the whenLoaded function.
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'enabled' => $this->enabled,
'sticky' => $this->sticky,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'asset_count' => $this->assets->where('enabled', 1)->count(),
'assets' => Asset::collection($this->whenLoaded('assets')),
];
Is there a way to still use the resource, return the asset count, but not have the whenLoaded called as a result?
Try this
you can simply create a new relationship like liveassets
function liveassets(){
return $this->hasMany('assets')->where('enabled','=', 1);
}
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'enabled' => $this->enabled,
'sticky' => $this->sticky,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'asset_count' => $this->liveassets->count(),
'assets' => $this->liveassets,
];
Hi so I'm trying to save data through Cakephp3 framework. I have two tables Measures and MeasuresUsers. Measures has many MeasuresUsers
$data = [
'name' => $this->request->data['name'],
'description' => $this->request->data['description'],
'deleted' => $this->request->data['deleted'],
'chart_type' => $this->request->data['chart_type'],
'public' => $this->request->data['public'],
'user_id' => $this->request->data['user_id']
];
$measure = $this->Measures->newEntity($data,['associated' => ['MeasuresUsers']]);
$measure['measures_users'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
$this->Measures->save($measure)
I'm able to save data into Measures table but not into the associative table (MeasuresUsers). I'm not sure what I'm missing here, any help or comments is appreciated!
#HarryM. is close, but you want to put your data into the $data array before creating the entity (the associated tag is kinda pointless if you're not providing that data!), and since it's a hasMany relation, I think you need to have the data in an array of arrays:
$data = [
'name' => $this->request->data['name'],
'description' => $this->request->data['description'],
'deleted' => $this->request->data['deleted'],
'chart_type' => $this->request->data['chart_type'],
'public' => $this->request->data['public'],
'user_id' => $this->request->data['user_id'],
'measures_users' => [
[
'user_id' => $user['id'], // Should this be $this->request->data['user_id'] instead?
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public'],
],
],
];
$measure = $this->Measures->newEntity($data, ['associated' => ['MeasuresUsers']]);
$this->Measures->save($measure);
Coming from CakePHP 2.x, I see your associate model is MeasuresUsers.
When you're setting the associated data in
$measure['measures_users'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
Trying changing $measure['measures_users'] to $measure['MeasuresUsers']:
$measure['MeasuresUsers'] = [
'user_id' => $user['id'],
'chart_type' => $this->request->data['chart_type'],
'deleted' => $this->request->data['deleted'],
'public' => $this->request->data['public']
];
CakePHP's model conventions usually assume camel casing of what you have in the database e.g. DB Table it movie_tickets and Cake would expect is as MovieTicket unless otherwise declared in the Model itself.
I'm new at yii and trying to find my way around. I have done a tutorial or two. I then decided to start editing/changing the example to allow me to learn more. I created a page that does a simple PING. That gets validated. On success, it loads a static page. This all works.
What I wanted to do next is to see how I can utilize a grid to populate that with some data. My real life example is the same. I will get a array of data coming in.
It seems that CArrayDataProvider is what I need. So, I am trying to get a very simple example to work. If I get this to work, I can move on.
I have tried a whole bunch of examples. The error is the same every time. It seems that I do not have CArrayDataProvider installed? If that is even possible.
I did a standard basic install:
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
I have the following at the beginning of my controllers file:
use yii2\data\ArrayDataProvider;
I get no error here.
I searched for the file itself on the file system, could not find it. I did find ArrayDataProvider, so I tried that, same result:
use vendor\yiisoft\yii2\data\ArrayDataProvider;
The error is:
PHP Fatal Error – yii\base\ErrorException
Class 'CArrayDataProvider' not found
This is on line 24:
"dataProvider = new CArrayDataProvider($fruits);"
Here is my example code. Not that I think the issue is in here, but to show what I am trying to do:
$fruits = array(
array('id' => 1, 'name'=>'apple', 'color' => 'green'),
array('id' => 2, 'name'=>'orange', 'color' => 'orange'),
array('id' => 3, 'name'=>'banana', 'color' => 'yellow'),
array('id' => 4, 'name'=>'pineapple', 'color' => 'brown')
);
$dataProvider = new CArrayDataProvider($fruits);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'fruits-grid',
'dataProvider' => $dataProvider ,
'columns' => array(
array(
'name' => 'ID',
'value' => '$data["id"]',
),
array(
'name' => 'Name',
'value' => '$data["name"]'
),
array(
'name' => 'Color',
'value' => '$data["color"]'
),
)
));
On the file-system (linux) itself I did a update:
composer update
I have been Googling for the last 2 days and I am finding nothing.
I tried adding a date picket. That worked. I used:
https://www.tutorialspoint.com/yii/yii_extensions.htm
So in short, the static page that I call, now displays a DateTimePicker.
At the start of the file I added:
use kartik\datetime\DateTimePicker;
And in the body:
<?php
echo DateTimePicker::widget([
'name' => 'dp_1',
'type' => DateTimePicker::TYPE_INPUT,
'value' => '23-Feb-1982 10:10',
'pluginOptions' => [
'autoclose'=>true,
'format' => 'dd-M-yyyy hh:ii'
]
]);
?>
How do I get yii2 to allow me to use ArrayDataProvider. Or how do I install the extension? Or who do I reference it?
In Yii2 there's not a CArrayDataProvider. Use ArrayDataProvider, like described in docs:
$provider = new yii\data\ArrayDataProvider([
'allModels' => $query->from('post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
Pretty well documented here.
Yii2 supports ArrayDataProvider and yii 1.* supports CArrayDataProvider, so as per your code you are using Yii2, so just replace the below line with
$dataProvider = new CArrayDataProvider($fruits);
With this:
$dataProvider = new ArrayDataProvider($fruits);
Thx to everyone's help! I now have a working example.
In case anyone else has the same issues, I will post my working code here:
use kartik\grid\GridView;
$resultData = [
array("id"=>1,"name"=>"Cyrus","email"=>"risus#consequatdolorvitae.org"),
array("id"=>2,"name"=>"Justin","email"=>"ac.facilisis.facilisis#at.ca"),
array("id"=>3,"name"=>"Mason","email"=>"in.cursus.et#arcuacorci.ca"),
array("id"=>4,"name"=>"Fulton","email"=>"a#faucibusorciluctus.edu"),
array("id"=>5,"name"=>"Neville","email"=>"eleifend#consequatlectus.com"),
array("id"=>6,"name"=>"Jasper","email"=>"lectus.justo#miAliquam.com"),
array("id"=>7,"name"=>"Neville","email"=>"Morbi.non.sapien#dapibusquam.org"),
array("id"=>8,"name"=>"Neville","email"=>"condimentum.eget#egestas.edu"),
array("id"=>9,"name"=>"Ronan","email"=>"orci.adipiscing#interdumligulaeu.com"),
array("id"=>10,"name"=>"Raphael","email"=>"nec.tempus#commodohendrerit.co.uk"),
];
$dataProvider = new \yii\data\ArrayDataProvider([
'key'=>'id',
'allModels' => $resultData,
'sort' => [
'attributes' => ['id', 'name', 'email'],
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'attribute' => 'name',
'value' => 'name',
],
[
"attribute" => "email",
'value' => 'email',
]
]
]);
I'm having trouble duplicating my MySQL delete query in elastic search, I am using this documentation: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete-by-query.html using the PHP wrapper for Laravel.
I'm trying this:
$this->es->deleteByQuery([
'index' => 'users',
'type' => 'user',
'body' => [
'query' => [
'term' => ['field1' => $this->field1],
'term' => ['field2' => $this->field2],
'term' => ['temp' => 0]
]
]
]);
Its suppose to be a DELETE FROM users WHERE field1 = $this->field1 AND field2 = $this->field2...
I'm having trouble translating the WHERE AND syntax to Elastic Search.
Any help?
Your second comment was mostly correct:
I think I have have gone overboard right now. I have body => query =>
filter => filtered => bool => must => term, term, term. Do I need the
filter => filtered arrays?
The bool filter is preferable over the bool query, since filtering is often much faster than querying. In your case, you are simply filtering documents that have the various terms, and don't want them contributing to the score, so filtering is the correct approach.
This should be done though the query clause, however, since the top-level filter clause is used for a different purpose (filtering facets/aggregations...it was in-fact renamed to post_filter in 1.0 to signify that it is a "post filtering" operation).
Your query should look something like this:
$this->es->deleteByQuery([
'index' => 'users',
'type' => 'user',
'body' => [
'query' => [
'filtered' => [
'filter' => [
'must' => [
['term' => ['field1' => $this->field1]],
['term' => ['field2' => $this->field2]],
['term' => ['temp' => 0]]
]
]
]
]
]
]);