我的zend框架2项目涉及在线餐厅菜单。我有一个表单来添加一个新的披萨,当我尝试在addPizzaForm.php和add.phtml视图的帮助下添加一个新的披萨时,没有数据添加到数据库中的表中。没有显示错误,但也没有添加数据。
以下是我的文件,请帮我查一下我的代码有什么问题。
PizzaTable.php:
namespace Pizza\Model;
use Zend\Db\TableGateway\TableGateway;
class PizzaTable{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway=$tableGateway;
}
public function find_all()
{
//select • FRom Pizza
return $this->tableGateway->select();
}
public function find_by_id($id=0)
{
$id = (int)$id;
$result=$this->tableGateway->select(array('id'=>$id));
$row=$this->tableGateway->current();
if ($row)
{
return $row;
}
else
{
return null;
}
}
public function save(Pizza $pizza)
{
$data = array ('name' => $pizza->name,
'ingredients' => $pizza->ingredients,
'smallprice' => $pizza->smallprice,
'bigprice' => $pizza->bigprice,
'familyprice' => $pizza->familyprice,
'partyprice' => $pizza->partyprice,
);
$id = (int) $pizza->id;
if($id == 0)
{
// insert a new record
$this->tableGateway->insert($data);
}
else
{
// update a new record
if($this->find_by_id($id))
{
$this->tableGateway->update($data , array('id'=>$id));
}
else
{
throw new \Exception("the pizza with the id = {$id} could not be found in database");
}
}
}
public function delete($id=0)
{
$this->tableGateway->delete(array('id' => (int)$id));
}
}
add.phtml:
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('pizza',array('action'=>'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('pizza_name')) . "</br>";
echo $this->formRow($form->get('ingredients')) . "</br>";
echo $this->formRow($form->get('small_price')) . "</br>";
echo $this->formRow($form->get('big_price')) . "</br>";
echo $this->formRow($form->get('family_price')) . "</br>";
echo $this->formRow($form->get('party_price')) . "</br>";
echo $this->formRow($form->get('add_pizza')) . "</br>";
echo $this->form()->closeTag();
?>
PizzaController.php:
public function addAction()
{
$add_form = new AddPizzaForm();
$request = $this->getRequest();
if($request->isPost())
{
$pizza = new Pizza();
$add_form->setInputFilter($pizza->getInputFilter());
$add_form->setData($request->getPost());
if($add_form->isValid())
{
$pizza->exchangeArray($add_form->getData());
$this->getPizzaTable()->save($pizza);
}
return $this->redirect()->toRoute('pizza');
}
return array('form' => $add_form);
}