# Select data from other models on Form

**URL:** <https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005>\
**Category:** Need Help\
**Created:** [February 7, 2017, 2:38pm UTC](https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005 "2017-02-07T14:38:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rennanbighi](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rennanbighi](https://discourse.cakephp.org/u/rennanbighi)\
**Post date:** [February 7, 2017, 2:38pm UTC](https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005/1 "2017-02-07T14:38:05Z")

</div>

Hey Guys. I am working on a form for adding an museum object on database. I have a field where I should select the object donator, wich belongs to another table. The way I did it, I can already select the donators by name on the form, but then this happens: ![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/1X/82b4306beb7188443a56d07f5452fedfc3db9d60.png)

Well, I want / need to remove these numbers from my select view, but can´t figure out how I do that.

Here is my controller function add code:

```
  public function add()
{

    $connection = ConnectionManager::get('default');
    $objeto = $this->Objeto->newEntity();
    if ($this->request->is('post')) {
        $objeto = $this->Objeto->patchEntity($objeto, $this->request->data);
        if ($this->Objeto->save($objeto)) {
            $this->Flash->success(__('The objeto has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The objeto could not be saved. Please, try again.'));
    }
    $this->set(compact('objeto'));
    $this->set('_serialize', ['objeto']);

    $this->loadModel('Doador');
    $listaDoadores = $this->Doador->find('all', array(
    'fields' => array('Doador.nome')));

    $doadores = $connection->execute($listaDoadores)->fetchAll('assoc');

    if ($this->request->is('requested')) { //Se for requisição de outra view/element:
        return $doadores;
    } else { //Senão envia para a view padrão
        $this->set('doadores', $doadores);
    } ?>

    <?php 
                    
    print_r($doadores);

}

```

And this is my View so far:

```
<?= $this->Form->create($objeto) ?>
<fieldset>
    <legend><?= __('Add Objeto') ?></legend>
    <?php
        echo $this->Form->input('nome');
        echo $this->Form->input('nomeTecnico');
        echo $this->Form->input('tipo');
        echo $this->Form->input('aquisicao');
        echo $this->Form->input('procedencia');
        echo $this->Form->input('dataDoacao', ['empty' => true]);
        echo $this->Form->input('dataObjeto');
        echo $this->Form->input('forma');
        echo $this->Form->input('cor');
        echo $this->Form->textarea('detalhamento', ['placeholder' => 'Detalhamento do Objeto']);
        echo $this->Form->input('dimensao_iddimensao');
        echo $this->Form->input('foto_idfoto');
        echo $this->Form->input('inscricao_idinscricao');

        echo $this->Form->input('doador_id', ['type' => 'select', 'options' => $doadores]);
        echo $this->Form->input('fabricante');
        echo $this->Form->input('historico');
        echo $this->Form->textarea('etiqueta_exposicao', ['placeholder' => 'Descrição da etiqueta de Exposição']);
        echo $this->Form->input('emExposição');
        echo $this->Form->input('condicao',['type' => 'select', 'options' => ['nome1' => 'Perfeito Estado', 'nome2' => 'Necessita Intervenção']]);
        echo $this->Form->input('condicao');
        echo $this->Form->input('catalogador');
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

```

Can Anyone Help me see realize what am I doing wrong?  
Thanks very much in advance!

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/marc/32/508_2.png) [@marc](https://discourse.cakephp.org/u/marc)\
**Post date:** [February 7, 2017, 6:21pm UTC](https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005/2 "2017-02-07T18:21:16Z")

</div>

In a cake 2.8 app I do same like this.

in the controller

> ```
> $proofs = $this->UserActivity->Proof->find('list', array('conditions'=>array($jointext),'order' => 'plist ASC'));
> $this->set(compact('proofs'));
> 
> ```

and in the view in the form

```
           echo $this->Form->input('proof_id', array(
				 'class' => 'input select',
				 'label' => 'Lernnachweis',
				 'type' => 'select',
				 'empty' => true,
				 'style'	=> 'width:130px;'
			));

```

Perhaps its a new point of view.

Marc

---

<div class="post-metadata">

**Author:** ![sohelrana820](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/sohelrana820/32/362_2.png) [@sohelrana820](https://discourse.cakephp.org/u/sohelrana820)\
**Post date:** [February 7, 2017, 9:26pm UTC](https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005/3 "2017-02-07T21:26:15Z")

</div>

@marc Replace this sort of codes

```
$this->loadModel('Doador');
$listaDoadores = $this->Doador->find('all', array(
'fields' => array('Doador.nome')));

```

With

```
$this->loadModel('Doador');
    $listaDoadores = $this->Doador->find('list',
        [
            'keyField' => 'id',
            'valueField' => 'nome'
        ]);

```

And try again!

---

<div class="post-metadata">

**Author:** ![rennanbighi](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rennanbighi](https://discourse.cakephp.org/u/rennanbighi)\
**Post date:** [February 7, 2017, 10:07pm UTC](https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005/4 "2017-02-07T22:07:24Z")

</div>

I tried this, but it returns the same I had before plus the donator ID. =/

---

<div class="post-metadata">

**Author:** ![rennanbighi](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rennanbighi](https://discourse.cakephp.org/u/rennanbighi)\
**Post date:** [February 7, 2017, 10:14pm UTC](https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005/5 "2017-02-07T22:14:05Z")

</div>

Tried to adapt your solution here, but I´m having some trouble with the $jointext variable. What would it be?

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/marc/32/508_2.png) [@marc](https://discourse.cakephp.org/u/marc)\
**Post date:** [February 8, 2017, 8:34am UTC](https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005/6 "2017-02-08T08:34:53Z")

</div>

$jointext ist only a embarrassing expression of my stupidity 🙂

```
		$jointext = '';
		if ($useractivity['User']){
			$user = $this->UserActivity->User->findById($useractivity['User']['id']);
			$nr = 0;
			foreach($user['Subject'] as $subs){
				$nr += 1;			
				if($nr > 1){				
					$jointext = $jointext . ' OR';			
				}
				$jointext = $jointext . ' subject_id = ' . $subs['id'];
			}
		}

```

thats all.

---

<div class="post-metadata">

**Author:** ![redwormx](https://avatars.discourse-cdn.com/v4/letter/r/7ea924/32.png) [@redwormx](https://discourse.cakephp.org/u/redwormx)\
**Post date:** [March 21, 2017, 4:13pm UTC](https://discourse.cakephp.org/t/select-data-from-other-models-on-form/2005/7 "2017-03-21T16:13:55Z")

</div>

Thanks for your help, I had several days trying to make this work
