我的產品實體有11個字段,我想只更新4個字段來更新我的productController的updateAction。在我的更新視圖中,我只顯示要更新的4個字段。每一件事情都很好,除了更新後,它將產品表的剩餘字段更新爲空。 野兔是我updateAction如何只更新Zend Framework 2中某個實體的字段
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');
}
}
}
感謝您的寶貴建議。
所以'的productTable :: saveProduct()會發生什麼;'? – AlexP 2014-09-01 09:38:47