Я использую ZEND1.12 и хочу сохранить изображение из формы в базе данных, а затем отобразить его в таблице CRUD. Вот код формы: Product. php
<?php
Класс Application_Form_Product расширяет Zend_Form {
protected $ db;
public function __construct($db)
{
$this->db = $db;
// Don't forget to call the parent __construct. Ultimately
// it is the parent __construct() that calls your init()
// method that adds your elements
parent::__construct();
}
public function init()
{
$this->addElement('select', 'category', array('label'=>'Category', 'multiOptions' => $this->buildMultiOptions()));
$this->addElement('text', 'name', array('label' => 'Enter The Name:','required' => true));
$this->addElement('select', 'warehouse', array('label'=>'Warehouse', 'multiOptions' => $this->buildMultiOptionsWarehouse()));
$this->addElement('file', 'picture', array('label' => 'Enter The picture:','required' => true));
$this->addElement('text', 'price', array('label' => 'Enter The Price:','required' => true));
// $this->addElement('file','file',array('label' => 'Add file'));
$this->addElement('submit','submit',array('label' => 'Add Product'));
}
} И вот контроллер со всеми действиями:
<?php
Класс ProductController расширяет Zend_Controller_Action {
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$categoryModel = new Application_Model_DbTable_Category();
$warehouseModel = new Application_Model_DbTable_Warehouse();
$productModel = new Application_Model_DbTable_Product();
$products = $productModel->showProducts();
$this->view->products = $products;
}
public function createAction()
{
$db = $this->getInvokeArg('bootstrap')->getResource('db');
$productForm = new Application_Form_Product($db);
$request = $this->getRequest();
if ($request->isPost()) {
if ($productForm->isValid($request->getPost())) {
$productModel = new Application_Model_DbTable_Product();
$productModel->create();
$this->_redirect('product');
}
}
$this->view->productForm= $productForm;
}
public function deleteAction()
{
$productModel = new Application_Model_DbTable_Product();
$productModel->deleteProduct();
$this->_redirect('product');
}
public function editAction()
{
$db = $this->getInvokeArg('bootstrap')->getResource('db');
$productForm = new Application_Form_Product($db);
$request = $this->getRequest();
$id = $request->getParam('id');
$productModel = new Application_Model_DbTable_Product();
$product = $productModel->fetchRow('id = '.$id);
if ($request->isPost()) {
$productModel->edit();
$this->_redirect('product');
}
$this->view->product = $product;
$this->view->productForm = $productForm;
}
} А вот и вид:
</br>
</br>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" class="mx-auto" style="width: 300px;">Name</th>
<th scope="col" class="mx-auto" style="width: 300px;">Category</th>
<th scope="col" class="mx-auto" style="width: 300px;">Warehouse</th>
<th scope="col" class="mx-auto" style="width: 300px;">Picture</th>
<th scope="col" class="mx-auto" style="width: 300px;">Price</th>
<th scope="col" class="mx-auto" style="width: 300px;">Action</th>
</tr>
</thead>
<?php
foreach ($this->products as $product)
{
echo "<tr>";
echo "<td>" . $product->name_product . "</td>";
echo "<td>" . $product->name . "</td>";
echo "<td>" . $product->warehouse_id . "</td>";
echo "<td>" . $product->picture . "</td>";
echo "<td>" . $product->price . "</td>";
echo "<td colspan='2'><a href='" . $this->url(array('controller' => 'product', 'action' => 'edit', 'id' =>
$product->id)) . "' type='button' class='btn btn-primary'>Edit</a>";
echo " <a href='" . $this->url(array('controller' => 'product', 'action' => 'delete', 'id' => $product->id)) . "' onclick='return confirm(\"Do you really want to delete this contact?\");' type='button' class='btn btn-danger'>Delete</a></td>";
echo "</tr>";
}
?>
</table>
А вот и модель : DbTable: Product. php со всеми функциями.
<?php
class Application_Model_DbTable_Product extends Zend_Db_Table_Abstract {
protected $_name = 'products';
public function create() {
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$data = array(
'id' => $request->getPost('id'),
'name_product' => $request->getPost('name'),
'category_id' => $request->getPost('category'),
'warehouse_id' => $request->getPost('warehouse'),
'picture' => $request->getPost('picture'),
'price' => $request->getPost('price')
);
$this->insert($data);
}
public function edit() {
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$data = array(
'name_product' => $request->getPost('name'),
'category_id' => $request->getPost('category'),
'warehouse_id' => $request->getPost('warehouse'),
'picture' => $request->getPost('picture'),
'price' => $request->getPost('price'),
);
$where = array('id = ?' => $request->getParam("id"));
$this->update($data, $where);
}
public function deleteProduct() {
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$where = array('id = ?' => $request->getParam("id"));
$this->delete($where);
}
public function showProducts() {
$select = $this->select()
->from('products')
->setIntegrityCheck(false)
->join('categorys', 'products.category_id =categorys.id', array('*'))
->columns(array('products.id'));
$result = $this->fetchAll($select);
if ($result !== null){
return $result;
} else {
echo "no records found";
}
}
public function showProductsWarehouse() {
$select = $this->select()
->from('products')
->setIntegrityCheck(false)
->join('warehouses', 'products.warehouse_id =warehouses.id', array('*'))
->columns(array('products.id'));
$result = $this->fetchAll($select);
if ($result !== null){
return $result;
} else {
echo "no records found";
}
}
}