我有一个包含许多数组的数组,我希望使用saveAll
或saveMany
保存数据。但是,由于我无法更改的因素,并非所有数组实际上都包含任何数据。在转储中,您可以看到,只有前两个数组被填充。
使用saveAll
或saveMany
保存此数组时,收到以下错误:Integrity constraint violation: 1048 Column 'productinfo_id' cannot be null
。
使用saveAll或saveMany保存数组时,如何避免此错误?
array(
(int) 1 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '68',
'quantity' => '30',
'productName' => 'ciplorex syp 125',
'pack' => '60ml',
'purchasePrice' => '49.30',
'grossAmount' => '1479.00',
'discountPercent' => '20.00',
'discountAmount' => '295.80',
'netAmount' => '1183.20'
),
(int) 2 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '69',
'quantity' => '27',
'productName' => 'ciplorex syp 250',
'pack' => '60ml',
'purchasePrice' => '85.00',
'grossAmount' => '2295.00',
'discountPercent' => '20.00',
'discountAmount' => '459.00',
'netAmount' => '1836.00'
),
(int) 3 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 4 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 5 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 6 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 7 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 8 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 9 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 10 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 11 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 12 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 13 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 14 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 15 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 16 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 17 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 18 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 19 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 20 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 21 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 22 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 23 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 24 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 25 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 26 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 27 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 28 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 29 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
),
(int) 30 => array(
'purchaseinfo_id' => '7',
'productinfo_id' => '',
'quantity' => '',
'productName' => '',
'pack' => '',
'purchasePrice' => '',
'grossAmount' => '',
'discountPercent' => '',
'discountAmount' => '',
'netAmount' => ''
)
)
答案 0 :(得分:1)
在保存数组之前,我建议循环它并过滤空数组。例如:
$originalArray = #Your Data;
$saveArray = array();
foreach($originalArray as $item) {
if(!empty($item['productinfo_id'])) {
$saveArray[] = $item;
}
}
然后,您可以将新数组提供给saveAll
方法。
答案 1 :(得分:1)
完整性约束违规:1048列'productinfo_id'不能为空
我假设你明白这意味着什么?您需要将值传递到此字段,不能保存空行productinfo_id
的表行。我认为这应该被称为product_info_id
。
因此,不像Lars所建议的那样过滤空数组,我首先要问的问题是:为什么它们会出现?我会避免这种情况。
避免在SQL错误发生时遇到的硬错误的正确解决方案是在模型中to use validation,以确保根本无法保存记录并且在空{{}上验证失败1}}。这将确保一种优雅而有意义的方式来处理这个问题。
除此之外,您还可以过滤掉beforeSave()回调中的“空”关联记录。但这是不是的主要修复方法,验证是。