1

私は解決策を高低で検索しましたが、これを理解できないようです。私がやろうとしているのは、製品を追加するときに、フォームの入力から名前フィールドに入力することです。したがって、名前には、ユーザーがtype_id、category_id、およびsubcategory_idに対して選択した値が含まれます。誰かがこれを達成する方法を知っていますか?

製品の追加ページを表示

    <fieldset>
    <legend><?php echo __('Add Product'); ?></legend>
<?php
    echo $this->Form->input('type_id');
    echo $this->Form->input('category_id', array('label' => 'Vendor'));
    echo $this->Form->input('subcategory_id', array('label' => 'Model'));
    echo $this->Form->input('location', array('label' => 'Location'));
    echo $this->Form->input('sku', array('label' => 'Asset Tag'));
    echo $this->Form->input('mac');
    echo $this->Form->input('description', array('label' => 'Notes'));
    echo $this->Form->input('name', array( 'value' => ['type_id']['category_id']  , 'type' => 'hidden'));
    //echo $this->Form->input('cost');
    // echo $this->Form->input('Tag');
    ?>
    </fieldset>

製品コントローラー追加機能

    public function add() {
    if ($this->request->is('post')) {
        $this->Product->create();
        if ($this->Product->save($this->request->data)) {
            $this->Session->setFlash(__('The product has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
        }
    }
    $subcategories = $this->Product->Subcategory->find('list',array('order'=>'Subcategory.name asc'));
    $categories = $this->Product->Category->find('list',array('order'=>'Category.name asc'));
    $types = $this->Product->Type->find('list',array('order'=>'Type.name asc'));
    $this->set(compact('subcategories', 'categories', 'types'));

}
4

1 に答える 1

1

あなたがしようとしている方法でそれを行うには、クライアント側のJavaScriptを使用して入力値を「オンザフライ」で更新する必要がありますが、それはあまり安全ではなく、簡単に混乱する可能性があります. 名前の入力を完全に削除し、これを Product モデルのbeforeSaveメソッドで処理する (または、保存する直前にコントローラーで名前の値を定義する) 方がはるかに理にかなっています。

public function beforeSave($options = array()) {
    // Generate the name based on type and category
    $this->data['Product']['name'] = $this->data['Product']['type_id'] .
                                     $this->data['Product']['category_id'];

    return true;
}

あなたのコメントに基づいて更新します。

名前を取得するには、それらの名前を見つけて (モデルが関連付けられていると仮定して)、それらを定義します。

public function beforeSave($options = array()) {
    // Get the type name
    $type = $this->Type->field('name', array(
        // Set the condition for the field
        'Type.id' => $this->data['Product']['type_id']
    ));

    // Get the category name
    $category = $this->Category->field('name', array(
        // Set the condition for the field
        'Category.id' => $this->data['Product']['category_id']
    ));

    // Generate the name based on type and category
    $this->data['Product']['name'] = $type . $category;

    return true;
}
于 2013-02-27T19:19:04.410 に答える