2010-04-21 2 views
2

1) 모듈을 처음 설치하고 사용할 때 새 데이터베이스 테이블을 채우는 가장 좋은 장소는 어디입니까? 외부 소스에서 데이터를 가져와 사용자 정의 모듈을 설치/사용할 때 투명하게 처리해야합니다.Drupal 스키마를 설명해주십시오. drupal_write_record

{mymodule} _schema()에서 스키마를 작성하고 drupal_install_schema ({tablename})을 수행하십시오. hook_install에서. 그런 다음 drupal_write_record를 사용하여 hook_enable에 테이블을 채 웁니다.

hook_enable이 실행될 때 테이블 생성시 오류가 발생하지 않지만 새 테이블을 쿼리 할 때 아무런 행이 반환되지 않습니다. 이는 비어 있습니다.

이 hook_install
/** 
* Implementation of hook_schema() 
*/ 
function ncbi_subsites_schema() { 
    // we know it's MYSQL, so no need to check 
    $schema['ncbi_subsites_sites'] = array(
     'description' => 'The base table for subsites', 
     'fields' => array(
      'site_id' => array(
       'description' => 'Primary id for site', 
       'type' => 'serial', 
       'unsigned' => TRUE, 
       'not null' => TRUE, 
      ), // end site_id 
      'title' => array(
       'description' => 'The title of the subsite', 
       'type' => 'varchar', 
       'length' => 255, 
       'not null' => TRUE, 
       'default' => '', 
      ), //end title field 
      'url' => array(
       'description' => 'The URL of the subsite in Production', 
       'type' => 'varchar', 
       'length' => 255, 
       'default' => '', 
      ), //end url field 
     ), //end fields 
     'unique keys' => array(
      'site_id'=> array('site_id'), 
      'title' => array('title'), 
     ), //end unique keys 
     'primary_key' => array('site_id'), 
    ); // end schema 

    return $schema; 
} 

은 다음과 같습니다 :

function ncbi_subsites_install() { 
    drupal_install_schema('ncbi_subsites'); 
} 

여기 hook_enable입니다 :

는 여기에 내가 해봤 코드의 하나의 변형이다

function ncbi_subsites_enable() { 
    drupal_get_schema('ncbi_subsites_site'); 

    // my helper function to get data for table (not shown) 
    $subsites = ncbi_subsites_get_subsites(); 
    foreach($subsites as $name=>$attrs) { 
     $record = new stdClass(); 
     $record->title = $name; 
     $record->url = $attrs['homepage']; 
     drupal_write_record('ncbi_subsites_sites', $record); 
    } 
} 

누군가가 난 무엇을 말해 줄 수 있어야 할 곳에 없는?

답변

1

ncbi_subsites_get_subsites()가 .install 파일에 없으면 모듈에있는 파일을 포함시켜야합니다. 그렇지 않으면 아무것도 반환하지 않습니다.이 경우 $ subsites를 덤프하고 종료하십시오.

+0

해당 함수를 포함을 통해 포함합니다. – Aaron

+0

$ 하위 사이트를 덤프하면 어떻게됩니까? 그 기능은 무엇을 하는가? – Kevin

1

나는 drupal_write_record가 설치 또는 활성화 후크를위한 것이 아니라고 생각합니다. 나는 가능하게하거나 설치할 때 SQL을 작성해야한다고 생각한다. 그것이 스키마가 이러한 훅에서 사용 가능하지 않다고 언급 한 일부 게시물을 읽는 것에서 얻은 인상입니다.

1

먼저 Drupal은 모듈에서 정의 된 데이터베이스 스키마를 찾지 못하기 때문에 hook_install()에서 drupal_write_record()을 호출 할 수 없습니다.이 스키마는 여전히 설치되고 활성화됩니다.

대신 db_query() 기능을 사용해야합니다. (코멘트 직렬화 hook_schema()에 prviding하여 기본 데이터를 포함하는 방법을 말하고있는,하지만 난 이것에 어떤 문서를 발견했습니다.) 그러나

, 당신은 드루팔 7 (의 개발 버전)를 사용하는 것 대신 db_insert() 함수를 살펴보아야합니다.

관련 문제