Zend Framework: How to save Objects from a select form to a database?

532 Views Asked by At

I started to work with Zend Framework 1.12.7 for a student project. I'm porgramming a User Class with an addAction and a form with various text fields and two dropdown menus that I want to fill with objects from the database (groups and roles). The idea is to select one object from the dropdown list (represented by its name) and save the id of the appropriate object to the database.

I already found out, that Zend Framework is working with associative arrays and I'm already able to select the names out of the dropdown menu. But however, there is still nothing saved to the database.

I provide you my controller, form and model code for the role object:

Controller:

 public function addAction()
    {
        $request = $this->getRequest();

        $rollen = new Application_Model_Rolle();
        $mapper = new Application_Model_RolleMapper();
        $rollen = $mapper->fetchAll();
        $options;
        foreach ($rollen as $rolle) {
            $key = (string) $rolle->bezeichnung;
            $options[] = array($key => $rolle->bezeichnung );
            $values[] = array($key => $rolle);
        }

        $form    = new Application_Form_Benutzer();
        $form->getElement('rolle')->setMultiOptions($options);
        $form->getElement('rolle')->setValues($values);


        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                $benutzerdaten = new Application_Model_Benutzer($form->getValues());
                die($form->getValue('rolle'));
                $mapper  = new Application_Model_BenutzerMapper();
                $mapper->save($benutzerdaten);
                return $this->_helper->redirector('index');
            }
        }

Form:

$this->addElement('select', 'rolle', array(
                'label'      => 'Rolle:',
                'required'   => false,
        ));

Model:

public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Benutzer();
            $entry->setb_Id($row->b_id)
            ->setNachname($row->nachname)
            ->setVorname($row->vorname)
            ->setBenutzername($row->benutzername)
            ->setEmail($row->email)
            ->setTelefon($row->telefon)
            ->setPasswort($row->passwort);

            $gruppeID = $row->g_id;
            $gruppe = new Application_Model_Gruppe();
            $mapper = new Application_Model_GruppeMapper();
            $gruppe = $mapper->find($gruppeID, $gruppe);

            $rolleID = $row->r_id;
            $rolle = new Application_Model_Rolle();
            $mapper = new Application_Model_RolleMapper();
            $rolle = $mapper->find($rolleID, $rolle);

            $entry->setRolle($rolle);
            $entry->setGruppe($gruppe);

            $entries[] = $entry;
        }

        return $entries;
    }

So how can I access the chose object and save its ID to the database?

Thank you very much for your help!

Cheers!

Martin

0

There are 0 best solutions below