2013-06-30 5 views
0

Oke, 제목에서 제안하는 바와 같이 하나의 데이터베이스 필드에 중첩 배열의 여러 값을 삽입하려고합니다.Codeigniter - 데이터베이스에 중첩 배열의 다중 값을 삽입합니다.

아래의 배열은 내 데이터베이스의 여러 레코드가 아닌 하나의 레코드에 저장됩니다.

내 어레이 모양입니다.

array(5) { 
    ["stars"]=> 
    array(5) { 
    [0]=> 
    string(10) "Chris Pine" 
    [1]=> 
    string(14) "Zachary Quinto" 
    [2]=> 
    string(11) "Zoe Saldana" 
    [3]=> 
    string(10) "Karl Urban" 
    [4]=> 
    string(10) "Simon Pegg" 
    } 
    ["directors"]=> 
    array(1) { 
    [0]=> 
    string(11) "J.J. Abrams" 
    } 
    ["writers"]=> 
    array(4) { 
    [0]=> 
    string(12) "Roberto Orci" 
    [1]=> 
    string(13) "Alex Kurtzman" 
    [2]=> 
    string(14) "Damon Lindelof" 
    [3]=> 
    string(16) "Gene Roddenberry" 
    } 
    ["genres"]=> 
    array(4) { 
    [0]=> 
    string(6) "Action" 
    [1]=> 
    string(9) "Adventure" 
    [2]=> 
    string(6) "Sci-Fi" 
    [3]=> 
    string(8) "Thriller" 
    } 
    ["movie_data"]=> 
    array(12) { 
    ["imdb_id"]=> 
    string(9) "tt1408101" 
    ["slug"]=> 
    string(23) "star-trek-into-darkness" 
    ["title"]=> 
    string(23) "Star Trek Into Darkness" 
    ["imdb_rating"]=> 
    string(3) "8.2" 
    ["release_date"]=> 
    string(11) "6 June 2013" 
    ["runtime"]=> 
    string(4) "132m" 
    ["mpaa_rating"]=> 
    string(5) "PG-13" 
    ["storyline"]=> 
    string(554) "When the crew of the Enterprise is called back home, they find an unstoppable force of terror from within their own organization has detonated the fleet and everything it stands for, leaving our world in a state of crisis. With a personal score to settle, Captain Kirk leads a manhunt to a war-zone world to capture a one man weapon of mass destruction. As our heroes are propelled into an epic chess game of life and death, love will be challenged, friendships will be torn apart, and sacrifices must be made for the only family Kirk has left: his crew." 
    ["plot"]=> 
    string(202) "After the crew of the Enterprise find an unstoppable force of terror from within their own organization, Captain Kirk leads a manhunt to a war-zone world to capture a one man weapon of mass destruction." 
    ["poster_large"]=> 
    bool(false) 
    ["poster"]=> 
    string(97) "http://ia.media-imdb.com/images/M/[email protected]@._V1._SY500.jpg" 
    ["trailer"]=> 
    string(157) "http://www.youtube.com/v/QAEkuVgt6Aw&feature=youtube_gdata_player?color2=FBE9EC&hd=1&autoplay=1&showsearch=0&version=3&modestbranding=1&fs=1&iv_load_policy=3" 
    } 
} 

나는 일반적인 배열 내부의 "별"라는 배열을 가지고 내 데이터베이스에서 하나 개의 필드에서 그 "별"배열의 모든 값을합니다. 쉬운 검색과 검색을 위해 별이라고도합니다.

나는 codeigniters batch_insert 함수를 살펴 봤지만 필요가 없으면하고 싶지 않습니다.

앞으로 나아갈 수있는 방법을 누군가가 최소한 올바른 방향으로 가르쳐 주길 바랍니다.

미리 감사드립니다.


답변

0

MySQL에는 배열 필드 유형이 없습니다. 당신은 단지에 관계없이 자신들이하고 얼마나 많은 영화의 각 배우에 대해 하나 개의 행이 있도록 해당 증속 링크 테이블이 더 걸릴 수 있습니다

films 
id | FilmName 
1 Star Trek 

actors 
id | FilmId | Name 
1 1  Chris Pine 
2 1  Zachary Quinto 
3 1  Zoe Saldana 

: MySQL을 사용하면 다음과 같은 스타일의 두 테이블을해야 할 것입니다.

CI에서 필름을 삽입하면 각 "액터"행을 삽입 할 수 있습니다. 테이블에 InnoDB와 같은 것을 사용하고있는 경우는, 트랜잭션 (http://ellislab.com/codeigniter/user-guide/database/transactions.html)으로 이것을 실시 할 수도 있습니다.

MongoDB와 같은 NoSQL 기반 데이터베이스를 고려해야합니다 (가능하지 않은 이유는 무엇입니까?).

+0

친절하게 설명 된 답변에 감사드립니다. 내 데이터베이스가 어떻게 구조화 될지 다시 생각해야합니다. 이제 문자열의 모든 액터를 압축하는 foreach 루프가 있습니다. 그러나 나는 당신의 생각이 더 나은 해결책이라고 생각합니다. ;) – user1928075

0

전에 Insert array into MySQL database with PHP으로 읽어야합니다.

조언 : 원하는 것을 만들려면 MongoDB를 검색하십시오.

+0

내 데이터베이스에 배열을 삽입하는 데 문제가 없지만 제대로 작동합니다. 중첩 된 배열에만 문제가 있습니다. 그러나 어쨌든 당신을 감사하십시오. – user1928075

관련 문제