Cakephp genereer options vanuit database

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Rolf -

Rolf -

20/10/2012 18:40:55
Quote Anchor link
Hoi allemaal,

Ik ben bezig met cakephp en sommige dingen zijn nog wat lastig.

Situatieschets:
Ik heb bedrijven in een tabel staan en ik heb branches in een tabel staan.
Nu wil op de invoerpagina voor nieuwe bedrijven een select lijstje maken met de branches uit de database.

Iemand enig idee hoe ik dit moet doen?
 
PHP hulp

PHP hulp

29/03/2024 03:31:54
 
Wouter J

Wouter J

20/10/2012 19:30:01
Quote Anchor link
Ik ken CakePHP niet, even globaal:
- Haal branch namen uit de tabel
- Sla deze op in variabele
- Importeer variabele in template
 
Mar cel

Mar cel

21/10/2012 00:29:41
Quote Anchor link
CONTROLLER:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
$this->set('branches', $this->Company->Branche->find('list'));


VIEW:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
echo $this->Form->select('branche_id');

of
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
echo $this->Form->input('branche_id', array('options'=>$branches));
Gewijzigd op 21/10/2012 00:31:09 door Mar cel
 
Rolf -

Rolf -

21/10/2012 13:49:53
Quote Anchor link
Dan doet hij het nog niet. Ik krijg de volgende foutmelding:
Error: Call to a member function find() on a non-object
File: /home/brabants-genoegen/public_html/cakephp/app/Controller/ContactsController.php
Line: 11

Ik zal de codes ook even geven.
Model voor bedrijven (Contact.php)
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
class Contact extends AppModel {
    public $validate = array(
                    'companyName' => array('rule' => 'notEmpty', 'message' => 'Vul aub een bedrijfsnaam in.'),
                    'sector' => array('rule' => 'notEmpty', 'message' => 'Vul aub een branche in.'),
                    'adress' => array('rule' => 'notEmpty', 'message' => 'Vul aub een adres in.'),
                    'zip' => array('rule' => 'notEmpty', 'message' => 'Vul aub een postcode in.'),
                    'city' => array('rule' => 'notEmpty', 'message' => 'Vul aub een plaats in.'),
                    'telephone' => array('rule' => 'notEmpty', 'message' => 'Vul aub een telefoonnummer in.'),
                    'website' => array('rule' => array('url', true), 'message' => 'Vul aub een correcte website in.'),
                    'contact' => array('notEmpty'=>array('rule'=>'notEmpty', 'message'=>'Vul aub een contactpersoon in.'),'isUnique'=>array('rule'=>'isUnique', 'message'=>'Contact bestaat al!')),
                    'email' => array('email'=>array('rule'=> array('email', true), 'message'=>'Vul aub een correct emailadres in.'),'isUnique'=>array('rule'=>'isUnique', 'message'=>'Emailadres bestaat al!')),
                    );
    var
$name = 'Contact';
    var
$belongTo = 'Sector';


}

?>


Controller (ContactsController.php)
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
class ContactsController extends AppController {
    public $helpers = array('Html', 'Form');
    
    public function index() {
        $this->set('contacts', $this->Contact->find('all', array('order' => array('Contact.companyName ASC'))));
    }

    
    public function addcontact()
    {

        $this->set('sectors', $this->Contact->Sector->find('list'));
        
        if ($this->request->is('post'))
        {

            if($this->Contact->save($this->request->data))
            {

                $this->Session->setFlash('Contact is toegevoegd!');
                $this->redirect(array('action' => 'index'));
            }
else {
                $this->Session->setFlash('Contact kon niet worden toegevoegd. Kijk of alles goed ingevuld is of probeer later opnieuw');
            }
        }
    }

    
}

?>


View waar ik de select wil (addcontact.ctp):
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!-- File: /app/View/Contact/addcontact.ctp -->
<h1>Voeg een contact toe</h1>

<?php
echo $this->Html->link('Terug naar het overzicht', array('controller' => 'contacts', 'action' => 'index'));
echo "<br/>";
echo $this->Form->create('Contact');
echo $this->Form->input('companyName', array('label' => 'Bedrijfsnaam'));
echo $this->Form->input('remark', array('label' => 'Opmerking'));
echo $this->Form->input('sector', array('label' => 'Branche'));
echo $this->Form->select('sector_id');
echo $this->Form->input('sector_id', array('options'=>$sectors));
echo $this->Form->input('adress', array('label' => 'Adres'));
echo $this->Form->input('zip', array('label' => 'Postcode'));
echo $this->Form->input('city', array('label' => 'Plaats'));
echo $this->Form->input('telephone', array('label' => 'Telefoon'));
echo $this->Form->input('fax');
echo $this->Form->input('email');
echo $this->Form->input('website', array('value' => 'http://'));
echo $this->Form->input('contact', array('label' => 'Contactpersoon'));
echo $this->Form->input('telephoneDirect', array('label' => 'Directe telefoon'));
echo $this->Form->input('mobile', array('label' => 'Mobiel'));
echo $this->Form->end('Opslaan');
?>


Daarnaast heb ik nog een model voor de tabel sectors en een controller die alle sectors uitleest. Maar die lijkt me niet relevant?
Gewijzigd op 21/10/2012 13:50:41 door Rolf -
 
Rolf -

Rolf -

24/10/2012 12:18:15
Quote Anchor link
Niemand? :(
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.