php CodeIgniter - Неожиданный счет выполнения цикла для населения массива
Я работаю над приложением Codeigniter и сталкиваю неожиданное поведение в цикле Foreach. У меня есть функция Automatic_stock_export_conversion, которая обрабатывает экспорт акций. что $ goord_delivery_detail всегда содержит ровно один элемент, когда эта функция вызывается в сценарии, который я отлаживаю. Тем не менее, я наблюдаю, что: < /p>
операторы, заполняющие $ data_insert (например, $ data_insert [] = $ node;), по -видимому, выполняют один раз на итерацию цикла (как и ожидалось, поскольку цикл работает только один раз).
, но конкретная строка $ t_data_insert [] = $ node; (помечен в приведенном ниже коде), по -видимому, выполняется дважды во время этой итерации в одной цикле. Сам только итерат один раз? Я упускаю что -то фундаментальное в отношении FOREACH в PHP или загрузке запросов CODEIGNITER/MODEL, которая может вызвать это?
public function automatic_stock_export_conversion($stock_export_id){
if(get_option('acc_wh_stock_export_automatic_conversion') == 0){
return false;
}
$this->delete_convert( $stock_export_id, 'stock_export');
$affectedRows = 0;
$this->load->model('warehouse/warehouse_model');
$goods_delivery = $this->warehouse_model->get_goods_delivery($stock_export_id);
if ($goods_delivery->approval != 1) {
return false;
}
$goods_delivery_detail = $this->warehouse_model->get_goods_delivery_detail($stock_export_id);
$this->load->model('currencies_model');
$currency = $this->currencies_model->get_base_currency();
$acc_wh_stock_export_profit_automatic_conversion = get_option('acc_wh_stock_export_profit_automatic_conversion');
$payment_account = get_option('acc_wh_stock_export_payment_account');
$deposit_to = get_option('acc_wh_stock_export_deposit_to');
$profit_payment_account = get_option('acc_wh_stock_export_profit_payment_account');
$profit_deposit_to = get_option('acc_wh_stock_export_profit_deposit_to');
$tax_payment_account = get_option('acc_tax_payment_account');
$tax_deposit_to = get_option('acc_tax_deposit_to');
if($goods_delivery){
if(get_option('acc_close_the_books') == 1){
if(strtotime($goods_delivery->date_c) strtotime(get_option('acc_closing_date'))){
return false;
}
}
$data_insert = [];
$t_data_insert = []; // This array is problematic
foreach ($goods_delivery_detail as $value) {
// --- THIS SECTION IS THE PROBLEM AREA ---
// If the loop runs once, this specific insertion appears to happen twice.
$node = [];
$node['goods_id'] = 1; // Example data
$t_data_insert[] = $node;
// --- END PROBLEM AREA ---
$goods_transaction_detail = $this->accounting_model->get_goods_transaction_detail($value['id']);
$this->db->where('id', $value['commodity_code']);
$item = $this->db->get(db_prefix().'items')->row();
$item_id = 0;
if(isset($item->id)){
$item_id = $item->id;
}
if($goods_transaction_detail){
$item_amount = $goods_transaction_detail->quantity * $goods_transaction_detail->purchase_price;
$profit_item_amount = ($goods_transaction_detail->quantity * $goods_transaction_detail->price) - $item_amount;
$item_total = $item_amount + $profit_item_amount;
}else{
if($item_id != 0){
$item_amount = $value['quantities'] * $item->purchase_price;
}else{
$item_amount = $value['quantities'] * $value['unit_price'];
}
$profit_item_amount = ($value['quantities'] * $value['unit_price']) - $item_amount;
$item_total = $item_amount + $profit_item_amount;
}
$item_automatic = $this->get_item_automatic($item_id);
// ... (rest of your logic for populating $data_insert, which appears to work as expected) ...
// This part is extensive, so I'm omitting for brevity, assuming it's not the source of the problem.
// Example of $data_insert population (executed once per loop iteration):
if($item_amount > 0){
$node = [];
$node['split'] = $payment_account;
$node['account'] = $item_automatic ? $item_automatic->inventory_asset_account : $deposit_to;
$node['item'] = $item_id;
$node['date'] = $goods_delivery->date_c;
$node['debit'] = $item_amount;
$node['tax'] = 0;
$node['sub_type'] = 'inventory';
$node['credit'] = 0;
$node['description'] = '';
$node['rel_id'] = $stock_export_id;
$node['rel_type'] = 'stock_export';
$node['datecreated'] = date('Y-m-d H:i:s');
$node['addedfrom'] = get_staff_user_id();
$data_insert[] = $node;
// ... another node for credit ...
}
} // End of foreach loop
if(!empty($data_insert)){
// This batch insert executes once, inserting multiple records if data_insert has them
$affectedRows = $this->db->insert_batch(db_prefix().'acc_account_history', $data_insert);
}
if(!empty($t_data_insert)){
// This batch insert is called once, but the $t_data_insert array unexpectedly has two entries.
$affectedRows = $this->db->insert_batch(db_prefix().'goods_transaction_detail', $t_data_insert);
}
if ($affectedRows > 0) {
return true;
}
}
return false;
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... population
PHP CodeIgniter - неожиданное количество выполнения цикла для населения массива ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
PHP CodeIgniter - неожиданное количество выполнения цикла для населения массива
Anonymous » » в форуме Php - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Возвращение значения из array_push () является целым числом вместо населения
Anonymous » » в форуме Php - 0 Ответы
- 10 Просмотры
-
Последнее сообщение Anonymous
-