使用SAVE()函数插入多个行- Joomla 2.5


INSERT more than one rows using SAVE() function - Joomla 2.5

我有自己的save()函数来将Joomla表单的数据保存到DB中。这就是

的样子

注意:我已经编写了自己的save()函数,因为我必须在一次保存操作中保存到两个表中

class footballModelPlayer extends JModelAdmin {
enter code here
  public function save($data) {
    $table_two = $this->getTable('player_sec_positions', 'footballTable', array());
    $player_id = $data['player_id'];
    $player_sec_positions_data = array();
    foreach ($data['sec_position_name'] as $pos_name) {
        $player_sec_positions_data['player_id'] = $player_id;
        $player_sec_positions_data['sec_position_name'] = $pos_name;
        // var_dump($player_sec_positions_data);
        $table_two->bind($player_sec_positions_data);
        $table_two->save($player_sec_positions_data);
    }
    return $data->player_id;
  }
}

问题是,我不能保存多行,只有这段代码做的是,它保存最后的array(见下文),其中sec_position_nam

我想保存所有行到DB ||如何使用save() joomla函数输入多行到DB。

var_dump($player_sec_positions_data)的输出是这样的…

   array
  'player_id' => int 1
  'sec_position_name' => string 'Left' (length=4)
   array
  'player_id' => int 1
  'sec_position_name' => string 'Middle' (length=6)
   array
  'player_id' => int 1
  'sec_position_name' => string 'Right' (length=5)

必须将表放入循环中,例如:

class footballModelPlayer extends JModelAdmin
{
    public function save ($data)
    {
        foreach ($data['sec_position_name'] as $pos_name)
        {
            $table_two = $this->getTable('player_sec_positions', 'footballTable', array());
            $player_sec_positions_data = array();
            $player_sec_positions_data['player_id'] = $data['player_id'];
            $player_sec_positions_data['sec_position_name'] = $pos_name;
            // var_dump($player_sec_positions_data);
            $table_two->bind($player_sec_positions_data);
            $table_two->save($player_sec_positions_data);
        }
        return $data['player_id'];
    }
}

我是这样解决的。

也许这不是一个好方法。

$player_id = $data['player_id'];
$player_sec_positions_data = array();
$db = $this->getDBO();
$query = "INSERT INTO #__football_player_sec_positions (player_id,sec_position_name)
          VALUES ";
foreach ($data['sec_position_name'] as $pos_name) {
    $player_sec_positions_data['player_id'] = $player_id;
    $player_sec_positions_data['sec_position_name'] = $pos_name;
    $query .= "($player_id, '$pos_name'),";
}
$query = rtrim($query, ",");

$db->setQuery($query);
$db->query();