how to update data in database table
-
26-02-2021 - |
質問
I am sending data to the database by checking duplicate data. But I can't be inserting/updating data in the database.
protected function saveEntityFinish(array $entityData, $table) {
$collection= $this->PostTableFactory->create();
$item = $collection->getCollection();
if ($entityData) {
$tableName = $this->_connection->getTableName($table);
$entityIn = [];
$entityInup = [];
foreach ($entityData as $id => $entityRows) {
foreach ($entityRows as $row) {
$item->addFieldToFilter('rule_id',$row['rule_id']);
$item->addFieldToFilter('store_id',$row['store_id']);
if(count($item->getData()) == 0){
$temp= $item->getData();
$logger->info('one');
$collection->setRuleId($row['rule_id']);
$collection->setStoreId($row['store_id']);
$collection->setStatus($row['status']);
$entityIn[] = $row;
$collection->save();
}else{
$entityInup = $row;
$collection = $this->PostTableFactory->create();
$collection->load($item->getData()[0]['p_id']);
$collection->setRuleId($entityInup['rule_id']);
$collection->setStoreId($entityInup['store_id']);
$collection->setStatus($entityInup['status']);
$entityIn[] = $entityInup;
$collection->save();
}
}
}
}
return $this;
}
can I get help on it? Thank you in advance
解決 2
To update a row we have to add that column id attached with updated data in that row will update the row
他のヒント
I have updated my value using CollectionFactory. You can give it a try
$query = $this->_YourCollection->create()
->addFieldToFilter('field_id', $value)
->addFieldToFilter('filed',$value2)->getFirstItem();
if ($query->getData()){
if ($yourCondition){
$query->setData('field','valuefor update');
$query->save();
}
}
所属していません magento.stackexchange