Change the value of a select in Cakephp

Asked

Viewed 1,096 times

1

I’m making a system, and there’s a part of it that needed a select. The names inside the select are being displayed, but the data saved in the database (mysql) is relative to the 'id' field, and need to be saved from the 'name' field of the ops table in the 'operation' field of the chips table'.

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’ve searched and so far I haven’t found how to change the values generated by Cakephp.

Structure of the tables: tokens: id, function, ordem_service, operation, start, end, count, ops_id ops: id, name, acronym, filename, operation ('operation' field in 'ops' had to be created depending on the relation between tables).

  • Reading the documentation I believe this can be done: $options = array("one" => 'one', "two" => 'two); $this->Form->select('Model.field', $options));

2 answers

3

Within your ops model, assign the following variable:

public $displayField = 'nome';

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

Source: http://book.cakephp.org/2.0/en/models/model-attributes.html#displayfield

Update: Relationships need to be performed correctly for the method find() seek associations to display them correctly.

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

http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive

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

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

Recursion for all Models, declared in the scope of the Appmodel class (Not recommended):

public $recursive = 0;

Setting only in the scope of the (recommended) method you want to recur, or to the Form model, declaring in the class scope:

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

After setting the desired recursion, you will perform the find:

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

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

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

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

or

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

You will get an indexed array in your view.

To check the array structure, debug the array within the view, and display all the data that find brought, from the Data Sheet model and from the Associated Op model.

<?php
    pr($resultado);

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

echo $resultado['Op']['nome'];
  • I don’t know what I’m doing wrong, but it’s giving error in the field that should be shown: Notice (8): Undefined index: Op [APP/View/Fichas/index.ctp

  • You made the pr($resultado)? It shows the array structure, and then you check if the indexes are correct, also remove the Fields fields within the relationships, it will restrict the search only to the fields you set there.

  • Did you ever do the cakephp tutorial? It addresses all of these issues, making it easier to understand how cakephp logic works.

  • The operation field (which is the problem) continues to appear the id. About the tutorial, I kind of parachuted into cakephp and did enough to start my application, since my deadline is not too long to finish

  • Put in your question, the structure of the tables, I think you’re having problems with modeling as well. cakephp is pretty boring about this, it needs to follow the conventions, and if you don’t finish the tutorial, it’s pretty tricky to finish this application.

  • I did the blog tutorial but already trying to adapt to my application.

Show 1 more comment

2

Oops, it took me a while because I had left this part, but I came back and got it. In fact, my relationships were reversed, which took me a while 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 stayed:

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

I changed the field 'forenigKey' to 'operation', because it was the field that interested me to be shown. Actually, I did not change the value of select, which is still saved in the database the field 'id'. However, the view output shows the name of the operation and that was the information you needed.

But anyway, it’s solved and working. Thanks for the tips.

  • What happened to your account?

  • Are you the author of the question? In what way, could you explain what you meant by "the relationships were reversed"? It was not clear to me.

  • Good morning. Being more clear about how I solved, my relations between tables were reversed. public $hasOne = array( 'Sheet' => array( 'classname' => 'Sheet', 'foreignKey' => 'operation', 'conditions' => ', 'Fields' => 'operation', 'order' => ' ) ); public $belongsTo = array( 'Op' => array( 'classname' => 'Op', 'foreignKey' => 'operation', 'dependent' => false, 'conditions' => ', 'Fields' => 'name', 'valueField' => 'name', 'order' => ', 'limit' => ', ) ); and the view got: <? php echo h($chip['Op']['name']); ?>

  • I landed the field 'forenigKey' for operation, because it was the field that interested me to be displayed. Actually, I have not changed the value of select, which is still saved in the database the 'id' field. However, the view output shows the name of the operation and that was the information I needed. About my account, I don’t know what happened. The password did not enter, I asked to change and I believe a new account has been created.

  • A new account has been created because your other user was not registered. I asked our community manager to merge their two ok accounts?

  • Thanks @bfavaretto

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.