cancel
Showing results for 
Search instead for 
Did you mean: 

Int not saving correctly

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Int not saving correctly

I've set up a compiler of sorts for quicker reads on frontend. However, it's not saving the correct value.

This is the relevant part for saving info to the table:

 

$indexedModel = $this->_indexedCollection->create();
$simple = $this->_productLoader->create()->load($row['simple_id']);

if ($this->deleteGroupedIndex($row['grouped_id'], $simple->getSku()) !== true) {
    return false;
}

switch($row['attr_id'])
{
    case 1: $indexedModel->setData('required_qty', $row['value']); break;
    case 2: $indexedModel->setData('img_number', $row['value']); break;
    case 3: $indexedModel->setData('notes', $row['value']); break;
}

$indexedModel->setData('group_id', $row['grouped_id']);
$indexedModel->setData('sku', $simple->getSku());
$indexedModel->setData('title', $simple->getName());
$indexedModel->setData('img_name', 'product'. $simple->getData('thumbnail'));
$indexedModel->setData('rrp', $simple->getPriceInfo()->getPrice('regular_price')->getValue());
$indexedModel->setData('availability', ($simple->isSaleable() ? 'In Stock' : 'Out of Stock'));

$indexedModel->save();

Every other field works fine, except for the `img_number` field, which gets defined in my schema like this:

->addColumn(
    'img_number',
    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
    11,
    $defaultOpts,
    'Image Number'
)

`$defaultOpts` for reference is just `$defaultOpts = ['nullable' => false];`

in the database:

MariaDB [partsusr_m2]> describe vendor_module_indexed;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| group_id | int(10) unsigned | NO | | NULL | |
| img_number | int(11) | NO | | NULL | |
| sku | varchar(30) | NO | | NULL | |
| title | varchar(100) | NO | | NULL | |
| img_name | varchar(100) | NO | | NULL | |
| notes | varchar(255) | NO | | NULL | |
| rrp | decimal(6,2) | NO | | NULL | |
| required_qty | int(11) | NO | | NULL | |
| availability | varchar(50) | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+

So everything looks like it should work, however, it's getting saved as 0 in the database.

I added this:

 

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/dab.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info($simple->getSku().'::'.$row['value']);

to my case 2 (case 2 = image number) which outputs as:

2020-03-13T16:51:43+00:00 INFO (6): SOME-SKU-001::1
2020-03-13T16:51:43+00:00 INFO (6): SOME-SKU-002::2
2020-03-13T16:51:43+00:00 INFO (6): SOME-SKU-003::3
2020-03-13T16:51:43+00:00 INFO (6): SOME-SKU-004::4
2020-03-13T16:51:44+00:00 INFO (6): SOME-SKU-005::5
2020-03-13T16:51:44+00:00 INFO (6): SOME-SKU-006::6

so, the case is being matched and there's integer values. I even tried typecasting by adding `(int)` before `$row['value']` but still the same result.

 

How do I save my integer value into my custom database? Or, how do I find out why it's using 0 instead of the value?

 

Edit:

I've changed the case 2 to this:

case 2: $indexedModel->setData('img_number', 7); break;

yet, it's still being saved as 0 - so not an issue with the post value being incorrect type/value.

 

2 REPLIES 2

Re: Int not saving correctly

@trey_sinnis In the switch case you are checking the value of  $row['attr_id'] but in the logger you are printng the value of $row['value'].

 

So can you please check if you are getting value 2 for $row['attr_id'].

 

Thanks

 

Re: Int not saving correctly

@Rahul Gupta  That writer only gets triggered if the attr_id = 2, no need to log the attr_id when it's only logging for case 2