-1

谁能解释我从数据库中填充表单中选择字段的最简单方法?

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>

我正在尝试从 db 填充 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

请帮帮我。

4

1 回答 1

0

你可以说得更详细点吗?什么类型的数据库?
您有两个选择:PDO(适用于所有驱动程序)和 MySQLy(适用于 MySQL)。

在此处查找更大的响应: https ://stackoverflow.com/a/8255054/6784143

更新:
您可以使用此行代码连接您的 Sql Server(在 .php 上运行良好)。

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

您可以使用此行代码执行查询。

mixed sqlsrv_query ( resource $conn , string $sql [, array $params [, array $options ]] )
于 2016-11-02T11:04:04.387 回答