Skip to content

#24376 update save/load query building logic, check definition of columns us… #24609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
*/
protected function updateObject(\Magento\Framework\Model\AbstractModel $object)
{
$condition = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $object->getId());
$idFieldValue = $this->getConnection()->prepareColumnValue(
(array)$this->_getColumnDefinition($this->getIdFieldName(), $this->getMainTable()),
$object->getId()
);
$condition = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $idFieldValue);
$data = $this->_prepareDataForSave($object);
unset($data[$this->getIdFieldName()]);
$this->getConnection()->update($this->getMainTable(), $data, $condition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ protected function _prepareDataForTable(DataObject $object, $table)
return $data;
}

/**
* @param string $column
* @param string $table
* @return array
*/
protected function _getColumnDefinition($column, $table)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, we are not allowed to add new protected methods to the @api classes according to our Backward Compatible Development Guide

{
$fields = $this->getConnection()->describeTable($table);
return $fields[$column] ?: null;
}

/**
* Prepare value for save
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public function load(\Magento\Framework\Model\AbstractModel $object, $value, $fi
$object->afterLoad();
$object->setOrigData();
$object->setHasDataChanges(false);

return $this;
}

Expand All @@ -373,6 +373,10 @@ public function load(\Magento\Framework\Model\AbstractModel $object, $value, $fi
*/
protected function _getLoadSelect($field, $value, $object)
{
$value = $this->getConnection()->prepareColumnValue(
(array)$this->_getColumnDefinition($field, $this->getMainTable()),
$value
);
$field = $this->getConnection()->quoteIdentifier(sprintf('%s.%s', $this->getMainTable(), $field));
$select = $this->getConnection()->select()->from($this->getMainTable())->where($field . '=?', $value);
return $select;
Expand Down Expand Up @@ -793,7 +797,11 @@ protected function saveNewObject(\Magento\Framework\Model\AbstractModel $object)
*/
protected function updateObject(\Magento\Framework\Model\AbstractModel $object)
{
$condition = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $object->getId());
$idFieldValue = $this->getConnection()->prepareColumnValue(
(array)$this->_getColumnDefinition($this->getIdFieldName(), $this->getMainTable()),
$object->getId()
);
$condition = $this->getConnection()->quoteInto($this->getIdFieldName() . '=?', $idFieldValue);
/**
* Not auto increment primary key support
*/
Expand Down