drupal - drupal_write_records() doesn’t work for those fields which was created by hook_update_n() -
currently have created table using install schema in drupal 7. after table alter add new column using hook_update_n().
now drupal_write_records() doesn’t work fields created hook_update_n().
regards, raj.
hook_schema or hook_update_n?
drupal provides powerful , structured way of upgrading sites , making ddic changes. 2 important hooks hook_schema() provides structured, db independent way of defining table schema. when new module installed, hook_schema() automatically invoked (yes that's case in drupal 7, while in drupal 6 had explicitly used create tables) , tables created.
now tables created , module being used, decide change in table, let's decided add new field. how change reach system , upgrade seamlessly module / code depending on field not break.
drupal provides hook called hook_update_n() n numerical value indicating version of schema e.g. mymodule_update_1() first change , mymodule_update_2() second change , on. coming our example, suppose need add new field, you'll need implement hook follows
/** * implements hook_update_n(). */ function mymodule_update_1() { db_add_field('mytable', 'myfield', array( 'description' => 'my status field', 'type' => 'int', 'not null' => false, 'unsigned' => false, )); }
once done, update has run using either drush updb or via url /update.php. once update run, drupal recognizes there update in schema since number 1 (from above example) never run. runs function , tracks version of schema 1. next time make change , need update table schema can use number higher previous maximum number , drupal apply change.
now change in hook_update_n() sufficient? answer no, need update hook_schema() appropriate table. 2 reasons 1. when run query after this, drupal not find new field in table schema definition , not write field e.g. when using drupal_write_record(). 2. when module installed in new site never had module before, drupal run hook_schema() , not run other updates expects table schema up-to-date , stores schema version highest hook_update_n() number. changed after applied when update run.
conclusion
appropriate table schema must updated in hook_schemea(). next perform database update using hook_update_n(), n > pervious max update number
Comments
Post a Comment