Hanya untuk memperjelas jawaban Jaimin:
Ini akan berfungsi untuk entitas apa pun.
Ini tidak benar. Ini hanya akan bekerja untuk entitas EAV yang diperluasMagento\Eav\Model\Entity\AbstractEntity
Jika Anda berurusan dengan entitas non EAV di mana model sumber daya meluas Magento\Framework\Model\ResourceModel\Db\AbstractDbAnda harus menerapkan saveAttributemetode dalam model sumber daya Anda.
Di Magento 2, mereka telah melakukannya untuk Magento\Sales\Model\ResourceModel\Attributekelas:
public function saveAttribute(AbstractModel $object, $attribute)
{
if ($attribute instanceof AbstractAttribute) {
$attributes = $attribute->getAttributeCode();
} elseif (is_string($attribute)) {
$attributes = [$attribute];
} else {
$attributes = $attribute;
}
if (is_array($attributes) && !empty($attributes)) {
$this->getConnection()->beginTransaction();
$data = array_intersect_key($object->getData(), array_flip($attributes));
try {
$this->_beforeSaveAttribute($object, $attributes);
if ($object->getId() && !empty($data)) {
$this->getConnection()->update(
$object->getResource()->getMainTable(),
$data,
[$object->getResource()->getIdFieldName() . '= ?' => (int)$object->getId()]
);
$object->addData($data);
}
$this->_afterSaveAttribute($object, $attributes);
$this->getConnection()->commit();
} catch (\Exception $e) {
$this->getConnection()->rollBack();
throw $e;
}
}
return $this;
}