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

时间:2014-07-09 05:59:20

标签: php joomla

我有自己的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)

2 个答案:

答案 0 :(得分:0)

你必须在循环中获取表格,例如:

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'];
    }
}

答案 1 :(得分:0)

我用它解决了它。

也许这不是一个好方法。

$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();