public function materialColumnToField($column, $structure) { // Find first user $user = null; if (dbQuery('user')->first($user)) { } // Create structure for all materials $db_structure = null; if (!dbQuery('structure')->Url('__' . $structure)->cond('Active', 1)->first($db_structure)) { $db_structure = new \samson\activerecord\structure(false); $db_structure->Name = $structure; $db_structure->Url = '__' . $structure; $db_structure->Active = 1; $db_structure->UserID = $user->id; $db_structure->system = 1; $db_structure->save(); } $dbField = null; if (!dbQuery('field')->Name($column)->first($dbField)) { $dbField = new \samson\activerecord\field(false); $dbField->Name = $column; $dbField->Type = 8; $dbField->Active = 1; $dbField->system = 1; $dbField->save(); } // Create structure field relations $db_sf = null; if (!dbQuery('structurefield')->FieldID($dbField->id)->StructureID($db_structure->id)->cond('Active', 1)->first($db_sf)) { $db_sf = new \samson\activerecord\structurefield(false); $db_sf->FieldID = $dbField->id; $db_sf->StructureID = $db_structure->id; $db_sf->Active = 1; $db_sf->save(); } // Iterate all existing materials $db_materials = array(); if (dbQuery('material')->cond('Active', '1')->Draft('0')->exec($db_materials)) { trace('Found materials:' . sizeof($db_materials)); foreach ($db_materials as $db_material) { //trace('Updating material:'.$db_material->id); // If current material has no connection with new structure $db_sm = null; if (!dbQuery('structurematerial')->StructureID($db_structure->id)->MaterialID($db_material->id)->first($db_sm)) { // Create this connection $db_sm = new \samson\activerecord\structurematerial(false); $db_sm->StructureID = $db_structure->id; $db_sm->MaterialID = $db_material->id; $db_sm->Active = 1; $db_sm->save(); //trace('Updating structurematerial:'.$db_material->id); } // If this material has no Content field right now $db_mf = null; if (!dbQuery('materialfield')->MaterialID($db_material->id)->FieldID($dbField->id)->cond('Active', 1)->first($db_mf)) { // Create Content additional field $db_mf = new \samson\activerecord\materialfield(false); $db_mf->MaterialID = $db_material->id; $db_mf->FieldID = $dbField->id; $db_mf->Active = 1; $db_mf->Value = $db_material[$column]; $db_mf->save(); //trace('Updating materialfield:'.$db_material->id); } } } db()->query('ALTER TABLE `material` DROP `' . $column . '`'); }
/** * Create field * @param $name * @param $description * @param $type * @param $value * @return \samson\activerecord\field */ public function createField($name, $description, $type, $value = '') { // Save value of field $field = new \samson\activerecord\field(false); $field->Name = $name; $field->Description = $description; $field->Type = $type; $field->system = 1; $field->Value = $value; $field->Active = 1; $field->save(); return $field; }