Codeigniter insert_batch fail weird array becomes string - codeigniter

So now I am passing a json and here is the result of echo '<pre>' . var_export($data, true) . '</pre>';:
array (
0 =>
array (
'booking_id' => 99,
'item_type' => '67',
'sales_item_seq' => '1',
'package_item_id' => '0',
'package_item_seq' => '0',
'package_group' => '0',
'package_group_rowspan' => '0',
'package_group_subdata_rowspan' => '0',
'channel_id' => '380',
'name_of_channel_programme' => '123',
'schedule_from' => '0000-00-00 00:00:00',
'schedule_to' => '0000-00-00 23:59:59',
'no_of_unit' => '123.00',
'exposure_inside_per_unit' => '',
'exposure_duration' => '123',
'advertising_offier' => '0.00',
'special_discount' => '0',
'net' => '0.00',
't_n_c' => '',
'remark' => '',
'status' => '1',
'create_datetime' => '2018-11-14 17:00:33',
'update_datetime' => '2018-11-14 17:00:33',
'channel' => '2',
'package_name' => '',
'sale_item_name' => 'hello',
'cat_id' => '1',
'aftersales_type' => '',
'typing_guide' => '',
'position' => '',
'platform_des' => 'hello',
'nature_des' => 'hello',
'unit_des' => 'Account',
),
...
...
...
9 =>
array (
'booking_id' => 99,
'item_type' => '59',
'sales_item_seq' => '1',
'package_item_id' => '0',
'package_item_seq' => '0',
'package_group' => '0',
'package_group_rowspan' => '0',
'package_group_subdata_rowspan' => '0',
'channel_id' => '0',
'name_of_channel_programme' => 'N/A',
'schedule_from' => '0000-00-00 00:00:00',
'schedule_to' => '0000-00-00 00:00:00',
'no_of_unit' => '123.00',
'exposure_inside_per_unit' => 'N/A',
'exposure_duration' => 'N/A',
'advertising_offier' => '1000.00',
'special_discount' => '100',
'net' => '0.00',
't_n_c' => '',
'remark' => '',
'status' => '1',
'create_datetime' => '2018-11-14 17:00:33',
'update_datetime' => '2018-11-14 17:00:33',
'channel' => '',
'package_name' => '',
'sale_item_name' => 'Service Charge',
'cat_id' => '7',
'aftersales_type' => '',
'typing_guide' => 'N/A',
'position' => 'N/A',
'platform_des' => 'N/A',
'nature_des' => 'Operations',
'unit_des' => 'Campaign',
),
10 =>
array (
'booking_id' => 99,
'item_type' => '0',
'sales_item_seq' => '0',
'package_item_id' => '161',
'package_item_seq' => '0',
'package_group' => '1',
'package_group_rowspan' => '6',
'package_group_subdata_rowspan' => '1',
'channel_id' => '39',
'name_of_channel_programme' => 'hello',
'schedule_from' => '0000-00-00 00:00:00',
'schedule_to' => '0000-00-00 23:59:59',
'no_of_unit' => '1.00',
'exposure_inside_per_unit' => '0',
'exposure_duration' => '',
'advertising_offier' => '180000.00',
'special_discount' => '100',
'net' => '0.00',
't_n_c' => '',
'remark' => '',
'status' => '1',
'create_datetime' => '2018-11-14 17:00:33',
'update_datetime' => '2018-11-14 17:00:33',
'channel' => 'Catch',
'package_name' => 'hello',
'sale_item_name' => 'hello',
'cat_id' => '1',
'aftersales_type' => '',
'typing_guide' => 'Name of Programme',
'position' => '',
'platform_des' => 'hello',
'nature_des' => 'Live',
'unit_des' => 'Live Segment',
'package_id' => '28',
),
...
...
...
'unit_des' => 'Video',
'package_id' => '28',
),
)
It looks fine to me when I view it on an online viewer. and after $this->db->insert_batch('table_name',$data) here is the error returned saying there is Array in the code.
Error Number: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'Array' at line 1
And I can find it at the end of the followings (think this is the raw output of the insert_batch):
INSERT INTO `table_name` () VALUES
(
'0.00',
'',
100,
'1',
'2',
'380',
'2018-11-14 17:06:46',
'123',
'',
'67',
'123',
'Access Right',
'0.00',
'123.00',
'0',
'0',
'0',
'0',
'0',
'',
'hello',
'',
'',
'hello',
'1',
'0000-00-00 00:00:00',
'0000-00-00 23:59:59',
'0',
'1',
'',
'',
'Account',
'2018-11-14 17:06:46'
)
,
...
...
...
(
'1000.00',
'',
100,
'7',
'',
'0',
'2018-11-14 17:06:46',
'N/A',
'N/A',
'59',
'N/A',
'Operations',
'0.00',
'123.00',
'0',
'0',
'0',
'0',
'0',
'',
'N/A',
'N/A',
'',
'Service Charge',
'1',
'0000-00-00 00:00:00',
'0000-00-00 00:00:00',
'100',
'1',
'',
'N/A',
'Campaign',
'2018-11-14 17:06:46'
)
,
array
I have no idea why there becomes an array for all the elements from 10 th onwards.
Please let me know if you require more information. Thank you!

You're array indexes doesn't match. 1 - 9 doesn't have package_id
You may add this to ensure all array has package_id index:
array_walk($array, function(&$val){ if (!isset($val['package_id'])) $val['package_id'] = null; });

Related

Ruby hash of hashes, how to get key if there is a key value match into the nested hashes

I have a ruby hash with this structure:
jotform = {
'1' => {
'name' => 'theGreat',
'order' => '1',
'text' => 'xxxxx',
'type' => 'control_head'
},
'3' => {
'name' => 'clickTo',
'order' => '2',
'text' => '<p>Final date to apply is August 29</p>',
'type' => 'control_text'
},
'4' => {
'name' => 'personalDetails',
'order' => '3',
'text' => 'Personal Details',
'type' => 'control_head'
},
'5' => {
'name' => 'name',
'order' => '4',
'sublabels' =>
'{"prefix":"Prefix","first":"First Name","middle":"Middle Name","last":"Last Name","suffix":"Suffix"}',
'text' => 'Name',
'type' => 'control_fullname',
'answer' => {
'first' => 'Example',
'last' => 'Example'
},
'prettyFormat' => 'Example'
},
'9' => {
'name' => 'country',
'order' => '5',
'text' => 'Country',
'type' => 'control_dropdown',
'answer' => 'Germany'
},
'10' => {
'name' => 'email',
'order' => '6',
'text' => 'Email',
'type' => 'control_email',
'answer' => 'picco#example.com'
},
'15' => {
'name' => 'pageBreak',
'order' => '8',
'text' => 'Page Break',
'type' => 'control_pagebreak'
},
'16' => {
'name' => 'contributions',
'order' => '9',
'text' => 'Contributions',
'type' => 'control_head'
}
}
This is a payload that I receive from jotform and I have to process in my app.
The issue is that I don't know the field order: this hash has some keys ('1', '2', …) that are equivalent to the form answer.
I must get the name in Pretty Format and the email, so I'm trying to get these values.
Here the email has key '10' and the name has key '5' but they can change in the next form.
I try with
jotform.select { |_key, hash| hash['name'] == 'email' }
But it returns
{
'10' => {
'name' => 'email',
'order' => '6',
'text' => 'Email',
'type' => 'control_email',
'answer' => 'example#example.com'
}
}
I need to get the key ('10') if there is a match into this key's value but I cannot understand how.
From the result of your select call:
h = jotform.select{ |key, hash| hash["name"] == "email" }
You can get the key via:
h.keys.first
#=> "10"
And the value via:
h.values.first["answer"]
#=> "example#example.com"
first is needed because there could (in theory) be multiple entries for email.
Well, that's a weird format.
I'm not sure I understand it, but assuming that name is unique, you could reindex your hash and use name as key:
new_data = jotform.map do |id, h|
key = h.delete 'name'
[key, h.merge('id' => id)]
end.to_h
The new format looks like :
{"theGreat"=>
{"order"=>"1", "text"=>"xxxxx", "type"=>"control_head", "id"=>"1"},
"clickTo"=>
{"order"=>"2",
"text"=>"<p>Final date to apply is August 29</p>",
"type"=>"control_text",
"id"=>"3"},
"personalDetails"=>
{"order"=>"3",
"text"=>"Personal Details",
"type"=>"control_head",
"id"=>"4"},
"name"=>
{"order"=>"4",
"sublabels"=>
"{\"prefix\":\"Prefix\",\"first\":\"First Name\",\"middle\":\"Middle Name\",\"last\":\"Last Name\",\"suffix\":\"Suffix\"}",
"text"=>"Name",
"type"=>"control_fullname",
"answer"=>{"first"=>"Example", "last"=>"Example"},
"prettyFormat"=>"Example",
"id"=>"5"},
"country"=>
{"order"=>"5",
"text"=>"Country",
"type"=>"control_dropdown",
"answer"=>"Germany",
"id"=>"9"},
"email"=>
{"order"=>"6",
"text"=>"Email",
"type"=>"control_email",
"answer"=>"picco#example.com",
"id"=>"10"},
"pageBreak"=>
{"order"=>"8",
"text"=>"Page Break",
"type"=>"control_pagebreak",
"id"=>"15"},
"contributions"=>
{"order"=>"9", "text"=>"Contributions", "type"=>"control_head", "id"=>"16"}}
No information has been lost, and it should be easier to access. In order to get the email address, you can simply use:
new_data["email"]["answer"]
#=> "picco#example.com"

How to sum in multidimentional array?

This is my Array Output
<pre>Array
(
[id] => 1
[leadservice] => Array
(
[minutes] => 34
)
)
<pre>Array
(
[id] => 2
[leadservice] => Array
(
[minutes] => 34
)
)
Your question is tagged with Laravel so if you turn your array into a Laravel collection you can start by grouping your items by their site_id then summing the minutes items in the leadservice items.
Something like:
collect($sitesArray)->groupBy('site_id')->map(function($site) {
return $site->map(function($siteData) {
return data_get($siteData, 'leadservice.minutes');
})->sum();
});
You can also add a third parameter to the data_get() to default any missing minutes data, presumably you'd want to default to 0
* ADD DETAILED EXAMPLE *
public function test()
{
return $this->setData()->groupBy('site_id')->map(function($site) {
return $site->map(function($siteData, $key) {
return data_get($siteData, 'leadservice.minutes');
})->sum();
});
}
public function setData()
{
return collect([
array(
'id' => 1,
'customer_lead_service_id' => 1,
'customer_id' => 94,
'site_id' => 1,
'branch_id' => null,
'start_date' => '2019-10-24',
'end_date' => '2019-10-24',
'price' => 24.00,
'status' => 'completed',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => 2,
'leadservice' => array(
'id' => 1,
'customer_lead_id' => 2,
'branch_id' => 10,
'service_id' => 9,
'service_title' => 'Sentinel Outlet Temperature Monitoring',
'minutes' => 34,
'quantity' => 1,
'type' => 'quarterly',
'contract_start_date' => null,
),
'documents' => array(
'0' => array(
'id' => 54,
'engineer_id' => 94,
'contract_id' => 1,
'original_name' => null,
'document' => '1574832562.mp4',
'document_type' => null,
),
),
'notes' => array(
'0' => array(
'id' => 28,
'customer_contract_id' => 1,
'engineer_id' => 94,
'note' => 'note',
),
),
),
array(
'id' => 2,
'customer_lead_service_id' => 2,
'customer_id' => 95,
'site_id' => 1,
'branch_id' => null,
'start_date' => '2019-10-24',
'end_date' => '2019-10-24',
'price' => 700.00,
'status' => 'running',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => null,
'leadservice' => array(
'id' => 2,
'customer_lead_id' => 3,
'branch_id' => 10,
'service_id' => 10,
'service_title' => 'HWS Vessel Temperature Monitoring Inspection',
'minutes' => 12,
'quantity' => 1,
'type' => 'monthly',
'contract_start_date' => null,
),
'documents' => array(
),
'notes' => array(
),
),
array(
'id' => 3,
'customer_lead_service_id' => 3,
'customer_id' => 96,
'site_id' => 1,
'branch_id' => null,
'start_date' => '2019-10-26',
'end_date' => '2019-10-24',
'price' => 550.00,
'status' => 'new',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => null,
'leadservice' => array(
'id' => 3,
'customer_lead_id' => 2,
'branch_id' => 10,
'service_id' => 11,
'service_title' => 'Hot Water Heater Temperature Monitoring',
'minutes' => 34,
'quantity' => 1,
'type' => 'six_monthly',
'contract_start_date' => null,
),
'documents' => array(
),
'notes' => array(
),
),
array(
'id' => 4,
'customer_lead_service_id' => 4,
'customer_id' => 100,
'site_id' => 2,
'branch_id' => null,
'start_date' => '2019-10-27',
'end_date' => '2019-10-24',
'price' => 24.00,
'status' => 'new',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => null,
'leadservice' => array(
'id' => 4,
'customer_lead_id' => 3,
'branch_id' => 10,
'service_id' => 12,
'service_title' => 'Tank Lid Installation',
'minutes' => 12,
'quantity' => 1,
'type' => 'annually',
'contract_start_date' => null,
),
'documents' => array(
),
'notes' => array(
),
),
array(
'id' => 5,
'customer_lead_service_id' => 5,
'customer_id' => 102,
'site_id' => 2,
'branch_id' => null,
'start_date' => '2019-10-24',
'end_date' => '2019-10-24',
'price' => 45.00,
'status' => 'new',
'confirmation_send_date' => null,
'confirmation_date' => null,
'assign_date' => '2019-11-21',
'assign_to' => 94,
'assign_by' => null,
'assign_note' => null,
'customer_remark' => null,
'reopen_date' => null,
'reopen_by' => null,
'reopen_note' => null,
'total_time' => null,
'leadservice' => array(
'id' => 5,
'customer_lead_id' => 3,
'branch_id' => 10,
'service_id' => 13,
'service_title' => 'Fitting of Tank Insulation',
'minutes' => 14,
'quantity' => 1,
'type' => 'annually',
'contract_start_date' => null,
),
'documents' => array(
),
'notes' => array(
),
),
]);
}
Considering your array name is $data, try below code
$arr = [];
foreach ($data as $value) {
foreach ($value['leadservice'] as $leadservice) {
if(isset($arr[$value['site_id']]['minutes']) && $arr[$value['site_id']]['minutes']!=null){
$arr[$value['site_id']]['minutes'] += $leadservice['minutes'];
}
else {
$arr[$value['site_id']]['site_id'] = $value['site_id'];
$arr[$value['site_id']]['minutes'] = $leadservice['minutes'];
}
}
}

A Database Error Occurred Error Number: It doesn't give a error number

This is the error:
A Database Error Occurred
Error Number:
select a.FOLIO, a.direccion, b.DESC_COLONIA, C.DESC_MPIO
from emergencias#db1 a INNER JOIN AMSGEOCODING#db1 Z ON A.FOLIO=Z.FOLIO inner join cat_colonia#db1 b on A.CVE_COL=b.cve_col and A.CVE_MPIO=b.cve_mpio and A.CVE_ent=b.cve_ent INNER JOIN CAT_MPIO#db1 C ON C.CVE_MPIO =A.CVE_MPIO AND C.CVE_ENT=A.CVE_ENT WHERE a.folio>=8782583 and z.LATITUD =0 and z.LONGITUD<>-1
Filename: C:\wamp\www\myapp\system\database\DB_driver.php
Line Number: 331
What could be wrong with this?
this is my C:\wamp\www\myapp\application\config\database.php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost/orcl',
'username' => 'local',
'password' => 'password',
'database' => 'orcl',
'dbdriver' => 'oci8',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

Magento add attribute on top of the group

I have magento 1.8. and i want to add product attribute in product. I can do all of that but when I want to add specific attribute to be located on top of others (sorting order in back office) in group. How can i do this?
$this->addAttribute('catalog_product', 'my_subtitle', array(
'attribute_model' => NULL,
'backend' => NULL,
'type' => 'text',
'table' => NULL,
'frontend' => NULL,
'input' => 'text',
'label' => 'Subtitle',
'frontend_class' => NULL,
'source' => NULL,
'required' => '0',
'user_defined' => true,
'default' => NULL,
'unique' => '0',
'note' => NULL,
'input_renderer' => NULL,
'visible' => '1',
'searchable' => '1',
'filterable' => '1',
'comparable' => '1',
'visible_on_front' => '1',
'is_html_allowed_on_front' => '1',
'is_used_for_price_rules' => '0',
'filterable_in_search' => '0',
'used_in_product_listing' => '1',
'used_for_sort_by' => '0',
'is_configurable' => '0',
'visible_in_advanced_search' => '0',
'position' => '0',
'wysiwyg_enabled' => '0',
'used_for_promo_rules' => '0',
'apply_to' => Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,));
Try adding:
'sort_order' => -1, // Or 0
Ie.
$this->addAttribute('catalog_product', 'my_subtitle', array(
'sort_order' => -1, // Or 0
...
)
);
This way, your new attribute should be displayed before the rest, as per the sort_order.

Magento programmatically create bundle Product

Where can I find a complete and working example for creating BundleProducts in PHP?
I'm using Magento 1.7
Google gives me only fragments
EDIT:
until now I'm at this code, but the save throws
Fatal error: Call to a member function getStoreId() on a non-object in \app\code\core\Mage\Bundle\Model\Selection.php on line 73
-
---removed old code--
EDIT 2
Ok the exception has gone by registering the product and my bundle appears in the backend, but the bundle items do not show up in the admin panel
$items = array();
$selections = array ();
$items[] = array(
'title' => '',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => 'true',
'position' => 0,
);
$items[] = array(
'title' => '',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => 'true',
'position' => 0,
);
$selection = array();
$selection[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '2',
'delete' => '',
'selection_price_value' => '',
'selection_price_type' => '0',
'selection_qty' => '1',
'selection_can_change_qty' => '0',
'position' => '0',
'is_default' => '1',
);
$selections[] = $selection;
$selection = array();
$selection[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '3',
'delete' => '',
'selection_price_value' => '',
'selection_price_type' => '0',
'selection_qty' => '1',
'selection_can_change_qty' => '0',
'position' => '0',
'is_default' => '1',
);
$selections[] = $selection;
$storeID = 1;
$websiteIDs = array(1);
$product = Mage::getModel('catalog/product');
$p = array(
'sku_type' => 0,
'sku' => '123321',
'name' => "BarProduct",
'description' => 'Foo',
'short_description' => 'Bar',
'type_id' => 'bundle',
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 0,
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => $storeID,
'website_ids' => $websiteIDs
);
$product->setData($p);
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setBundleOptionsData($items);
$product->setBundleSelectionsData($selections);
$product->setCanSaveBundleSelections(true);
$product->setCanSaveConfigurableAttributes(false);
$product->setAffectBundleProductSelections(true);
$product->setCanSaveCustomOptions(true);
$product->save();
Ok this code is working for me now:
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
$items = array();
$selections = array ();
$items[] = array(
'title' => 'o1',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => '1',
'position' => '0',
);
$items[] = array(
'title' => 'o2',
'option_id' => '',
'delete' => '',
'type' => 'radio',
'required' => '1',
'position' => '0',
);
$selection = array();
$selection[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '2',
'delete' => '',
'selection_price_value' => '',
'selection_price_type' => '0',
'selection_qty' => '1',
'selection_can_change_qty' => '0',
'position' => '0',
'is_default' => '1',
);
$selections[] = $selection;
$selection = array();
$selection[] = array(
'selection_id' => '',
'option_id' => '',
'product_id' => '3',
'delete' => '',
'selection_price_value' => '',
'selection_price_type' => '0',
'selection_qty' => '1',
'selection_can_change_qty' => '0',
'position' => '0',
'is_default' => '1',
);
$selections[] = $selection;
$storeID = 1;
$websiteIDs = array(1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$cats = array(3);
$p = array(
'sku_type' => 1,
'sku' => '123321',
'name' => "BarProduct",
'description' => 'Foo',
'short_description' => 'Bar',
'type_id' => 'bundle',
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 1,
'price_view' => 0,
'price' => 1.99,
'has_options' => 1,
'required_options' => 1,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => 0,
'website_ids' => $websiteIDs,
'weight' => 0,
'weight_type' => 1,
'delivery_time' => '',
'generate_meta' => 1,
'tax_class_id' => 1, //19%
);
$product->setData($p);
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
// Set the Bundle Options & Selection Data
$product->setBundleOptionsData($items);
$product->setBundleSelectionsData($selections);
$product->setCanSaveBundleSelections(true);
$product->setAffectBundleProductSelections(true);
$product->save();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');
$processes->walk('reindexEverything');

Resources