Change value of a select in CakePHP

1

I am making a system, and have a part of it that needed a select . The names inside the select are being displayed, but the data stored in the database (mysql) are relative to the 'id' field, and need to be written to the 'name' field of the 'ops' table in the 'operation' field. table 'tabs'.

Relationships:

public $hasMany = array(
    'Op' => array(
        'className' => 'Op',
        'foreignKey' => 'ficha_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => 'operacao',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''

public $belongsTo = array(
    'Ficha' => array(
        'className' => 'Ficha',
        'foreignKey' => 'ficha_id',
        'conditions' => '',
        'fields' => 'operacao',
        'order' => ''

I searched and until now I have not found how to change the 'values' generated by CakePHP.

Structure of the tables: tokens: id, official, server_order, operation, start, end, count, ops_id ops: id, name, acronym, ficha_id, operation (the 'operation' field in 'ops' had to be created based on the relationship between the tables).

    
asked by anonymous 01.07.2014 / 21:45

2 answers

3

Within your ops model, assign the following variable:

public $displayField = 'nome';

And the cake itself will adjust for the select display names correctly ...

Source: link

Update: It is necessary to make the relationships correctly for the find() method to find associations to display them correctly.

For the Model to fetch its members correctly, it is also necessary to set the recursion level of the search operation through the $recursive models attribute.

link

In this case I suggest using level 0, by default the cake uses -1.

Note: The higher the level of recursion, the more data the cake will bring from bd, and consequently the slower the process.

Recursion for all Models, declared within the scope of the AppModel class (Not recommended):

public $recursive = 0;

Set only in the scope of the (recommended) method you want to recursion, or for the Datasheet model, declaring in class scope:

public $this->Ficha->recursive = 0;

After defining the desired recursion, you will find:

$resultado = $this->Ficha->find('all');

The find will bring you, in addition to the 'Ops' model data, also the associated data defined as $hasOne , $belongsTo , $hasMany and HABTM .

To display the method results, pass the resultado variable to your View:

$this->set(compact('resultado'));

or

$this->set('resultado', $resultado);

You will get an indexed array in your view.

To check the structure of the array, debug the array within the view, and show all the data that the find has brought, from the Datasheet model and from the Associated Op model.

<?php
    pr($resultado);

From this, you separate all the fields, as they are displayed in the array, as in this example:

echo $resultado['Op']['nome'];
    
01.07.2014 / 21:58
2

Oops, I delayed because I had dropped that part, but I came back and I did it. In fact, my relationships were reversed, which I took to understand.

public $hasOne = array(
    'Ficha' => array(
        'className' => 'Ficha',
        'foreignKey' => 'operacao',
        'conditions' => '',
        'fields' => 'operacao',
        'order' => ''
    )
);
public $belongsTo = array(
    'Op' => array(
        'className' => 'Op',
        'foreignKey' => 'operacao',
        'dependent' => false,
        'conditions' => '',
        'fields' => 'nome',
        'valueField' => 'nome',
        'order' => '',
        'limit' => '',

    )
);

And the view was:

<?php echo h($ficha['Op']['nome']); ?>

I changed the 'forenigKey' field to 'operation' because it was the field I was interested in being displayed. Actually, I did not change the value of select, which continues to be saved in the bank the 'id' field. However, the output in the view shows the name of the operation and this is the information you need.

But finally, it is resolved and working. Thanks for the tips.

    
24.07.2014 / 22:28