M
M
Maxim2014-01-19 01:50:03
Yii
Maxim, 2014-01-19 01:50:03

How to customize the application for the database structure?

There are game servers whose resources are stored in the database. I want to make an application that can manage these resources through the web. Everything rests on the fact that all game server administrators have different field names in the database and it becomes necessary to configure the application for the finished database.
That is, it may be that the field with the name is called username , it may be that Name , there are a lot of options, so the user needs to configure them himself through some kind of interface.
Would it be correct to use /config/main.php application settings for these purposes like this?

'params'=>array(
  'tbl_profiles'=>array(
    'tbl_name'=>'accounts',
    'tbl_fields'=>array(
      'id'=>array(
        'field'=>'id',
        'label'=>'ID'
      ),
      'username'=>array(
        'field'=>'username',
        'label'=>'Ваше имя'
      ),
      'password'=>array(
        'field'=>'pass',
        'label'=>'Пароль'
      ),
    }
  }
}

And access resources like this:
$model->{Yii::app()->params[tbl_profiles][tbl_fields][username][field]}'

Or is there a better way to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nowm, 2014-01-19
@Frapsy

This is not very convenient through /config/main.php - because then you will need to provide for the situation so that the whole thing is written to the config when the server administrator wants to modify something “through the interface”.
It's easier to create a couple of tables in the database that will store these fields.

CREATE TABLE IF NOT EXISTS `table_table` (
  `table_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  PRIMARY KEY (`table_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

INSERT INTO `table` (`table_id`, `name`) VALUES
(1, 'accounts');

CREATE TABLE IF NOT EXISTS `table_field` (
  `table_field_id` int(11) NOT NULL AUTO_INCREMENT,
  `table_id` int(11) NOT NULL,
  `name` varchar(32) NOT NULL,
  `alias` varchar(32) NOT NULL,
  `label` varchar(64) NOT NULL,
  PRIMARY KEY (`table_field_id`),
  KEY `table_id` (`table_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

ALTER TABLE `table_field`
  ADD CONSTRAINT `table_field_ibfk_1` FOREIGN KEY (`table_id`) 
  REFERENCES `table_table` (`table_id`) ON DELETE CASCADE ON UPDATE CASCADE;

INSERT INTO `table_field` (`table_field_id`, `table_id`, `name`, `alias`, `label`) VALUES
(1, 1, 'id', 'id', 'ID'),
(2, 1, 'username', 'username', 'Ваше имя'),
(3, 1, 'password', 'pass', 'Пароль');

Then you create models for these two tables, controllers, implement C RU D , and you have an interface for setting fields. "C" and "D" I crossed out to show that you only need to leave the ability to read and edit existing entries, and there should not be an opportunity to add / delete them. On the edit page, for example, you need to make sure that only "alias" and "label" are available for editing.
Then you get the data about the fields as usual, you get the data from the model. I wouldn't use them as $model->{field_name_generated here} , but I can't offer anything else just offhand, because I don't see the whole picture.
You can also get data from the config, but at this moment you will lose the ability to configure the composition of the fields through the interface (without additional difficulties with reading / overwriting the PHP file from the PHP file).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question