2016-05-03 3 views
0

cakephp 3.xのデータベースに複数の値を格納しようとしましたが、選択したすべての値を取得できません。一つだけ。私のコントローラでselect cakephpから複数の値を取得中にエラーが発生しました

<select name="internalDestinations[ids]" id="internalDestinations-ids" multiple="true" class="..."> 
<?php 
    foreach ($users2 as $i): ?> 
     <option value="<?= $i['email'] ?>"> <?= $i['email'] ?> </option> 
    <?php endforeach; 
    ?> 
</select> 

:私の見解では

if($this->request->is('post')){ 
    $alarm->internalDestinations=$this->request->data['internalDestinations']['ids']; 
    $this->log($this->request->data['internalDestinations']); 
} 

そして、私の入力で複数の項目を選択し、私は1得る:

Array 
(
    [ids] => [email protected] 
) 

すべてのヘルプ? どうもありがとう

答えて

1

あなたは単に

name="internalDestinations[ids][]" 

が、なぜFormHelperを使用していないを行うことができますか?

//first of all let's create an array with email both in the keys and in the values 

$users3 = Cake\Utility\Hash::combine($users2, '{n}.email', '{n}.email'); 

// then let's use cake dotted notation to create arrays 

<?= $this->Form->input('internalDestinations.ids', [ 
     'type' => 'select', 
     'multiple' => true, 
     'options' => $users3 
    ]); ?> 
+1

私はinternalDestinationsが何であるか分かりません。あなたが達成しようとしていることはわかりません。私はあなたに複数の値を渡す方法を示しました。関連するモデルを保存しようとする場合は、マニュアル[ここ](http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-associations)と[ここ](http ://book.cakephp.org/3.0/ja/views/helpers/form.html#creating-inputs-for-associated-data) – arilia

+0

基本的なPHP:use [implode](http://php.net/manual/ it/function.implode.php)関数を呼び出します。しかし、コンマ区切りの値を1つのフィールドに格納するのは悪い考えです。 – arilia

関連する問題