2016-11-02 53 views
-1

任何人都可以解釋我從數據庫填充選擇字段的最簡單方法嗎?ZF3無法從數據庫填充選擇字段

ChartsForm.php

<pre> 
<?php 
namespace Charts\Form; 

use Zend\Form\Form; 

use Charts\Model\PostRepositoryInterface; 



class ChartsForm extends Form 
{ 

    private $postRepository; 

    public function __construct(PostRepositoryInterface $postRepository) 
    { 
     $this->postRepository = $postRepository; 

     // We will ignore the name provided to the constructor 
     parent::__construct('album'); 

     $this->add([ 
      'name' => 'id', 
      'type' => 'hidden', 
     ]); 
     $this->add([ 
      'name' => 'title', 
      'type' => 'text', 
      'options' => [ 
       'label' => 'Title', 
      ], 
     ]); 
     $this->add(array(
      'type' => 'Radio', 
      'name' => 'select', 
      'options' => array(
       'label' => 'select type', 
       'value_options' => array(
        '1' => 'Index', 
        '2' => 'Security', 
       ), 
      ), 
      'attributes' => array(
       'value' => '1' 
      ) 
     )); 
       $this->add(array(
      'type' => 'Zend\Form\Element\Select', 
      'name' => 'gender', 
      'options' => array(
       'value_options' => $this->postRepository->findAllPosts(), 
      ), 
      'attributes' => array(
       'value' => '1' 
      ) 
     )); 
     $this->add([ 
      'name' => 'submit', 
      'type' => 'submit', 
      'attributes' => [ 
       'value' => 'Go', 
       'id' => 'submitbutton', 
      ], 
     ]); 
    } 



} 
</pre> 

module.config.php 
<pre> 
<?php 

namespace Charts; 

use Zend\ServiceManager\Factory\InvokableFactory; 
use Zend\Db\Adapter\AdapterAbstractServiceFactory; 


return [ 
    'controllers' => [ 
     'factories' => [ 
      Model\ChartSqlRepository::class => Factory\ChartSqlRepositoryFactory::class, 
     ], 
    ], 

    'service_manager' => [ 
     'aliases' => [ 
      Model\PostRepositoryInterface::class => Model\PostRepository::class, 
     ], 
     'factories' => [ 
      Model\PostRepository::class => InvokableFactory::class, 
     ], 
    ], 

    'view_manager' => [ 
     'template_path_stack' => [ 
      'charts' => __DIR__ . '/../view', 
     ], 
    ], 

     'router' => [ 
     'routes' => [ 
      'charts' => [ 
       'type' => 'segment', 
       'options' => [ 
        'route' => '/charts[/:action[/:id]]', 
        'constraints' => [ 
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'id'  => '[0-9]+', 
        ], 
        'defaults' => [ 
         'controller' => Controller\ChartsController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
     ], 
    ], 




     // 'service_manager' => [ 
    // 'factories' => [ 
     // 'Zend\Db\Adapter\Adapter' => AdapterAbstractServiceFactory::class, 
    // ], 
// ], 
]; 

</pre> 

module.php 
<pre> 
<?php 

namespace Charts; 

use Zend\ModuleManager\Feature\ConfigProviderInterface; 

use Zend\Db\Adapter\AdapterInterface; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 



class Module implements ConfigProviderInterface 
{ 
    public function getConfig() 
    { 
     return include __DIR__ . '/../config/module.config.php'; 
    } 


    public function getServiceConfig() 
    { 
     return [ 
      'factories' => [ 
       Model\BKPagesTable::class => function($container) { 
        $tableGateway = $container->get(Model\BKPagesTableGateway::class); 
        return new Model\BKPagesTable($tableGateway); 
       }, 
       Model\BKPagesTableGateway::class => function ($container) { 
        $dbAdapter = $container->get(AdapterInterface::class); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Model\BKPages()); 
        return new TableGateway('bkpages', $dbAdapter, null, $resultSetPrototype); 
       }, 
       Model\BuyOrdersTable::class => function($container) { 
        $tableGateway = $container->get(Model\BuyOrdersTableGateway::class); 
        return new Model\BuyOrdersTable($tableGateway); 
       },       
       Model\BuyOrdersTableGateway::class => function($container) { 
        $dbAdapter2 = $container->get('api2web'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Model\BuyOrders()); 
        return new TableGateway('BuyOrders', $dbAdapter2, null, $resultSetPrototype); 
       }, 

       //Stocklist Table 
       Model\StockListTable::class => function($container) { 
        $tableGateway = $container->get(Model\StockListTableGateway::class); 
        return new Model\StockListTable($tableGateway); 
       },       
       Model\StockListTableGateway::class => function($container) { 
        $dbAdapter2 = $container->get('api2web'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Model\StockList()); 
        return new TableGateway('StockList', $dbAdapter2, null, $resultSetPrototype); 
       }, 

       //Fulldayquot Table 
       Model\FulldayquotTable::class => function($container) { 
        $tableGateway = $container->get(Model\FulldayquotTableGateway::class); 
        return new Model\FulldayquotTable($tableGateway); 
       },       
       Model\FulldayquotTableGateway::class => function($container) { 
        $dbAdapter2 = $container->get('api2web'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Model\Fulldayquot()); 
        return new TableGateway('fulldayquot', $dbAdapter2, null, $resultSetPrototype); 
       }, 


      ], 
     ]; 
    } 

     public function getControllerConfig() 
    { 
     return [ 
      'factories' => [ 
       Controller\ChartsController::class => function($container) { 
        return new Controller\ChartsController(
         $container->get(Model\BKPagesTable::class) 
        ); 
       }, 
      ], 
     ]; 
    } 
} 

</pre> 

ChartSqlRepositoryFactory.php 

<pre> 
<?php 

namespace Charts\Factory; 


use Charts\Model\ChartSqlRepository; 
use Zend\Db\Adapter\AdapterInterface; 

use Blog\Model\PostRepositoryInterface; 
use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\Factory\FactoryInterface; 

class ChartSqlRepositoryFactory implements FactoryInterface 
{ 

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 

     return new ChartSqlRepository(
     $container->get($container->get(PostRepositoryInterface::class) 
     ); 
    } 
} 
</pre> 

ChartSqlRepository.php 

<pre> 
<?php 

namespace Charts\Model; 

use InvalidArgumentException; 
use RuntimeException; 
use Zend\Db\Adapter\AdapterInterface; 

use Zend\Db\Adapter\Driver\ResultInterface; 
use Zend\Db\ResultSet\ResultSet; 

class ChartSqlRepository implements PostRepositoryInterface 
{ 

    private $db; 
private $repository; 

    public function __construct(ChartSqlRepository $repository) 
    { 

     $this->repository = $repository; 
    } 

    public function findAllPosts() 
    { 
     $sql = new Sql($this->db); 
     $select = $sql->select('BKPages'); 
     $stmt = $sql->prepareStatementForSqlObject($select); 
     $result = $stmt->execute(); 
     var_export($result); 
    die(); 
    } 

     public function IndexesOptions() 
    { 
     $sql = new Sql($this->db); 
     $select = $sql->select('BKPages'); 
     $stmt = $sql->prepareStatementForSqlObject($select); 
     $result = $stmt->execute(); 
     $selectData = array(); 

     foreach ($result as $res) { 
      $selectData[$res['MenuID']] = $res['MenuID']; 
     } 
     return $selectData; 
    } 


    public function findPost($id) 
    { 
    } 
} 
</pre> 

我試圖從數據庫填充在ChartsForm.php選擇字段並建立了一個工廠,也許有些錯誤module.config.php它說。

Catchable fatal error: Argument 1 passed to Charts\Form\ChartsForm::__construct() must be an instance of Charts\Model\PostRepositoryInterface, none given, called in C:\wamp\www\bw\module\Charts\src\Controller\ChartsController.php on line 60 and defined in C:\wamp\www\bw\module\Charts\src\Form\ChartsForm.php on line 24 

請幫我解決。

回答

0

你能更具體嗎?什麼類型的數據庫?
您有兩種選擇:PDO(適用於所有驅動程序)和MySQLy(適用於MySQL)。

here了較大的反響: https://stackoverflow.com/a/8255054/6784143

UPDATE:
您可以用下面這行代碼(上運行.PHP好)連接您的SQL Server。

resource mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]]) 

您可以使用此行代碼執行查詢。

mixed sqlsrv_query (resource $conn , string $sql [, array $params [, array $options ]]) 
+0

我正在使用SqlSrv連接。沒關係,只是試圖在form.php中獲取數據庫連接並針對它運行查詢。 – ajazsiddiqui

+0

@ajazsiddiqui告訴我,如果一切都沒問題。 –

+0

上傳了一些文件,希望你能夠提示我正在嘗試做什麼。 – ajazsiddiqui

相關問題