why is my conditional result_select function not working? - codeigniter

result_select('keyword', ['ctv' => $ctv_id,'finish' => 1, 'finish' => 3] ,[]);
My code helps me access the database and retrieve it with 3 conditions ('ctv' => $ctv_id, 'finish' => 1,
'finish' => 3 ) but my #2 condition is not working ( 'finish' => 1 ), I don't understand where am I wrong?

Related

Laravel Collection with groupby, count and sum

I'm struggling to get a groupby on a collection to work - I'm not getting the concept just yet.
I'm pulling a collection of results from a table for a player the eloquent collection will have data like this:
['player_id'=>1, 'opposition_id'=>10, 'result'=>'won', 'points'=>2],
['player_id'=>1, 'opposition_id'=>11, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>12, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>10, 'result'=>'won', 'points'=>2],
['player_id'=>1, 'opposition_id'=>11, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>10, 'result'=>'lost', 'points'=>0],
['player_id'=>1, 'opposition_id'=>12, 'result'=>'won', 'points'=>2],
I want to be able to groupBy('opposition_id') and then give me a count of results in total, total won, total lost and sum of points to end up with a collection like this:
['opposition_id'=>10, 'results'=>3, 'won'=>2, 'lost'=>1, 'points'=>4],
['opposition_id'=>11, 'results'=>2, 'won'=>0, 'lost'=>2, 'points'=>0],
['opposition_id'=>10, 'results'=>2, 'won'=>1, 'lost'=>1, 'points'=>2]
I'm trying to avoid going back to the database to do this as I already have the results from previous activity.
How can I do this using Laravel collection methods, So far all I have is:
$stats = $results->groupBy('opposition_id');
I've looked at map() but do not yet understand that method to work through a solution
Can anyone point me in the right direction please.
Happy to go back to the database if needed but assumed I could do this with the collection I already have rather than create another query. Solutions I've found on here all appear to be providing a solution in the query
Thank you
Take a look here, working code with explanation in comments.
// make a collection
$c = collect(
[
['player_id' => 1, 'opposition_id' => 10, 'result' => 'won', 'points' => 2],
['player_id' => 1, 'opposition_id' => 11, 'result' => 'lost', 'points' => 0],
['player_id' => 1, 'opposition_id' => 12, 'result' => 'lost', 'points' => 0],
['player_id' => 1, 'opposition_id' => 10, 'result' => 'won', 'points' => 2],
['player_id' => 1, 'opposition_id' => 11, 'result' => 'lost', 'points' => 0],
['player_id' => 1, 'opposition_id' => 10, 'result' => 'lost', 'points' => 0],
['player_id' => 1, 'opposition_id' => 12, 'result' => 'won', 'points' => 2]
]
);
// this only splits the rows into groups without any thing else.
// $groups will be a collection, it's keys are 'opposition_id' and it's values collections of rows with the same opposition_id.
$groups = $c->groupBy('opposition_id');
// we will use map to cumulate each group of rows into single row.
// $group is a collection of rows that has the same opposition_id.
$groupwithcount = $groups->map(function ($group) {
return [
'opposition_id' => $group->first()['opposition_id'], // opposition_id is constant inside the same group, so just take the first or whatever.
'points' => $group->sum('points'),
'won' => $group->where('result', 'won')->count(),
'lost' => $group->where('result', 'lost')->count(),
];
});
// if you don't like to take the first opposition_id you can use mapWithKeys:
$groupwithcount = $groups->mapWithKeys(function ($group, $key) {
return [
$key =>
[
'opposition_id' => $key, // $key is what we grouped by, it'll be constant by each group of rows
'points' => $group->sum('points'),
'won' => $group->where('result', 'won')->count(),
'lost' => $group->where('result', 'lost')->count(),
]
];
});
// here $groupwithcount will give you objects/arrays keyed by opposition_id:
[
10 => ["opposition_id" => 10,"points" => 4,"won" => 2,"lost" => 1]
11 => ["opposition_id" => 11,"points" => 0,"won" => 0,"lost" => 2]
12 => ["opposition_id" => 12,"points" => 2,"won" => 1,"lost" => 1]
]
// if you use $groupwithcount->values() it'll reset the keys to 0 based sequence as usual:
[
0 => ["opposition_id" => 10,"points" => 4,"won" => 2,"lost" => 1]
1 => ["opposition_id" => 11,"points" => 0,"won" => 0,"lost" => 2]
2 => ["opposition_id" => 12,"points" => 2,"won" => 1,"lost" => 1]
]

Strange assertJSON behaviour

I want to test, whether an AJAX response contains the array I'm expecting.
So far so good, not really a great deal.
This is how my array should look like:
array (
'data' =>
array (
0 =>
array (
'key1' => 'value1',
'key2' => 'value2,
),
1 =>
array (
'key1' => 'value3',
'key2' => "value4",
),
),
)
When I run my test:
$request->assertJson([the array mentioned above]);
The array really looks like that but it fails anyway. Why?
because in reality it expects the array twice.
In the comparison window, I see that it expects this:
array (
'data' =>
array (
0 =>
array (
'key1' => 'value1',
'key2' => 'value2,
),
1 =>
array (
'key1' => 'value3',
'key2' => "value4",
),
),
0 =>
array (
'key1' => 'value1',
'key2' => 'value2,
),
1 =>
array (
'key1' => 'value3',
'key2' => "value4",
),
)
But got the array mentioned above (which would be what I expect too).
When I run $request->assertJSON([]); the test succeeds but this can't be the way it's supposed to work, is it?
This is not a real answer (in terms of solving the underlying problem), but as I'm supposing this to be a bug, I want to share a workaround with people who encounter this problem too:
It's pretty simple. Just store the json into a variable $array = $request->json() (assuming that the response is saved into the $request variable.
Then test the contained array.
$this->assertEquals(EXPECTED_DATA, ARRAY_TO_TEST).

Getting data from Laravel Collection

I realize this is a dumb question, but I can't find an answer for it. I am missing some understanding around how collections work
I am querying a google places API for information about a location
$response = GooglePlaces::textSearch('10 Downing Street', $optionnalParameters);
This works and gives me a response, which I can var_export:
Illuminate\Support\Collection::__set_state(array(
'items' =>
array (
'html_attributions' =>
array (
),
'results' =>
Illuminate\Support\Collection::__set_state(array(
'items' =>
array (
0 =>
array (
'formatted_address' => '10 Downing St, London SW1A 2AA, United Kingdom',
'geometry' =>
array (
'location' =>
array (
'lat' => 51.503363499999999,
'lng' => -0.12762480000000001,
),
'viewport' =>
array (
'northeast' =>
array (
'lat' => 51.503432400000001,
'lng' => -0.12551999999999999,
),
'southwest' =>
array (
'lat' => 51.503156800000014,
'lng' => -0.12832640000000001,
),
),
),
'icon' => 'https://maps.gstatic.com/mapfiles/place_api/icons/civic_building-71.png',
'id' => 'ffd85a3e543406e34703ee947c73ef54f5e167fe',
'name' => '10 Downing Street',
'photos' =>
array (
0 =>
array (
'height' => 2534,
'html_attributions' =>
array (
0 => 'Christopher Chan',
),
'photo_reference' => 'CoQBdwAAAHdqDNRUhalI7Ko_YXqckWM5M7I1IeD0xXOvjnxruS4BYCFnt99lCEy5xQJh7XtTvGTfZbKlnVhbxaJ_OloLxaPyoInqIpgRY-3LyB3Q70tDX3izeraFEM4Bw-ExmRzz6h18iMQlKb0DoDXnW26uO4RR-7YFjPNi6M0y5D7cmrl9EhAmI7GerM-TXpKD3BVVTtOWGhQZotS0ZNI2nK9G7jXxDEyvoQ5IxQ',
'width' => 3801,
),
),
'place_id' => 'ChIJRxzRQcUEdkgRGVaKyzmkgvg',
'rating' => 3.3999999999999999,
'reference' => 'CmRSAAAAh-Drhh9G_EW3azxZSikW_jR-ZjI2lhZTw6MfWqh9EiTaCEy4uW2okv_g6QfaKoupoeDu3DpfS6MjIvUvp6cc2uAoLlyH9NbkMrnQg9q3ED3R91OejmAScjRhe8G47kJ1EhD_oYVFakm4n6I27j-8iN6oGhQVH6rv1t6uGQBVVOqy1fCAB17JOg',
'types' =>
array (
0 => 'point_of_interest',
1 => 'establishment',
),
),
),
)),
'status' => 'OK',
),
))
I want to get the formatted_address element from the collection. it appears under the results element, that in turn has a collection under it with elements Items,0,formatted_address.
when i start trying to 'walk down' to the element i require I get index not found for Items
var_export($response['results']['items']);
so essentially, how am I supposed to interact with collections? i have tried google but most the links tell me about the extra functions that collections provide and not how to get data from it
any help greatly appreciated.
I think https://laravel.com/docs/5.3/collections#method-all explains what you are missing. You want to call
->all()
on your Collection to return the underlying array represented by the collection.
Than you can access fields in the returned array.

Iterating an array via Smarty in SugarCRM

When iterating an array in Smarty I am getting results conflicting with a print_r() of the same array.
When doing a print_r() on the array I can see that there are 2 objects in the array and each object is unique. However, when I run a print_r() on each item of the array (via foreach loop) I seem to get the same results for each item.
Can anyone identify what I'm doing wrong?
See the below code examples and results.
Code:
{$fields.rgggo_spreadsheetcellmap_documents.value->beans|#print_r}
Ouput:
Array
(
[a852b076-a5cb-dda9-3868-52010d6957ab] => RGGGO_SpreadsheetCellMap Object
(
[new_schema] => 1
[module_dir] => RGGGO_SpreadsheetCellMap
[object_name] => RGGGO_SpreadsheetCellMap
[table_name] => rgggo_spreadsheetcellmap
[importable] =>
[id] => a852b076-a5cb-dda9-3868-52010d6957ab
[name] =>
[date_entered] => 08/06/2013 09:52am
[date_modified] => 08/06/2013 02:16pm
[modified_user_id] => 1
[modified_by_name] => Sean
[created_by] => 1
[created_by_name] => Sean
[description] => Test 2
[deleted] => 0
[created_by_link] =>
[modified_user_link] =>
[team_id] => 2e201ae4-fac9-8426-44c5-4f54f5830831
[team_set_id] => 65bd59b2-b3ed-fb2b-6a85-4f8888229942
[team_count] =>
[team_name] => Development
[team_link] =>
[team_count_link] =>
[teams] =>
[assigned_user_id] =>
[assigned_user_name] =>
[assigned_user_link] =>
[field] => document_type_text_c
[row] => 3
[col] => 1
.... a bunch more stuff here
)
[f038918d-a13d-69f0-3541-51fff82f0497] => RGGGO_SpreadsheetCellMap Object
(
[new_schema] => 1
[module_dir] => RGGGO_SpreadsheetCellMap
[object_name] => RGGGO_SpreadsheetCellMap
[table_name] => rgggo_spreadsheetcellmap
[importable] =>
[id] => f038918d-a13d-69f0-3541-51fff82f0497
[name] =>
[date_entered] => 08/05/2013 02:10pm
[date_modified] => 08/06/2013 02:17pm
[modified_user_id] => 1
[modified_by_name] => Sean
[created_by] => 1
[created_by_name] => Sean
[description] =>
[deleted] => 0
[created_by_link] =>
[modified_user_link] =>
[team_id] => 2e201ae4-fac9-8426-44c5-4f54f5830831
[team_set_id] => 65bd59b2-b3ed-fb2b-6a85-4f8888229942
[team_count] =>
[team_name] => Development
[team_link] =>
[team_count_link] =>
[teams] =>
[assigned_user_id] =>
[assigned_user_name] =>
[assigned_user_link] =>
[field] => description
[row] => 2
[col] => 1
.... a bunch more stuff here
)
)
When I iterate the array...
{{foreach from=$fields.rgggo_spreadsheetcellmap_documents.value->beans key=k item=v}}
<pre>
{$v|#print_r}
</pre>
{{/foreach}}
I get these results:
RGGGO_SpreadsheetCellMap Object
(
[new_schema] => 1
[module_dir] => RGGGO_SpreadsheetCellMap
[object_name] => RGGGO_SpreadsheetCellMap
[table_name] => rgggo_spreadsheetcellmap
[importable] =>
[id] => f038918d-a13d-69f0-3541-51fff82f0497
[name] =>
[date_entered] => 08/05/2013 02:10pm
[date_modified] => 08/06/2013 02:17pm
[modified_user_id] => 1
[modified_by_name] => Sean
[created_by] => 1
[created_by_name] => Sean
[description] =>
[deleted] => 0
[created_by_link] =>
[modified_user_link] =>
[team_id] => 2e201ae4-fac9-8426-44c5-4f54f5830831
[team_set_id] => 65bd59b2-b3ed-fb2b-6a85-4f8888229942
[team_count] =>
[team_name] => Development
[team_link] =>
[team_count_link] =>
[teams] =>
[assigned_user_id] =>
[assigned_user_name] =>
[assigned_user_link] =>
[field] => description
[row] => 2
[col] => 1
.... a bunch more stuff here
)
RGGGO_SpreadsheetCellMap Object
(
[new_schema] => 1
[module_dir] => RGGGO_SpreadsheetCellMap
[object_name] => RGGGO_SpreadsheetCellMap
[table_name] => rgggo_spreadsheetcellmap
[importable] =>
[id] => f038918d-a13d-69f0-3541-51fff82f0497
[name] =>
[date_entered] => 08/05/2013 02:10pm
[date_modified] => 08/06/2013 02:17pm
[modified_user_id] => 1
[modified_by_name] => Sean
[created_by] => 1
[created_by_name] => Sean
[description] =>
[deleted] => 0
[created_by_link] =>
[modified_user_link] =>
[team_id] => 2e201ae4-fac9-8426-44c5-4f54f5830831
[team_set_id] => 65bd59b2-b3ed-fb2b-6a85-4f8888229942
[team_count] =>
[team_name] => Development
[team_link] =>
[team_count_link] =>
[teams] =>
[assigned_user_id] =>
[assigned_user_name] =>
[assigned_user_link] =>
[field] => description
[row] => 2
[col] => 1
.... a bunch more stuff here
)
I found the answer in another stackoverflow question. The issue is double vs single curly brackets and their use in SugarCRM.
Is there a difference between using a single vs. double brace (delimiters) in SugarCRM Smarty tpls command construct?

programmatically add child products of bundle product in Quote

I am trying to create order programmatically. it is working fine with simple product but when i am trying with bundle product it will not add child product.
below is the code for that
$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
$newProd = Mage::getModel('catalog/product')->load($sku);
$option= array(
"bundle_option" => array(
"35" => 66,
"36" => 67
),
'qty' => 1
);
$quote->addProduct($newProd, new Varien_Object($option));
please any one have solution for that or where am i wrong?
thanks
I think you should check the Bundle-options array because in Bundled item there are various input type such as radio (which takes one input) checkbox (takes multiple product) similarly there are drop down as well as multiple select.
[bundle_option] => Array
(
[5] => 13
[6] => Array
(
[0] => 23
)
[8] => Array
(
[0] => 37
)
[12] => Array
(
[0] => 56
)
)
[bundle_option_qty] => Array
(
[5] => 1
[6] => Array
(
[23] => 1
)
[8] => Array
(
[37] => 1
)
[12] => Array
(
[56] => 1
)
)
Here is an bundle product option where [5] is an radio button and will have one product ,[6],[8] is checkox and can have more product in it and so on.
Similarly you have to maintain the product quantity as shown above.
Hope this would help you.

Resources