How to sum in multidimentional array? - laravel

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'];
}
}
}

Related

Elastica Mapping include_type_name

I'm trying to create a mapping from Elastica.
Here is my code to create my index with params of mapping :
$elasticaClient = new ElasticaClient;
$elasticaIndex = $elasticaClient->getIndex('products');
$elasticaIndex->create(
array(
'settings' => array(
'number_of_shards' => 1,
'number_of_replicas' => 0,
'analysis' => array(
'filter' => array(
'french_elision' => array(
'type' => 'elision',
'articles_case' => 'true',
'articles' => array("l", "m", "t", "qu", "n", "s", "j", "d", "c", "jusqu", "quoiqu", "lorsqu", "puisqu"),
),
'french_synonym' => array(
"type" => "synonym",
"ignore_case" => true,
"expand" => true,
"synonyms" => []
),
'french_stemmer' => array(
"type" => "stemmer",
"language" => "light_french"
)
),
'analyzer' => array(
'french_heavy' => array(
'type' => 'custom',
'tokenizer' => 'icu_tokenizer',
'filter' => array('french_elision', 'icu_folding', 'french_synonym', 'french_stemmer', 'asciifolding', 'lowercase')
),
'french_light' => array(
'type' => 'custom',
'tokenizer' => 'icu_tokenizer',
'filter' => array('french_elision', 'icu_folding', 'lowercase', 'asciifolding')
)
)
)
)
),
true
);
$elasticaType = $elasticaIndex->getType('product');
$mapping = new $mapping = new ElasticaTypeMapping;
$mapping->setType($elasticaType);
$mapping->setParam('index_analyzer', 'french_heavy');
$mapping->setParam('search_analyzer', 'french_light');
$mapping->setProperties(
array(
'name' => array(
'type' => 'string',
'boost' => 4
),
'description' => array(
'type' => 'string',
'boost' => 2
),
)
);
$mapping->send();
But I'm having the following error:
Types can not be provided in put mapping requests, unless the include_type_name parameter is set to true.
I can't find how to pass "include_type_name = true" in Ruflin/Elastica.
All my searches return examples in CURL ..
Thank a lot to help me
You seem to be running ES7 and include_type_name is false by default.
You can prevent that error from occurring by changing the last line with this one:
$mapping->send(['include_type_name' => true]);
It's WORK !!
Here is my config :
PHP 7.3.4
Symfony 4.3
Elasticsearch 7.0.1
composer require elasticsearch/elasticsearch "dev-master"
composer require ruflin/elastica "dev-master"
$elasticaClient = new ElasticaClient;
$elasticaIndex = $elasticaClient->getIndex('products');
$elasticaType = $elasticaIndex->getType('product');
$elasticaIndex->create(
array(
'settings' => array(
'number_of_shards' => 1,
'number_of_replicas' => 0,
'analysis' => array(
'filter' => array(
'french_elision' => array(
'type' => 'elision',
'articles_case' => 'true',
'articles' => array("l", "m", "t", "qu", "n", "s", "j", "d", "c", "jusqu", "quoiqu", "lorsqu", "puisqu"),
),
'french_synonym' => array(
"type" => "synonym",
"ignore_case" => true,
"expand" => true,
"synonyms" => []
),
'french_stemmer' => array(
"type" => "stemmer",
"language" => "light_french"
),
),
'analyzer' => array(
'french_heavy' => array(
'type' => 'custom',
'tokenizer' => 'icu_tokenizer',
'filter' => array('french_elision', 'icu_folding', 'french_synonym', 'french_stemmer', 'asciifolding', 'lowercase')
),
'french_light' => array(
'type' => 'custom',
'tokenizer' => 'icu_tokenizer',
'filter' => array('french_elision', 'icu_folding', 'lowercase', 'asciifolding')
),
)
)
)
),
true
);
$mapping = new ElasticaTypeMapping;
$mapping->setType($elasticaType);
$mapping->setProperties(
array(
'name' => array(
'type' => 'text',
'analyzer' => 'french_heavy',
'boost' => 4
),
'description' => array(
'type' => 'text',
'analyzer' => 'french_light',
'boost' => 2
),
)
);
$mapping->send(['include_type_name' => true]);

How to configure an entity as part of the second-level cache in DoctrineORMModule?

I was trying to configure memcached in DoctrineORM module of my Zendframework 2 project to perform caching queries, results and metadata follow this tutorial https://github.com/doctrine/DoctrineORMModule/blob/master/docs/cache.md
In module.config.php
<?php
return array(
'service_manager' => array(
'invokables' => array(
// ...
),
'factories' => array(
'doctrine.cache.my_memcached' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcachedCache();
$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
$cache->setMemcached($memcache);
return $cache;
},
'MemcachedFactory' => \Application\Service\Cache\MemcachedFactory::class,
)
),
'doctrine' => array(
'cache' => array(
'memcached' => array(
'namespace' => '_Doctrine',
'instance' => 'MemcachedFactory',
),
),
'configuration' => array(
'orm_default' => array(
'query_cache' => 'memcached',
'result_cache' => 'memcached',
'metadata_cache' => 'memcached',
'hydration_cache' => 'memcached',
'second_level_cache' => [
'enabled' => true,
'default_lifetime' => 200,
'default_lock_lifetime' => 500,
'file_lock_region_directory' => './data/cache',
'regions' => [
'My\FirstRegion\Name' => [
'lifetime' => 800,
'lock_lifetime' => 1000
],
'My\SecondRegion\Name' => [
'lifetime' => 10,
'lock_lifetime' => 20
],
],
],
),
),
'driver' => array(
'_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'my_memcached',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => '_driver'
),
),
),
),
);
and after I create a method as the following:
public function findById($id) {
$qb = $this->entityManager->createQueryBuilder();
$qb->select("u")
->from(User::class, "u")
->where($qb->expr()->eq("u.id", ":id"))
->setParameter("id", (int) $id);
return $qb->getQuery()->setCacheable(true)->getOneOrNullResult();
}
and after, I get a Doctrine\ORM\Cache\CacheException
with message:
Entity "Application\Entity\User" not configured as part of the second-level cache.
Anyone who can suggest for me to solved this problem?
Add this to the USER class:
* #ORM\Cache(usage="NONSTRICT_READ_WRITE")

Symfony 1.4 doctrine create table

Can someone show me an example on how to use the createTable in Doctrine?
For example, I'd like to create a table 'attachment' with the following columns:
'file_path' =>string
'message_id'=>integer
Thanks
Found it :
$this->createTable('attachment', array(
'id' =>
array(
'type' => 'integer',
'length' => 8,
'autoincrement' => true,
'primary' => true,
),
'file_path' =>
array(
'type' => 'string',
'notnull' => true,
'length' => 255,
),
'message_id' =>
array(
'type' => 'integer',
'notnull' => false,
'length' => 8,
),
'created_at' =>
array(
'notnull' => true,
'type' => 'timestamp',
'length' => 25,
),
'updated_at' =>
array(
'notnull' => true,
'type' => 'timestamp',
'length' => 25,
),
), array(
'indexes' =>
array(
),
'primary' =>
array(
0 => 'id',
),
'collate' => 'utf8_general_ci',
'charset' => 'utf8',
));

Does Magento supports attribute with type date, not datetime

I have problem with this format and I don't know does magento support it.
In code I have:
$this->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'release_date', array(
'input' => 'date',
'type' => 'datetime',
'label' => 'Release date',
'backend' => 'eav/entity_attribute_backend_datetime',
'visible' => true,
'required' => false,
'user_defined' => true,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
));
How to change type to date? And how to make it dependable from Magento locale?
Here solution....
$data = array(
"attribute_code" => "date_attribute_3",
"frontend_input" => "date",
"default_value" => "06/19/2014",
"is_unique" => 0,
"is_global"=>"1",
"is_required" => 0,
"apply_to" => array("simple"),
"is_configurable" => 0,
"is_searchable" => 0,
"is_visible_in_advanced_search" => 0,
"is_comparable" => 0,
//"is_global" =>1,
"is_used_for_promo_rules" => 0,
"is_visible_on_front" => 0,
"used_in_product_listing" => 0,
"additional_fields" => array(),
"frontend_label" => array(array("store_id" => "0", "label" => "my date"))
);
$Attribute=Mage::getModel('catalog/product_attribute_api')->create($data);

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