0
votes

I'm sorry, I am a newbie in CakePHP and I am a little bit confused in this subject, let me explain:

I have a relationship between two tables. One of the table is Dose and the other is tank. So, one Tank belongs to a Dose. A Dose has many Tanks. The table schema is:

CREATE TABLE `doses` (
    `id` INT(10) NOT NULL AUTO_INCREMENT,
    `dose` INT(5) NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)

In my Tank view I have the following code:

<?php echo $form->input('dose_id', array('class'=>'input', 'label' => ''));?>

Each 'dose' (field) from Dose table correspond to a value, such as 200, 300, and so forth. I need to use these numbers to calculate others numbers before to insert into my database (table tank). For instance, my code in tanks_controllers:

$t_u = $this->data['Tank']['tipo_uso_id'];
if( $t_u == '1'){
$this->data['Tank']['producao_adubo_diaria'] = $this->data['Tank']['dose_id'] * 0.10;
.
.
.

However, it is bringing to me the ID of the Dose and not the value (dose field). Where can I set up this to bring me the correct data (dose)? I tried to set up this way in my model:

'Dose' => array(
        'className' => 'Dose',
        'foreignKey' => 'dose_id',
        'conditions' => '',
        'fields' => 'dose',
        'order' => ''
    )

It did not work.

I appreciate your time helping me.

Thanks in advance.

2

2 Answers

0
votes

it is bringing to me the ID of the Dose and not the value (dose field). Where can I set up this to bring me the correct data (dose)?

You need to get it from the db (model), not from the view. So you need to do a find(). If you are new to Cake, you should read the cookbook first to see how it works.

0
votes

What does $form->input('dose_id') produce? A dropdown? If so; by default cake will produce a dropdown with the value containing (dose_)id, and the text you see as the value of $displayField(usually name/title).

To do this; if I understand you, you would need to first query doses for all the values and store the result in an array using the dose value as the key AND the value, rather than the id as you normally would. You would then be able to access the actual dose value from $this->data.

$doseArray=array();
$doses = $this->Dose->find('all');
foreach($doses['Dose'] as $k => $v) {
    $doseArray[$v] = $v;
}

perhaps. Seems a bit redundant so I might be off.