Tüm ürünü Magento2'ye kaydetmek yerine yalnızca belirli özellik değeri nasıl kaydedilir?


10

Bildiğiniz gibi , bu yöntemle belirli bir özellik değerini kaydetmek için magento'da aşağıdaki yöntemi kullandığımızı zaten biliyorsunuz .

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

veya

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

Magento2'de yukarıdakilerin alternatifini kimse bana bildirebilir mi ?

Yanıtlar:


8

Magento ile aynı 1

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

Bu herhangi bir varlık için işe yarar.

Raphael'in @ uyarınca Yanıt Bu satış özellikleri için çalışmaz.

Temel olarak Magento \ Eav \ Model \ Entity \ AbstractEntity :: saveAttribute () işlevini çağırır .

Bu iki parametreyi kabul eder

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

Birincisi, $objectgüncellenmesi gereken bir nesne ve ikinci parametre $attributeCode, niteliğin güncellenmesi için kod olacak.


Temelde herhangi bir varlık için olmalıdır.
— Kingshuk Deb

Evet, herhangi bir varlık için çalışacaktır. Temel olarak Magento\Eav\Model\Entity\AbstractEntity::saveAttribute()bir dataobject ve varlık kodunu kabul edecek şekilde çağrılacaktır.
— Jaimin Sutariya

Güncelleme ancak şu anda bir cevap olarak kabul etmeme. Kontrol edip güncelleyeceğim.
— Kingshuk Deb

Lütfen dosyadaki 1608 satırına gidin. (Magento 2.1.5) Başka bir işlev daha varpublic function saveAttribute(\Magento\Framework\DataObject $object, $attributeCode)
— Jaimin Sutariya

1
Magento temelde tüm önemli fonksiyonları sağlam tutuyor.
— Kingshuk Deb

4

Sadece Jaimin'in cevabını açıklığa kavuşturmak için:

Bu herhangi bir varlık için işe yarar.

Bu doğru değil. Yalnızca genişleyen EAV varlıkları için çalışırMagento\Eav\Model\Entity\AbstractEntity

Kaynak modelinin uzandığı EAV olmayan bir varlıkla uğraşıyorsanız yöntemi kaynak modelinize Magento\Framework\Model\ResourceModel\Db\AbstractDbuygulamanız saveAttributegerekir.

Magento 2'de bunu Magento\Sales\Model\ResourceModel\Attributesınıf için yaptılar :

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

Ürün söz konusu olduğunda, toplu eylem nesnesini kullanabilirsiniz. Örneğin:

// 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);
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.