# Set input data like id from the called model

**URL:** <https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225>\
**Category:** Uncategorized\
**Created:** [April 12, 2021, 9:25pm UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225 "2021-04-12T21:25:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wtamayom](https://avatars.discourse-cdn.com/v4/letter/w/b2d939/32.png) [@wtamayom](https://discourse.cakephp.org/u/wtamayom)\
**Post date:** [April 12, 2021, 9:25pm UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/1 "2021-04-12T21:25:50Z")

</div>

Hi.  
I need some help.

I am in View from Seleccion Model.  
I did create a button for Add for Antecedentes Model (in the seleccion view).  
Antecedentes Model has Seleccion\_id input.

I want to set the value of Antecendete Seleccion\_id = Seleccion id wich I pickup the button.

Thanks.

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [April 13, 2021, 4:11am UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/2 "2021-04-13T04:11:20Z")

</div>

For example this way:  
Add a link in your Seleccion View, where you pass one (or more params):

> \<?= $this-\>Html-\>link(\_\_('Add Antecedentes'), ['controller'=\>'AntecedentesController','action' =\> 'add','?'=\>["foo" =\> "bar",'seleccion\_id'=\>$seleccion-\>id],"#" =\> "first"], ['class' =\> 'side-nav-item']) ?\>

After clicking on the link you can check in your AntecedentesController in the add-action which values were passed:  
`debug($this->getRequest());`

---

<div class="post-metadata">

**Author:** ![wtamayom](https://avatars.discourse-cdn.com/v4/letter/w/b2d939/32.png) [@wtamayom](https://discourse.cakephp.org/u/wtamayom)\
**Post date:** [April 14, 2021, 8:09pm UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/3 "2021-04-14T20:09:39Z")

</div>

Thanks for your answer.  
In the URL I can see the seleccione\_id=2  
Now how I do se it in the add view or controller

echo $this-\>Form-\>control(‘seleccione\_id’, [‘default’ =\> $this-\>request-\>???[‘seleccione\_id’]]);

Thanks.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 14, 2021, 9:49pm UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/4 "2021-04-14T21:49:40Z")

</div>

Is this in your add or edit page? In the edit, you shouldn’t need to set a default at all, as it should be there automatically from the entity you’re editing. In the add, do you want to set a default?

---

<div class="post-metadata">

**Author:** ![wtamayom](https://avatars.discourse-cdn.com/v4/letter/w/b2d939/32.png) [@wtamayom](https://discourse.cakephp.org/u/wtamayom)\
**Post date:** [April 14, 2021, 10:08pm UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/5 "2021-04-14T22:08:06Z")

</div>

Hi.  
Yes is the add action for seleccionAntecendestes that was called from the view of a record in seleccion.  
seleccionAntecedentes stores the seleccion\_id, that is what i want to store deffault.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 14, 2021, 11:17pm UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/6 "2021-04-14T23:17:52Z")

</div>

Ah, gotcha. In your controller, you can use something like  
`$seleccione_id = $this->request->getQuery('seleccione_id');`  
to get the query parameter, and then  
`$this->set(compact('seleccione_id'));`  
to send that variable to the view. Then just `'default' => $seleccione_id`.

Note that if you don’t want them to be able to change the value, then a hidden field instead of an input control would be the way to go.

---

<div class="post-metadata">

**Author:** ![wtamayom](https://avatars.discourse-cdn.com/v4/letter/w/b2d939/32.png) [@wtamayom](https://discourse.cakephp.org/u/wtamayom)\
**Post date:** [April 15, 2021, 2:09am UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/7 "2021-04-15T02:09:44Z")

</div>

Still mising something.

The link in seleccione view

#### \<?= $this-\>Html-\>image("add.png", ['url' =\> ['controller' =\> 'SeleccionAntecedentes','action' =\> 'add','?'=\>['seleccione\_id'=\>$seleccione-\>id]]]) ?\>\<?= \_\_('ANTECEDENTES') ?\>

In seleccioneAntecedente add controller, the value in captured correctly, I echo it and it’s ok, but the value is not setting for storing in db

$seleccionAntecedente = $this-\>SeleccionAntecedentes-\>patchEntity($seleccionAntecedente, $this-\>request-\>getData());  
$seleccione\_id = $this-\>request-\>getQuery(‘seleccione\_id’);  
$this-\>set(compact(‘seleccione\_id’));  
if ($this-\>SeleccionAntecedentes-\>save($seleccionAntecedente)) {  
$this-\>Flash-\>success(**(‘The seleccion antecedente has been saved.’.$seleccione\_id));  
return $this-\>redirect([‘controller’ =\> ‘selecciones’, ‘action’ =\> ‘view’, $this-\>request-\>getData(‘seleccione\_id’)]);  
}  
$this-\>Flash-\>error(**(‘The seleccion antecedente could not be saved. Please, try again.’));

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 15, 2021, 2:43am UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/8 "2021-04-15T02:43:23Z")

</div>

You’ve skipped some code from your controller here. There’s presumably a bit that checks if it’s a post, and does all of this stuff only in that case? You want the `getQuery` and `set` lines to be _before_ that, so they happen when the empty form is loaded, not just when it’s posted.

---

<div class="post-metadata">

**Author:** ![wtamayom](https://avatars.discourse-cdn.com/v4/letter/w/b2d939/32.png) [@wtamayom](https://discourse.cakephp.org/u/wtamayom)\
**Post date:** [April 15, 2021, 1:14pm UTC](https://discourse.cakephp.org/t/set-input-data-like-id-from-the-called-model/9225/9 "2021-04-15T13:14:08Z")

</div>

Thanks so much for all your help and patience.  
You where right just put the code befote de post check.  
Thanks again.
