i have a table commande witch contains a date, heure ... i want to return a table witch gives me a commande group by date but my problem i get one commande for each date wwhen i have more than a commande on that date her is my request:
$factures = $this->Commande->find('all', array(
'conditions' => array(
'Commande.prestataire_id' => $this->request->data['prestataire_id']
),
'group' => array('Commande.date'),
'order' => array('Commande.date' => 'DESC'),
'fields' => array(' Commande.date'),
));
here is the output:
{
"status": "Success",
"status_code": 200,
"message": "Commande trouvé",
"data": {
"everjob_get_Mission": {
"data": [
{
"Commande": {
"date": "2018-04-25"
}
},
{
"Commande": {
"date": "2018-04-24"
}
},
{
"Commande": {
"date": "2018-04-23"
}
},
{
"Commande": {
"date": "2018-04-19"
}
},
{
"Commande": {
"date": "2018-04-20"
}
}
]
}
}
}
Try to below code. I hope its work for you not sure.
$factures = $this->Commande->find('all', array(
'fields' => 'DISTINCT Commande.date',
'conditions' => array(
'Commande.prestataire_id' => $this->request->data['prestataire_id']
),
'group' => array('Commande.date'),
'order' => array('Commande.date' => 'DESC')
));
Related
here is the data
req[
{
"_id": "123",
"_ar_id": "111",
"_val": "10",
"ar_date": "2023-02-02"
},
{
"_id": "1234",
"_ar_id": null,
"_val": "0",
"ar_date": "2023-02-02"
}
]
$validator = Validator::make($req->all(), [
'req.*._id' => 'required',
'req.*._val' => 'required',
'req.*.ar_date' => 'required',
]);
if ($validator->fails()) {
return response()->json(['stat' => 0,'msg' => $validator->errors(), 'data' => "Please input required details" ]);
}
since i validate the request, how can i use it in where to ?
here's the code
$record = ArRecordCount::where('ar_list_id', $list_id)->where('ar_date', $ar_date)->first();
and if has record then it will update, if no record then delete
An API I'm using need this specific format when I try anything else I get a 200 but without it actually working
{
"things": [
{
"awdad": "awd",
"234": "234234",
"234234234": {
"name": "Jon",
},
"wadadw": {
"234234234": "awdawd",
}
}
]
}
what I tried to do is this
$data['things'] = [
'awdad' => 'awd',
'234' => '234234',
'234234234' => [
'name'=>'Jon',
],
'wadadw' => [
'234234234'=>'awdawd',
],
];
after JSON encoding it gave me this
{
"things": {
"awdad": "awd",
"234": "234234",
"234234234": {
"name": "Jon"
},
"wadadw": {
"234234234": "awdawd"
}
}
}
I am trying to integrate search by date range with PHP and elastic search
$params1 = [
'index' => 'joborders',
'type' => 'joborder',
'from' =>0,
'size' => 50,
'body' => [
'query' => [
'query_string' => [
'query' => $wildCardString,
'fields' => ['description'],
]
]
]
];
$filter_date=array();
$filter_date['range']['datecreatedsort']['gte']='2015-11-27';
$filter_date['range']['datecreatedsort']['lte']='2017-11-27';
$params1['body']['query']['filtered']['filter']=$filter_date;
$params1['body']['sort']['datecreatedsort']['order'] = 'desc';
try {
$results = $client->search($params1);
//print_r($results);
}
catch (Exception $e) {
$last = $client->transport->getLastConnection()->getLastRequestInfo();
$last['results']['error'] = [];
print_r($last);
}
When I am running above query I am getting following error
[query_string] malformed query, expected [END_OBJECT] but found
[FIELD_NAME]","line":1,"col":78},"status":400}
datecreatedsort filed mapping is date type is date and value in
elastic search db is "datecreatedsort":"2016-05-30T09:39:40.000Z"
please help where is the issue in elastic query.
It's the native elasticsearch request.
In PHP you need to create array query with the same structure.
GET /joborders/_search?pretty=true
{
"query": {
"bool": {
"must": [
{
"range": {
"created_at": {
"gte": "2017-11-22 13:49:00",
"lte": "2017-11-22 23:50:00"
}
}
}
]
}
}
PS You need to pass date ranges in the same format as datecreatedsort in db ("2016-05-30T09:39:40.000Z")
try this:
{
"query": {
"filtered": {
"query": {
"query_string": {
"default_field": "description",
"query": $wildCardString
}
},
"filter": {
"range": {
"datecreatedsort": {
"gte": '2015-11-27',
"lte": '2017-11-27'
}
}
}
}
}
}
your code for the query will look something like this:
$query=array(
'filtered'=>array(
'query' => array(
'query_string' => [
'query' => $wildCardString,
'fields' => ['description'],
]
),
'filter'=>$filter_date
)
);
Hi Got the solution its working fine
$params1 = [
'index' => 'joborders',
'type' => 'joborder',
'body' => [
'query' => [
'bool' => [
'filter' => [
'range' => [ 'date_modified' => ['gt'=>$duration,'lt'=>$today,'boost'=> '2.0'] ]
],
'must' => [
'match' => [ 'description' => $wildCardString ]
]
]
]
]
];
Thanks guys for help.
To initialize my app I have the following route:
/initialize
This returns Taxonomies, Enumerables and a couple of other taxonomy like collections. This saves multiple HTTP requests.
Although with Dingo / Fractal, I cannot see how I can respond with multiple collections?
e.g.
return [
'taxonomies' => $this->response->collection($taxonomies, new TaxonomyTransformer);
'enumerables' => $this->response->collection($enumerables, new EnumerableTransformer);
'otherStuff' => $this->response->collection($otherStuff, new OtherStuffTransformer);
];
return response()->json([
'data' => [
'taxonomies' => $this->fractal->collection($taxonomies, new TaxonomyTransformer);
'enumerables' => $this->fractal->collection($enumerables, new EnumerableTransformer);
'otherStuff' => $this->fractal->collection($otherStuff, new OtherStuffTransformer);
]
], 200);
This should return the JSON in the format you are looking for.
I have the same issue ,and I found the solution from How to use Transformer in one to many relationship. #1054.
Here is the collection I want to return with the transfomer of dingo in my controller.
$user = User::where('email','=',$input['email'])->with('departments')->with('roles')->get();
DepartmentTransformer
class DepartmentTransformer extends TransformerAbstract
{
public function transform($department)
{
return [
'id' => $department['id'],
'name' => $department['name'],
'level' => $department['level'],
'parent_id' => $department['parent_id']
];
}
}
RolesTransformer
class RolesTransformer extends TransformerAbstract
{
public function transform($role)
{
return [
'name' => $role['name'],
'slug' => $role['slug'],
'description' => $role['description'],
'level' => $role['level']
];
}
}
UserTransformer
class UserTransformer extends TransformerAbstract
{
protected $defaultIncludes = ['departments','roles'];
public function transform($user)
{
return [
'id' => $user['id'],
'name' => $user['name'],
'email' => $user['email'],
'phone' => $user['phone'],
];
}
public function includeDepartments(User $user)
{
$dept = $user->departments;
return $this->collection($dept, new DepartmentTransformer());
}
public function includeRoles(User $user)
{
$rl = $user->roles;
return $this->collection($rl, new RolesTransformer());
}
}
In my controller
$user = User::where('email','=',$input['email'])->with('departments')->with('roles')->get();
return $this->response->collection($user, new UserTransformer());
And I got the result
"data": {
{
"id": 43,
"name": "test7",
"email": "test7#foxmail.com",
"phone": "186********",
"departments": {
"data": {
{
"id": 1,
"name": "业务一部",
"level": 1,
"parent_id": 0
}
}
},
"roles": {
"data": {
{
"name": "agent",
"slug": "agent",
"description": "业务员",
"level": 1
}
}
}
}
}
Please take note of the usage of $defaultIncludes and includeXXX() methonds in the UserTransformer.You can get more detail info from Fractal Doc.
I am running a functionality of listing the cities with the rest api url, http://localhost/cities, to get the result set as:
[
{
"status": "SUCCESS",
"totalRecords": 1220,
"cities": [
{
"id": 1,
"name": "New York"
},
{
"id": 2,
"name": "Washington"
}
.....
]
}
]
and this is my code:
Module Config:
'listcities' => array(
'type' => 'segment',
'options' => array(
'route' => '/cities',
'defaults' => array(
'controller' => 'City\Controller\City',
'action' => 'listCities',
),
),
),
City Controller:
public function listCitiesAction() {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");
$response = array();
$cities = $this->libraryService->listCities();
if (isset($cities) && count($cities) > 0) {
$response[] = $this->getCityResponse($cities);
} else {
$response['status'] = 'FAILURE';
$response['errorCode'] = 578;
$response['errorMessage'] = "No cities found";
}
return new JsonModel($response);
}
private function getCityResponse($cities) {
$citiesArray = array();
$response = array();
foreach ($cities as $city) {
$citiesArray[] = ['id' => $city->getId(),
'name' => $city->getName()
];
}
$response['status'] = 'SUCCESS';
$response['totalRecords'] = count($citiesArray);
$response['cities'] = $citiesArray;
return $response;
}
The above code is from the controller, where the service is called and from the service, repository model is called to get the list of cities. So hereby i used the Doctrine2 ORM, to list the cities. My question how can i insert the pagination script in the Doctrine2 ORM.
City Reposity:
public function findAll() {
return $this->entityManager->getRepository('Library\Entity\City')
->findBy(array('active' => true));
}