Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ActiveTable
- {
- public $name = 'noname';
- public function __construct($tableName)
- {
- $this->name = $tableName;
- }
- }
- class MemberTable extends ActiveTable
- {
- }
- class ActiveRecord
- {
- public function __construct()
- {
- echo "<pre>"; print_r($this); echo "</pre>";
- }
- public function insertIntoTable(ActiveTable $table)
- {
- $fields = array_map(
- function($element){
- if (is_string($element))
- return "'$element'";
- if (is_int($element))
- return $element;
- if (is_null($element))
- return "NULL";
- if (is_float($element))
- return "'$element'";
- },
- $this->getAsArray()
- );
- $r = 'INSERT INTO '.$table->name.' ('.implode(', ', array_keys($fields)).') VALUES ('.implode(', ', $fields).')';
- echo "<pre>"; print_r($r); echo "</pre>";
- }
- public static function createFromArray($fields = array())
- {
- $instance = new static();
- foreach ($fields as $fieldName => $fieldValue) {
- $instance->$fieldName = $fieldValue;
- }
- return $instance;
- }
- public function getAsArray()
- {
- $recordValues = array();
- foreach ($this as $fieldName => $fieldValue) {
- $recordValues[$fieldName] = $fieldValue;
- }
- return $recordValues;
- }
- }
- class Member extends ActiveRecord
- {
- public $memberID = null;
- public $groupID = null;
- public $username = '';
- public function __construct()
- {
- parent::__construct();
- }
- }
- $table = new ActiveTable('prefix__members_lang_pl');
- $ar = new ActiveRecord();
- $ar->userID = 5;
- $ar->insertIntoTable($table);
- $ar = ActiveRecord::createFromArray(array(
- 'username' => 'root',
- 'password' => 'd033e22ae348aeb5660fc2140aec35850c4da997',
- ));
- $ar->insertIntoTable($table);
- $mem = new Member();
- $mem->insertIntoTable($table);
- $mem = new Member();
- unset($mem->groupID);
- $mem->insertIntoTable($table);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement