วิธีบันทึกเฉพาะค่าคุณลักษณะเฉพาะแทนที่จะบันทึกผลิตภัณฑ์ทั้งหมดใน Magento2


10

อย่างที่คุณรู้แล้วว่าเราเคยมีวิธีการด้านล่างในวีโอไอพีเพื่อบันทึกค่าคุณลักษณะเฉพาะเช่นนี้

// saving product attribute
$product = Mage::getModel('catalog/product')->load('id here');
$product->setName('your name here');
$product->getResource()->saveAttribute($product, 'name');

หรือ

// saving customer attribute
$customer->setData($attrCode, $value)->getResource()->saveAttribute($customer, $attrCode);

ทุกคนสามารถแจ้งให้ทราบว่าทางเลือกของการดังกล่าวข้างต้นใน Magento2

คำตอบ:


8

มันเหมือนกับ Magento 1

$dataobject->setData('attribute_code', $value);
$dataobject->getResource()->saveAttribute($dataobject, 'attribute_code');

สิ่งนี้จะใช้ได้กับเอนทิตีใด ๆ

ตามคำตอบของ @ Raphael มันจะไม่ทำงานกับคุณลักษณะการขาย

โดยทั่วไปจะเรียกMagento \ Eav \ Model \ Entity \ AbstractEntity :: saveAttribute ()ฟังก์ชั่น

วิธีนี้จะยอมรับพารามิเตอร์สองตัว

saveAttribute(\Magento\Framework\DataObject $object, $attributeCode)

ก่อนอื่นที่จะ$objectเป็นวัตถุที่จะต้องมีการปรับปรุงและพารามิเตอร์ที่สองจะ$attributeCodeเป็นรหัสสำหรับแอตทริบิวต์ที่จะปรับปรุง


มันควรจะเป็นพื้นสำหรับหน่วยงานใด ๆ
Kingshuk Deb

ใช่มันจะทำงานสำหรับนิติบุคคลใด ๆ โดยทั่วไปจะเรียกMagento\Eav\Model\Entity\AbstractEntity::saveAttribute()ซึ่งจะยอมรับ dataobject และรหัสเอนทิตี
Jaimin Sutariya

การลงทะเบียน แต่ไม่ยอมรับว่าเป็นคำตอบในขณะนี้ ฉันจะตรวจสอบและอัปเดต
Kingshuk Deb

โปรดไปที่บรรทัด 1608 ในไฟล์ (Magento 2.1.5) มีฟังก์ชั่นอื่นpublic function saveAttribute(\Magento\Framework\DataObject $object, $attributeCode)
Jaimin Sutariya

1
ดูเหมือนว่าวีโอไอพีนั้นได้ทำหน้าที่สำคัญทั้งหมดไว้เหมือนเดิม
Kingshuk Deb

4

เพียงชี้แจงคำตอบของ Jaimin:

สิ่งนี้จะใช้ได้กับเอนทิตีใด ๆ

นี่ไม่เป็นความจริง. มันจะทำงานสำหรับเอนทิตี EAV ที่ขยายออกไปเท่านั้นMagento\Eav\Model\Entity\AbstractEntity

หากคุณกำลังจัดการกับเอนทิตีที่ไม่ใช่ EAV ซึ่งขยายโมเดลรีซอร์สMagento\Framework\Model\ResourceModel\Db\AbstractDbคุณจะต้องใช้saveAttributeเมธอดในโมเดลรีซอร์สของคุณ

ใน Magento 2 พวกเขาได้ทำเพื่อMagento\Sales\Model\ResourceModel\Attributeชั้นเรียน:

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;
}

3

ในกรณีของผลิตภัณฑ์คุณสามารถใช้ออบเจคการกระทำแบบรวม ตัวอย่างเช่น:

// Edit
$productIds = [123];
$attributesData = ['name' => 'new product name'];
$storeId = 0;
$productMassAction = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Catalog\Model\Product\Action');
$productMassAction->updateAttributes($productIds, $attributesData, $storeId);
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.