简体   繁体   English

Codeigniter活动记录问题

[英]Codeigniter active record issue

Ok, so I have following SQL query: 好的,所以我有以下SQL查询:

INSERT INTO film_genre
    (film_id, genre_id)
VALUES
    (1, 1),
    (1, 2),
    (1, 3)

I need to convert this into codeigniters active records and this is what I got so far but I only get one data entry out of three: 我需要将其转换为codeigniters活动记录,这是到目前为止的结果,但是我只能从三项中获得一项:

     $movie_genre = array(
            'film_id' => $movies['id'],
            'genre_id' => 1, 
            'film_id' => $movies['id'],
            'genre_id' => 2, 
            'film_id' => $movies['id'],
            'genre_id' => 3, 
            );

$this->db->insert('film_genre', $movie_genre );

If you execute a print_r($movie_genre) before your insert you will see that there is just a single value for film_id and genre_id . 如果在插入之前执行print_r($movie_genre) ,将会看到film_idgenre_id只有一个值。 This is because there can be only one value for each key in an array. 这是因为数组中的每个键只能有一个值。 You are defining three values for each key, so your earlier values are getting overwritten. 您正在为每个键定义三个值,因此您先前的值将被覆盖。

If you format your array as follows, you can use the insert_batch method. 如果按如下所示格式化数组,则可以使用insert_batch方法。 See a full description of the method at http://ellislab.com/codeigniter/user-guide/database/active_record.html 请在http://ellislab.com/codeigniter/user-guide/database/active_record.html上查看该方法的完整说明。

$records = array(
    array('film_id' => 1, 'genre_id' => 1),
    array('film_id' => 1, 'genre_id' => 2),
    array('film_id' => 1, 'genre_id' => 3)
);

$this->db->insert_batch('film_genre', $records);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM