简体   繁体   中英

Populate selectable field from DB in Zend 2 Album example

I'm starting programming with Zend and I am using the default Album example they provide as a "getting started" guide.

I would like to have a select drop down field in the form, but I can't find an easy way of doing it like the code is right now

This is how I'm doing it without consulting DB from my UsersForm.php

$this->add(array(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'level',
    'options' => array(
        'label' => 'User level',
        'value_options' => array(
                    '1' => 'admin',
                    '2' => 'boss',
                    '3' => 'assistent',
                    '4' => 'client',
        ),
    ),
));

UPDATE

Ok, so following that tutorial using TableGateway I managed to have a selectable but is grabbing data from the 'project' table because the rest of my fields need that table, but I need that selectable to get from 'user' table.

My Module.php looks like this:

public function getServiceConfig()
{
    return array(
        'invokables' => array(),
        'factories' => array(
            'Project\Model\ProjectTable' =>  function($sm) {
                $projectTableGateway = $sm->get('ProjectTableGateway');
                $usersTableGateway = $sm->get('UsersTableGateway');
                $table = new ProjectTable($projectTableGateway, $usersTableGateway);
                return $table;
            },
            'project-model-selectable' =>  function($sm) {
                $tableGateway = $sm->get('selecttable-gateway');
                $table = new SelectTable($projectTableGateway, $usersTableGateway);
                return $table;
            },
            'ProjectTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Project());
                return new TableGateway('project', $dbAdapter, null, $resultSetPrototype);
            },
            'UsersTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Users());
                return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
            },
            'selecttable-gateway' => function ($sm) {
                $dbAdapter          = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new SelectOption());
                return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
            },
        ),
    );
}

And my getProject function in ProjectTable.php:

public function getProject($id)
    {
        $id  = (int) $id;

        $rowset = $this->projectTableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

My addAction in ProjectController.php

public function addAction()
    {
        $tableGateway = $this->getServiceLocator()->get('Project\Model\ProjectTable');
        $form = new ProjectForm($tableGateway);
        $form->get('submit')->setValue('Nuevo');

        $request = $this->getRequest();
        if ($request->isPost()) {

            $project = new ProjectForm($tableGateway);
            $form->setInputFilter($project->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $project->exchangeArray($form->getData());
                $this->getProjectTable()->saveProject($project);

                // Redirect to list of projects
                return $this->redirect()->toRoute('project');
            }
        }
        return array('form' => $form);
    }

Thanks

public function getProject($id)
{
    $id  = (int) $id;

    $rowset = $this->projectTableGateway->select(array('id' => $id));
    $row = $rowset->current();
    if (!$row) {
        throw new \Exception("Could not find row $id");
    }
    return $row;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM