Skip to content

Instantly share code, notes, and snippets.

@snetty
Created May 21, 2013 12:09
Show Gist options
  • Select an option

  • Save snetty/5619349 to your computer and use it in GitHub Desktop.

Select an option

Save snetty/5619349 to your computer and use it in GitHub Desktop.
Import form fields and columns from one model to another
<?php
class Extended_ActiveRecord extends Db_ActiveRecord{
public function import_column_definitions(Db_ActiveRecord $model, $context=null){
My_Helper::import_column_definitions($model, $this, $context);
}
public function import_form_field_definitions(Db_ActiveRecord $model, $context=null, $force_tab=null){
My_Helper::import_form_field_definitions($model, $this, $context, $force_tab);
}
}
<?php
class My_Helper{
public static function import_column_definitions(Db_ActiveRecord $source, Db_ActiveRecord $destination, $context=null){
$columns = $source->get_column_definitions($context);
foreach($columns as $column){
$tmp_column = $destination->define_column($column->dbName, $column->displayName);
$column_properties = get_object_vars($column);
foreach($column_properties as $key => $value){
$tmp_column->$key = $value;
}
}
}
public static function import_form_field_definitions(Db_ActiveRecord $source, Db_ActiveRecord $destination, $context=null, $force_tab=null){
$source->init_columns_info($context)->define_form_fields($context);
$form_elements = $source->form_elements;
foreach($form_elements as $form_element){
$tmp_field = $destination->add_form_field($form_element->dbName);
$form_properties = get_object_vars($form_element);
foreach($form_properties as $key => $value){
$tmp_field->$key = $value;
}
if(strlen($force_tab) != 0){
$tmp_field->tab($force_tab);
}
$destination->{$form_element->dbName} = $source->{$form_element->dbName};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment