title: how to update only some field of an entity in Zend Framework 2
body: <p>my product entity has 11 fields and i want to update only 4 field to update on updateAction of my productController. In my update view I only display that 4 fields that i want to updade . Every thing is work fine except that after update it update remaining fields of the product table to null.
  hare is my updateAction</p>
  
  <pre><code>public function updateAction()
  {
       $pid = (int) $this-&gt;params()-&gt;fromRoute('pid', 0);
      if (!$pid) {
          return $this-&gt;redirect()-&gt;toRoute('product', array(
              'action' =&gt; 'add'
          ));
      }
      $product = $this-&gt;getProductTable()-&gt;getProduct($pid);
  
      $form  = new ProductForm();
      $form-&gt;setValidationGroup('product_name', 'bv','price');
      $form-&gt;bind($product);
      $form-&gt;get('submit')-&gt;setAttribute('value', 'Edit');
  
      $request = $this-&gt;getRequest();
      if ($request-&gt;isPost()) {
          $form-&gt;setInputFilter($product-&gt;getInputFilter());
          $form-&gt;setData($request-&gt;getPost());
  
          if ($form-&gt;isValid()) {
              $this-&gt;getProductTable()-&gt;saveProduct($form-&gt;getData());
               $this-&gt;flashMessenger()-&gt;addSuccessMessage("Product ".$product-&gt;getProduct_name()." updated Succesfully");
              // Redirect to list of products
              return $this-&gt;redirect()-&gt;toRoute('product');
          }
      }
  
      return array(
          'pid' =&gt; $pid,
          'form' =&gt; $form,
      );
  
  }
  </code></pre>
  
  <p>my product entity</p>
  
  <pre><code>namespace Admin\Model;
  use Zend\InputFilter\Factory as InputFactory;
  use Zend\InputFilter\InputFilter;
  use Zend\InputFilter\InputFilterAwareInterface;
  use Zend\InputFilter\InputFilterInterface;
  
  
  class Product implements InputFilterAwareInterface
  {
  
  protected $pid;
  protected $code;
  protected $category;
  protected $product_name;
  protected $product_desc;
  protected $bv;
  protected $price;
  protected $stock;
  protected $sale;
  protected $productimage;
  protected $thumbnail;
  protected $inputFilter;
  
  public function setPid($pid)
  {
      $this-&gt;pid=$pid;
  }
  public function getPid()
  {
      return $this-&gt;pid;
  }
  public function setCode($code)
  {
      $this-&gt;code=$code;
  }
  public function getCode()
  {
      return $this-&gt;code;
  }
  public function setCategory($catgory)
  {
      $this-&gt;category=$catgory;
  }
  public function getCategory()
  {
      return $this-&gt;category;
  }
  public function setProduct_desc($desc)
  {
      $this-&gt;product_desc=$desc;
  }
  public function getProduct_desc()
  {
      return $this-&gt;product_desc;
  }
  public function setProduct_name($name)
  {
      $this-&gt;product_name=$name;
  }
  public function getProduct_name()
  {
      return $this-&gt;product_name;
  }
  public function setBv($bv)
  {
      $this-&gt;bv=$bv;
  }
  public function getBv()
  {
      return $this-&gt;bv;
  }
  public function setPrice($price)
  {
      $this-&gt;price=$price;
  }
  public function getPrice()
  {
      return $this-&gt;price;
  }
  public function setProduct_img($image)
  {
      $this-&gt;productimage=$image;
  }
  public function getProduct_img()
  {
      return $this-&gt;productimage;
  }
  public function setThumbnail($thumb)
  {
      $this-&gt;thumbnail=$thumb;
  }
  public function getThumbnail()
  {
      return $this-&gt;thumbnail;
  }
  public function setStock($stock) {
  
      $this-&gt;stock=$stock;
  }
  public function getStock() {
  
      return $this-&gt;stock;
  }
  public function setSale($sale) {
  
      $this-&gt;sale=$sale;                
  }
  public function getSale() {
  
      return $this-&gt;sale;
  }
  public function exchangeArray($data)
  {
      $this-&gt;pid     = (isset($data['pid'])) ? $data['pid'] : null;
      $this-&gt;code = (isset($data['code'])) ? $data['code'] : null;
      $this-&gt;product_desc  = (isset($data['product_desc'])) ? $data['product_desc'] : null;
      $this-&gt;product_name=(isset($data['product_name'])) ? $data['product_name'] : null;
      $this-&gt;bv=(isset($data['bv'])) ? $data['bv'] : null;
      $this-&gt;price=(isset($data['price'])) ? $data['price'] : null;
      $this-&gt;stock=(isset($data['stock'])) ? $data['stock'] : null;
      $this-&gt;sale=(isset($data['sale'])) ? $data['sale'] : null;
      $this-&gt;productimage=(isset($data['productimage'])) ? $data['productimage'] : null;
      $this-&gt;thumbnail=(isset($data['thumbnail'])) ? $data['thumbnail'] : null;
  
  }
  public function getArrayCopy()
  {
      return get_object_vars($this);
  }
   public function setInputFilter(InputFilterInterface $inputFilter)
  {
      throw new \Exception("Not used");
  }
  public function getInputFilter()
  {
  
      if (!$this-&gt;inputFilter) {
          $inputFilter = new InputFilter();
          $factory     = new InputFactory();
  
          $inputFilter-&gt;add($factory-&gt;createInput(array(
              'name'     =&gt; 'pid',
              'required' =&gt; true,
              'filters'  =&gt; array(
                  array('name' =&gt; 'Int'),
              ),
          )));
  
          $inputFilter-&gt;add($factory-&gt;createInput(array(
              'name'     =&gt; 'code',
              'required' =&gt; true,
              'filters'  =&gt; array(
                  array('name' =&gt; 'StripTags'),
                  array('name' =&gt; 'StringTrim'),
              ),
              'validators' =&gt; array(
                  array(
                      'name'    =&gt; 'StringLength',
                      'options' =&gt; array(
                          'encoding' =&gt; 'UTF-8',
                          'min'      =&gt; 5,
                          'max'      =&gt; 100,
                      ),
                  ),
              ),
          )));
  
          $inputFilter-&gt;add($factory-&gt;createInput(array(
              'name'     =&gt; 'product_name',
              'required' =&gt; true,
              'filters'  =&gt; array(
                  array('name' =&gt; 'StripTags'),
                  array('name' =&gt; 'StringTrim'),
              ),
              'validators' =&gt; array(
                  array(
                      'name'    =&gt; 'StringLength',
                      'options' =&gt; array(
                          'encoding' =&gt; 'UTF-8',
                          'min'      =&gt; 1,
                          'max'      =&gt; 100,
                      ),
                  ),
              ),
          )));
          $inputFilter-&gt;add($factory-&gt;createInput(array(
              'name'     =&gt; 'bv',
              'required' =&gt; true,
              'validators'  =&gt; array(
                  array('name' =&gt; 'digits'),
               ),
  
          )));
         $inputFilter-&gt;add($factory-&gt;createInput(array(
              'name'     =&gt; 'price',
              'required' =&gt; true,
              'validators'  =&gt; array(
                  array(
                        'name' =&gt; 'digits',
  
  
                  ),
               ),
  
          )));
         $inputFilter-&gt;add($factory-&gt;createInput(array(
              'name'     =&gt; 'stock',
              'required' =&gt; true,
              'validators'  =&gt; array(
                  array(
                        'name' =&gt; 'digits',
  
  
                  ),
               ),
  
          )));
  
          $this-&gt;inputFilter = $inputFilter;
      }
  
      return $this-&gt;inputFilter;
  }
   }
  </code></pre>
  
  <p>saveproduct method is here</p>
  
  <pre><code> public function saveProduct(Product $product)
  {
      $data =$product-&gt;getArrayCopy();
  
  
  
      $pid = (int)$product-&gt;getPid();
      if ($pid == 0) {
          $this-&gt;tableGateway-&gt;insert($data);
      } else {
          if ($this-&gt;getProduct($pid)) {
              $this-&gt;tableGateway-&gt;update($data, array('pid' =&gt; $pid));
          } else {
              throw new \Exception('Product id does not exist');
          }
      }
  
  }
  </code></pre>
  
  <p>Thanks in advance for your valuable suggestions. </p>