2

学生プロジェクトで Zend Framework 1.12.7 を使い始めました。データベースからのオブジェクト (グループとロール) を入力したい、さまざまなテキスト フィールドと 2 つのドロップダウン メニューを含むフォームと、addAction を使用してユーザー クラスをプログラミングしています。アイデアは、ドロップダウン リスト (名前で表される) から 1 つのオブジェクトを選択し、適切なオブジェクトの ID をデータベースに保存することです。

Zend Framework が連想配列で動作していることは既にわかっており、ドロップダウン メニューから名前を選択することができます。ただし、データベースにはまだ何も保存されていません。

ロール オブジェクトのコントローラー、フォーム、およびモデル コードを提供します。

コントローラ:

 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');
            }
        }

形:

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

モデル:

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

では、選択したオブジェクトにアクセスしてその ID をデータベースに保存するにはどうすればよいでしょうか?

ご助力ありがとうございます!

乾杯!

マーティン

4

0 に答える 0