3

I have faced a problem. I have a form and form field name card_number. card_number comes from database. Entry Card Number that denotes card_number filed. In my ctp file I have written this.

<div class="form-group">
    <label class="col-sm-2 control-label"> Entry Card Number </label>
    <div class="col-md-10">
        <?php echo $this->Form->input('card_number', array('options' => $readCard,'class'=>'form-control','div'=>false,'label'=>false));?>
    </div> 
</div>

Entry Card Number

I have written this code in my Controller. And my view looks like above image.

public function add_card_to_device($btsId = null){
    $readerData = $this->CardManagement->find('list',
        array(
            'conditions'    =>  array('site_name'=>$site_name),
            'fields'        =>  array('card_number'),
            'keyField'      => 'card_number',
            'valueField'    => 'card_number'    
        )
    );
    debug($readerData);
    $this->set('readCard', $readerData);
    if ($this->request->is('post')|| $this->request->is('put')) {
        $dataa          =   $this->request->data;
        print_r($dataa);
    }
}

The problem is occurred when I click submit button. When I click the submit button I get these value. I get the id value instead of card_number [card_number] => 5a13b3d9-67ac-4847-b3f9-1870991894ac

Array ( [CardManagement] => Array ( [id] => 5a12d321-a7e0-4cf6-ab84-1870991894ac [site_name] => 1235 [card_number] => 5a13b3d9-67ac-4847-b3f9-1870991894ac ) )

But my desired output looks like this. [card_number] => 6473088

Array ( [CardManagement] => Array ( [id] => 5a12d321-a7e0-4cf6-ab84-1870991894ac [site_name] => 1235 [card_number] => 6473088 ) )

Any solution or suggestion please.

1 Answer 1

2

Cakephp 3.x

When calling list you can configure the fields used for the key and value with the keyField and valueField options respectively:

https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs

    $readerData = $this->CardManagement->find('list',
        array(
            'conditions'    =>  array('site_name'=>$site_name),
            'fields'        =>  array('card_number'),
            'keyField'      => 'card_number',
            'valueField'    => 'card_number'
        )
    );

Cakephp 2.x

https://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list

    $readerData = $this->CardManagement->find('list',
        array(
            'conditions' => array('site_name' => $site_name),
            'fields' => array('card_number','card_number'),
        )
    );
Sign up to request clarification or add additional context in comments.

6 Comments

I have change my Controller but output is same. Have any change in view??
No you don't have to try to debug the $readerData like this debug($readerData);. What is the output?
After debug($readerData); I get this array( '5a12d321-a7e0-4cf6-ab84-1870991894ac' => '3369515', '5a13b3d9-67ac-4847-b3f9-1870991894ac' => '6473088' )
As I run the same on my local machine it's working fine. Can you update the question with the latest code
I have updated my controller. I am using Cakephp 2.7.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.