如何只更新Zend Framework 2中某个实体的某个字段

时间:2014-09-01 04:38:12

标签: zend-framework zend-framework2 zend-form

我的产品实体有11个字段,我想只更新4个字段来更新我的productController的updateAction。在我的更新视图中,我只显示我想要更新的4个字段。除了在更新之后它将产品表的剩余字段更新为null之外,每件事都可以正常工作。 野兔是我的updateAction

public function updateAction()
{
     $pid = (int) $this->params()->fromRoute('pid', 0);
    if (!$pid) {
        return $this->redirect()->toRoute('product', array(
            'action' => 'add'
        ));
    }
    $product = $this->getProductTable()->getProduct($pid);

    $form  = new ProductForm();
    $form->setValidationGroup('product_name', 'bv','price');
    $form->bind($product);
    $form->get('submit')->setAttribute('value', 'Edit');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $form->setInputFilter($product->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $this->getProductTable()->saveProduct($form->getData());
             $this->flashMessenger()->addSuccessMessage("Product ".$product->getProduct_name()." updated Succesfully");
            // Redirect to list of products
            return $this->redirect()->toRoute('product');
        }
    }

    return array(
        'pid' => $pid,
        'form' => $form,
    );

}

我的产品实体

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->pid=$pid;
}
public function getPid()
{
    return $this->pid;
}
public function setCode($code)
{
    $this->code=$code;
}
public function getCode()
{
    return $this->code;
}
public function setCategory($catgory)
{
    $this->category=$catgory;
}
public function getCategory()
{
    return $this->category;
}
public function setProduct_desc($desc)
{
    $this->product_desc=$desc;
}
public function getProduct_desc()
{
    return $this->product_desc;
}
public function setProduct_name($name)
{
    $this->product_name=$name;
}
public function getProduct_name()
{
    return $this->product_name;
}
public function setBv($bv)
{
    $this->bv=$bv;
}
public function getBv()
{
    return $this->bv;
}
public function setPrice($price)
{
    $this->price=$price;
}
public function getPrice()
{
    return $this->price;
}
public function setProduct_img($image)
{
    $this->productimage=$image;
}
public function getProduct_img()
{
    return $this->productimage;
}
public function setThumbnail($thumb)
{
    $this->thumbnail=$thumb;
}
public function getThumbnail()
{
    return $this->thumbnail;
}
public function setStock($stock) {

    $this->stock=$stock;
}
public function getStock() {

    return $this->stock;
}
public function setSale($sale) {

    $this->sale=$sale;                
}
public function getSale() {

    return $this->sale;
}
public function exchangeArray($data)
{
    $this->pid     = (isset($data['pid'])) ? $data['pid'] : null;
    $this->code = (isset($data['code'])) ? $data['code'] : null;
    $this->product_desc  = (isset($data['product_desc'])) ? $data['product_desc'] : null;
    $this->product_name=(isset($data['product_name'])) ? $data['product_name'] : null;
    $this->bv=(isset($data['bv'])) ? $data['bv'] : null;
    $this->price=(isset($data['price'])) ? $data['price'] : null;
    $this->stock=(isset($data['stock'])) ? $data['stock'] : null;
    $this->sale=(isset($data['sale'])) ? $data['sale'] : null;
    $this->productimage=(isset($data['productimage'])) ? $data['productimage'] : null;
    $this->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->inputFilter) {
        $inputFilter = new InputFilter();
        $factory     = new InputFactory();

        $inputFilter->add($factory->createInput(array(
            'name'     => 'pid',
            'required' => true,
            'filters'  => array(
                array('name' => 'Int'),
            ),
        )));

        $inputFilter->add($factory->createInput(array(
            'name'     => 'code',
            'required' => true,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'    => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min'      => 5,
                        'max'      => 100,
                    ),
                ),
            ),
        )));

        $inputFilter->add($factory->createInput(array(
            'name'     => 'product_name',
            'required' => true,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'    => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min'      => 1,
                        'max'      => 100,
                    ),
                ),
            ),
        )));
        $inputFilter->add($factory->createInput(array(
            'name'     => 'bv',
            'required' => true,
            'validators'  => array(
                array('name' => 'digits'),
             ),

        )));
       $inputFilter->add($factory->createInput(array(
            'name'     => 'price',
            'required' => true,
            'validators'  => array(
                array(
                      'name' => 'digits',


                ),
             ),

        )));
       $inputFilter->add($factory->createInput(array(
            'name'     => 'stock',
            'required' => true,
            'validators'  => array(
                array(
                      'name' => 'digits',


                ),
             ),

        )));

        $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
}
 }

saveproduct方法就在这里

 public function saveProduct(Product $product)
{
    $data =$product->getArrayCopy();



    $pid = (int)$product->getPid();
    if ($pid == 0) {
        $this->tableGateway->insert($data);
    } else {
        if ($this->getProduct($pid)) {
            $this->tableGateway->update($data, array('pid' => $pid));
        } else {
            throw new \Exception('Product id does not exist');
        }
    }

}

提前感谢您的宝贵建议。

2 个答案:

答案 0 :(得分:1)

问题在于您的 exchangeArray()方法。我有类似的问题。我通过将模型的exchangeArray()方法改为这样的方法来解决。

<?php

namespace Admin\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class Category  implements InputFilterAwareInterface{

public $category_id=0;
public $name;
public $slug;
public $position=0;
public $news_count=0;
public $status=0;
protected $inputFilter;

public function setInputFilter(InputFilterInterface  $InputFilter){
    throw new Exception("Not used");
}

public function getInputFilter(){
    if(!$this->inputFilter){
        $filter=new InputFilter();
        // now define filters
        $filter->add(array(
            'name'=>'category_id',
            'required'=>'true',
            'filters'=>array(array('name'=>'Int'))
            ));
        $filter->add(array(
            'name'=>'name',
            'required'=>'true',
            'filters'=>array(
                array('name'=>'StripTags'),
                array('name'=>'StringTrim'),
                ),
            'validators'=>array(
                    array(
                        'name'=>'StringLength',
                        'options'=>array(
                            'encoding'=>'UTF-8',
                            'min'=>'3',
                            'max'=>'100',
                            )
                        )
                )
            ));

            $filter->add(array(
                'name'=>'position',
                'filters'=>array(array('name'=>'Int')),
            ));


        $this->inputFilter=$filter;
    }
    return $this->inputFilter;

}

public function exchangeArray($data){
    if(isset($data['category_id'])){
        $this->category_id=$data['category_id'];
    }

    if(isset($data['name'])){
        $this->name=$data['name'];
    }

    if(isset($data['slug'])){
        $this->slug=$data['slug'];
    }
    if(isset($data['status'])){
        $this->status=$data['status'];
    }
    if(isset($data['news_count'])){
        $this->news_count=$data['news_count'];
    }
    if(isset($data['position'])){
        $this->position=$data['position'];
    }

}

public function getArrayCopy()
 {
     return get_object_vars($this);
 }
}

我之前的exchangeArray()方法是

public function exchangeArray($data){
    $this->category_id=(!empty($data['category_id'])) ? $data['category_id']:0;
    $this->name=(!empty($data['name'])) ? $data['name'] : null;
    $this->position=(!empty($data['position'])) ? $data['position']:0;
    $this->status=(!empty($data['status'])) ? $data['status'] : 0;
    $this->slug=(!empty($data['slug'])) ? $data['slug'] : null;
    $this->newsCount=(!empty($data['news_count'])) ? $data['news_count'] : 0;
}

答案 1 :(得分:0)

您已将产品绑定到表单,因此而不是$form->getData()$product传递给save方法:

$this->getProductTable()->saveProduct($product);

由于产品具有剩余的字段值,因此不会将其覆盖为null

如果那不是问题那么你需要提供saveProduct方法体来看看它是什么......