1

I am trying to update the record but it creates a new record.

Collection factory

     <?php


namespace Webkul\Mpquotesystem\Block;

use Webkul\Mpquotesystem\Model\ResourceModel\Quoteconversation;
use Magento\Framework\Pricing\Helper\Data;
use Webkul\Mpquotesystem\Model\QuotesFactory;
use Webkul\Mpquotesystem\Model\SellerQuotesFactory;
use Webkul\Mpquotesystem\Model\QuoteconversationFactory;
use Magento\Catalog\Model\ProductFactory;
use Webkul\Mpquotesystem\Model\ResourceModel\SellerQuotes;


class Mpeditquotes extends \Magento\Framework\View\Element\Template
{
    /**
     * @var customerSession
     */
    protected $_customerSession;
    /**
     * @var \Magento\Customer\Model\Customer
     */
    protected $_customerModel;
    /**
     * @var quoteCollection
     */
    protected $_quoteConversationCollection;
    /**
     * @var pricingHelper
     */
    protected $_pricingHelper;
    /**
     * @var _quotesFactory
     */
    protected $_quotesFactory;
    /**
     * @var _sellerQuoteFactory
     */
    protected $_sellerQuoteFactory;
    /**
     * @var _productFactory
     */
    protected $_productFactory;
    /**
     * @var \Magento\Catalog\Helper\Image
     */
    protected $_imageHelper;
    /**
     * @var _quoteConversationCollectionFactory
     */
    protected $_quoteConversationCollectionFactory;
    /**
     * @var _quoteconversationFactory
     */
    protected $_quoteconversationFactory;
    protected $_sellercolletcionquotes;

    /**
     * @param \Magento\Customer\Model\Session        $customerSession
     * @param \Magento\Customer\Model\Customer       $customerModel
     * @param \Magento\Catalog\Block\Product\Context $context
     * @param Quoteconversation\CollectionFactory    $conversationCollectionFactory
     * @param QuotesFactory                          $_quotesFactory
     * @param ProductFactory                         $productFactory
     * @param QuoteconversationFactory               $conversationFactory
     * @param Data                                   $pricingHelper
     * @param array                                  $data
     */

    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Customer $customerModel,
        \Magento\Catalog\Block\Product\Context $context,
        Quoteconversation\CollectionFactory $conversationCollectionFactory,
        QuotesFactory $_quotesFactory,
        SellerQuotesFactory $sellerQuoteFactory,
        ProductFactory $productFactory,
        QuoteconversationFactory $conversationFactory,
        \Webkul\Mpquotesystem\Model\ResourceModel\SellerQuotes\CollectionFactory $sellercolletcionquotes,
        Data $pricingHelper,
        array $data = []
    ) {
        $this->_customerSession = $customerSession;
        $this->_customerModel = $customerModel;
        $this->_quoteConversationCollectionFactory = $conversationCollectionFactory;
        $this->_pricingHelper = $pricingHelper;
        $this->_quotesFactory = $_quotesFactory;
        $this->_productFactory = $productFactory;
        $this->_sellerQuoteFactory = $sellerQuoteFactory;
        $this->_quoteconversationFactory = $conversationFactory;
        $this->_imageHelper = $context->getImageHelper();
        $this->_sellercolletcionquotes = $sellercolletcionquotes;
        parent::__construct($context, $data);
    }
    /**
     * @return $this
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if ($this->getQuoteConversationCollection()) {
            $pager = $this->getLayout()->createBlock(
                'Magento\Theme\Block\Html\Pager',
                'mpquotesystem.pager'
            )
            ->setCollection(
                $this->getQuoteConversationCollection()
            );
            $this->setChild('pager', $pager);
            $this->getQuoteConversationCollection()->load();
        }

        return $this;
    }
    /**
     * @return string
     */
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }
    /**
     * customer Id by customer session.
     *
     * @return int
     */
    public function getCustomerId()
    {
        return $this->_customerSession->getCustomerId();
    }
    /**
     * customer data by customer id.
     *
     * @return object
     */
    public function getCustomerData($customerId)
    {
        return $this->_customerModel->load($customerId);
    }
    /**
     * get Collection of quotes conversation for particular quote id.
     *
     * @return collection
     */
    public function getQuoteConversationCollection()
    {
        if (!$this->_quoteConversationCollection) {
            $sellerquoteId = $this->getRequest()->getParam('id');
             $sellerquote = $this->getSellerQuotes($sellerquoteId);
             $quoteId = $sellerquote->getQuoteId();
             $sellerId = $sellerquote->getSellerId();
            if ($quoteId != 0) {
                $collection = $this->_quoteConversationCollectionFactory
                    ->create()
                    ->addFieldToFilter('quote_id', $quoteId)
                    ->addFieldToFilter('sender', $sellerId);

                $this->_quoteConversationCollection = $collection;
            }
        }

        return $this->_quoteConversationCollection;
    }
    /**
     * get formatted price by currency.
     *
     * @return format price string
     */
    public function getFormattedPrice($price)
    {
        return $this->_pricingHelper
            ->currency($price, true, false);
    }

    public function getQuoteData($entityId)
    {
        $quoteModel = $this->_quotesFactory->create()->load($entityId);
        return $quoteModel;
    }
    /**
     * get Collection of quotes conversation for particular quote id.
     *
     * @return collection
     */
    public function getsellerQuoteConversationCollection($quoteId)
    {

        if (!$this->_quoteConversationCollection) {
            //~ $quoteId = $this->getRequest()->getParam('id');
            if ($quoteId != 0) {
                $collection = $this->_quoteConversationCollectionFactory
                    ->create()
                    ->addFieldToFilter('quote_id', $quoteId);

                $this->_quoteConversationCollection = $collection;
            }
        }

        return $this->_quoteConversationCollection;
    }


  /**  public function **getsellerupdateCollection**($quoteId,$sellerid)
    {


        //~ $quoteId = $this->getRequest()->getParam('id');
        if ($quoteId != 0) {
            $collection = $this->_sellercolletcionquotes
                ->create()

                ->load('quote_id',$quoteId)
                ->load('main_table.status',array('in'=>array('1','2')))
                ->load('main_table.seller_id',['neq'=>$sellerid])
                ->load('main_table.nstatus',1);

          foreach($collection as $item) {

          }
            $this->_sellercolletcionquotes = $collection;

        }


        return $this->_sellercolletcionquotes;
    }*/

   public function getsellerupdateCollection($quoteId,$sellerid)
   {


       //~ $quoteId = $this->getRequest()->getParam('id');
       if ($quoteId != 0) {
           $collection = $this->_sellercolletcionquotes
               ->create()
               ->addFieldToSelect('quote_id', $quoteId)
               ->addFieldToFilter('main_table.status', array('in' => array('1', '2')))
               ->addFieldToFilter('main_table.seller_id', ['neq' => $sellerid])
               ->addFieldToFilter('main_table.nstatus', 1);



           foreach($collection as $item)
           {
                $item->setNstatus('Sold');
           $item->save();

           }

       $this->_sellercolletcionquotes = $collection;
       }
    return $this->_sellercolletcionquotes;
    }


    public function getSellerQuotes($entityId){
        $quoteModel = $this->_sellerQuoteFactory->create()->load($entityId);
        return $quoteModel;
    }
    public function getProductData($productId)
    {
        $productModel = $this->_productFactory->create()->load($productId);
        return $productModel;
    }
    public function imageHelperObj()
    {
        return $this->_imageHelper;
    }
    public function getProductPriceHtml(
        \Magento\Catalog\Model\Product $product,
        $priceType = \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
        $renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
        array $arguments = []
    ) {
        if (!isset($arguments['zone'])) {
            $arguments['zone'] = $renderZone;
        }

        /** @var \Magento\Framework\Pricing\Render $priceRender */
        $priceRender = $this->getLayout()->getBlock('product.price.render.default');
        $price = '';

        if ($priceRender) {
            $price = $priceRender->render($priceType, $product, $arguments);
        }
        return $price;
    }

    public function getParameters()
    {
        return $this->getRequest()->getParams();
    }


    /**
     * getIsSecure check is secure or not
     * @return boolean
     */
    public function getIsSecure()
    {
        return $this->getRequest()->isSecure();
    }
    /**
     * check whether a quote is sold or not?
     * @param  int $quoteStatus
     * @return boolean
     */
    public function quoteStatusIsNotSold($quoteStatus)
    {
        if ($quoteStatus!=\Webkul\Mpquotesystem\Model\Quotes::STATUS_SOLD) {
            return true;
        }
        return false;
    }
}

I want multiple where a condition for update record in Magento 2

0

Try this one, adding getCollection():

public function getsellerupdateCollection($quoteId,$sellerid)
{
    if ($quoteId != 0) {
        $collection = $this->_sellercolletcionquotes
            ->create()->getCollection()
            ->addFieldToSelect('quote_id',$quoteId)
            ->addFieldToFilter('main_table.status',array('in'=>array('1','2')))
            ->addFieldToFilter('main_table.seller_id',['neq'=>$sellerid])
            ->addFieldToFilter('main_table.nstatus',1);

        foreach($collections as $item)
        {
           $item->setNstatus('Sold');
           $item->save();
        }
        $this->_sellercolletcionquotes = $collection;
    }
    return $this->_sellercolletcionquotes;
}
  • where i can use save(); - method – prabhakaran7 Apr 8 at 12:05
  • updated the answer – magefms Apr 8 at 12:06
  • Uncaught Error: Call to undefined method in Resource model Collection::getCollection() in /var/www/html/* – prabhakaran7 Apr 8 at 12:07
  • i already defined in constract \Webkul\Mpquotesystem\Model\ResourceModel\SellerQuotes\CollectionFactory $sellercolletcionquotes, – prabhakaran7 Apr 8 at 12:08
  • 1
    i make mistake ** ->addFieldToSelect('quote_id',$quoteId)** addFieldsToFIlter instead select – prabhakaran7 Apr 10 at 9:52
0

I have updated a few lines of code.

public function getsellerupdateCollection($quoteId,$sellerid)
{
    if ($quoteId != 0) {
        $collection = $this->_sellercolletcionquotes
            ->create()->getCollection()
            ->addFieldToSelect('quote_id',$quoteId)
            ->addFieldToFilter('main_table.status',array('in'=>array('1','2')))
            ->addFieldToFilter('main_table.seller_id',['neq'=>$sellerid])
            ->addFieldToFilter('main_table.nstatus',1);

        foreach($collections as $item)
        {
           $item->setNstatus('Sold');
           $item->save();
        }
        $this->_sellercolletcionquotes = $collection;
    }
    return $this->_sellercolletcionquotes;
}
  • i will check ..................... – prabhakaran7 Apr 8 at 11:46
  • Why you are adding condition like this ->addFieldToFilter('main_table.seller_id',['neq'=>$sellerid]) instead of ->addFieldToFilter('main_table.seller_id',['eq'=>$sellerid]) – Kamlesh Solanki Apr 8 at 11:48
  • i am omitting that customer for update... – prabhakaran7 Apr 8 at 11:50
  • ok is that worked? – Kamlesh Solanki Apr 8 at 11:54
  • Actaully, what you want update it – Kamlesh Solanki Apr 8 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.