2

I have created two database tables:

  • sellers_tbls

    • id
    • business_name
    • businesstype_tbls_business_type
    • mobile
  • businesstype_tbls

    • id
    • business_type

where businesstype_tbls is for displying dropdown types upon registration.

After submitting the form, register() is saving businesstype_tbls.id in sellers_tbls.businesstype_tbls_business_type.

How can I get business_type instead of id in my sellers_tbls?

My code is as follows:

register.ctp

<tr>
  <td>Business type</td>
  <td><?php echo $this->Form->select('businesstype_tbls_business_type',$drop);?></td>
</tr>

SellerController

public function register()
{
    $this->loadModel("Seller");

    if($this->Seller->save($this->request->data))
    {
        $this->Session->setFlash('Successully save your information!');
    }

    $this->loadModel("Businesstype");
    $dt=$this->Businesstype->find('list',array('fields'=>array('id','business_type')));
    $this->set('drop',$dt);
}

Model Businesstype.php

class Businesstype extends AppModel
{
    public $name = 'businesstype_tbls';
    public $hashMany = 'sellers_tbls';
}

Model Seller.php

class Seller extends AppModel
{
    public $name = 'sellers_tbls';
    public $belongsTo = 'businesstype_tbls';
}
Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
Klaus Mikaelson
  • 572
  • 6
  • 17

1 Answers1

2

Saving businesstype_tbls.id in sellers_tbls.businesstype_tbls_business_type is the expected behavior. This is the whole purpose of setting up relationships between models.

If you wish to store business_type in plain text:

  1. Remove the relationships
  2. Set the type of sellers_tbls.businesstype_tbls_business_type to something that can hold the content of businesstype_tbls.business_type
  3. Set $dt to an associative array with every key set to its corresponding value:

    $dt=$this->Businesstype->find('list',array(
        'fields'=>array('business_type','business_type')
    ));
    

Having said this, if you are building a new app and designing the data model from scratch, I recommend that you shorten your table and field names. Your naming strategy is not only complex, but it also doesn't follow CakePHP conventions, which will make your life harder in the long run.

Why not just the following?

  • sellers

    • id
    • business_name
    • businesstype (or businesstype_id if you want to store businesstypes.id)
    • mobile
  • businesstypes

    • id
    • type
Inigo Flores
  • 4,461
  • 1
  • 15
  • 36