如何使用MYSQL ZEND1.12从表单存储和显示图像

问题描述 投票:0回答:1

我正在使用ZEND1.12,我想将图像从表单存储到数据库,然后将其显示在CRUD表上。这是表格的代码:Product.php

<?php

类Application_Form_Product扩展Zend_Form{

受保护的$ 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

Class 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;
}

}这是View:

</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

类Application_Model_DbTable_Product扩展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";
}   
 }

}

php mysql database zend-framework crud
1个回答
0
投票

使用getFiles和file_get_contents获取持久性Blob列的图像文件:

$rowData = [
       'name_product' => $request->getPost('name'),
       'category_id' => $request->getPost('category'),
       'warehouse_id' => $request->getPost('warehouse'),
       'picture' => file_get_contents($request->getFiles('picture')->toArray()['tmp_name']),
       'price' => $request->getPost('price')
];

getFiles参考:https://docs.zendframework.com/zend-http/request/

直接从持久性blob显示图像文件。

jpg图片:

echo '<td><img src="data:image/jpeg;base64,'.base64_encode( $product->picture ).'"/></td>';

png图片:

echo '<td><img src="data:image/png;base64,'.base64_encode( $product->picture ).'"/></td>';

--- == ^^^ == ---

© www.soinside.com 2019 - 2024. All rights reserved.