Skip to content

Commit a6fb2c3

Browse files
author
Sergey
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # lib/orm/DataBase/AbstractDataBase.php # lib/orm/DataBase/Table.php # lib/orm/Exceptions/ExceptionsMessages.php # lib/orm/Query/MysqlQueryGenerator.php # lib/orm/Query/QueryGeneratorInterface.php
2 parents abddc99 + 27df642 commit a6fb2c3

15 files changed

+144
-131
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# lightweight-php-orm
1+
# lightweight-php-orm ![code style](https://img.shields.io/badge/StyleCI-passed-green.svg)
22

33
Simple ORM based on pattern ActiveRecord.
44

@@ -11,7 +11,7 @@ final class HomeLibrary extends AbstractDataBase
1111
{
1212

1313
public $dbtype = "mysql"; // driver for connection and executing queries
14-
public $dbname = "HomeLibrary"; // database name (dbname and this class are not equal by case of letters)
14+
public $dbname = "homelibrary"; // database name (dbname and this class are not equal by case of letters)
1515
public $user = "root"; // login
1616
public $password = "1111"; // password
1717

@@ -25,14 +25,14 @@ class Book extends Table
2525
// fields of table in database
2626
public $id;
2727
public $book;
28-
// override parent constructor, the last action in constructor of table class
28+
2929
public function __construct()
3030
{
31-
$this->table_name = "books"; // if name of table and this class are not equal, place name of table in this field
31+
$this->table_name = "books"; // if name of table and this class are not equal, place name of table in this field
3232
// describe fields type
3333
$this->id = Field::primaryKey(); // describe PrimaryKey (with auto_increment)
3434
$this->book = Field::varchar(100); // describe varchar field of 100 symbools
35-
$this->initTable(); // call method for initialisation table
35+
$this->initTable(); // call method for initialisation table
3636
}
3737

3838
}
@@ -129,3 +129,17 @@ foreach ($lib as $item) {
129129
Book::findFirst(["book" => "Book_56"])->remove();
130130

131131
```
132+
133+
# update v1.3:
134+
Now it's possible to create tables in database from classes. All you need is describe classes and call migrate method:
135+
```php
136+
// migrate.php
137+
$db = new HomeLibrary();
138+
$book1 = new Book();
139+
$book1->migrate();
140+
$author1 = new Author();
141+
$author1->migrate();
142+
$library = new Library();
143+
$library->migrate();
144+
```
145+
Be careful, when you call migrate for tables which are exists in database, their structure will be overwritten and all data will deleted.

lib/orm/DataBase/DataTypeValidators.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
namespace orm\DataBase;
44

5-
65
use orm\Exceptions\ExceptionsMessages;
76
use orm\Exceptions\OrmRuntimeException;
87

9-
108
/**
11-
* Class DataTypeValidators
12-
* @package orm\DataBase
9+
* Class DataTypeValidators.
1310
*/
1411
class DataTypeValidators
1512
{
16-
1713
/**
1814
* @param $data
1915
* @param $size
@@ -27,6 +23,7 @@ public static function varchar($data, $size)
2723
* @param $instance
2824
* @param $className
2925
* @param $field
26+
*
3027
* @throws OrmRuntimeException
3128
*/
3229
public static function foreignKey($instance, $className, $field)
@@ -48,31 +45,37 @@ public static function text($data, $size)
4845
/**
4946
* @TODO: realize
5047
*/
51-
public static function time() {}
48+
public static function time()
49+
{
50+
}
5251

5352
/**
5453
* @TODO: realize
5554
*/
56-
public static function date() {}
55+
public static function date()
56+
{
57+
}
5758

5859
/**
5960
* @TODO: realize
6061
*/
61-
public static function dateTime() {}
62+
public static function dateTime()
63+
{
64+
}
6265

6366
/**
6467
* @param $data
6568
* @param $size
69+
*
6670
* @throws OrmRuntimeException
6771
*/
6872
private static function throwExceptionIfNotStringOrMoreThanTransmittedLength($data, $size)
6973
{
7074
if (!is_string($data)) {
71-
throw new OrmRuntimeException(ExceptionsMessages::unexpectedTypeOfValue("string", gettype($data)));
75+
throw new OrmRuntimeException(ExceptionsMessages::unexpectedTypeOfValue('string', gettype($data)));
7276
}
7377
if (strlen($data) > $size) {
7478
throw new OrmRuntimeException(ExceptionsMessages::sizeOfValueBiggerThanSizeOfField(strlen($data), $size));
7579
}
7680
}
77-
78-
}
81+
}

lib/orm/DataBase/Field.php

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,152 +2,169 @@
22

33
namespace orm\DataBase;
44

5-
6-
use orm\DataBase\fields\Number;
75
use orm\DataBase\fields\DateTime;
8-
use orm\DataBase\fields\PrimaryKey;
96
use orm\DataBase\fields\ForeignKey;
7+
use orm\DataBase\fields\Number;
8+
use orm\DataBase\fields\PrimaryKey;
109
use orm\DataBase\fields\StringField;
1110

12-
1311
/**
1412
* Class Field
1513
* Contains methods for describing fields and types in database
16-
* for migrations and controlling types and references in database
17-
* @package orm\DataBase
14+
* for migrations and controlling types and references in database.
1815
*/
1916
class Field
2017
{
21-
2218
/**
23-
* @param string $type type of number field (int, float, double)
24-
* @param int $size size of field
25-
* @param string $attribute attribute of field (binary, unsigned)
26-
* @param bool $auto_increment, auto_increment flag
19+
* @param string $type type of number field (int, float, double)
20+
* @param int $size size of field
21+
* @param string $attribute attribute of field (binary, unsigned)
22+
* @param bool $auto_increment, auto_increment flag
23+
*
2724
* @return \orm\DataBase\fields\Number
2825
*/
29-
public static function number($type = "int", $size = 10, $attribute = "", $auto_increment = false)
26+
public static function number($type = 'int', $size = 10, $attribute = '', $auto_increment = false)
3027
{
3128
$obj = new Number();
3229
$obj->type = $type;
3330
$obj->size = $size;
3431
$obj->attribute = $attribute;
3532
$obj->auto_increment = $auto_increment;
33+
3634
return $obj;
3735
}
3836

3937
/**
40-
* describe PRIMARY KEY with AUTO_INCREMENT
38+
* describe PRIMARY KEY with AUTO_INCREMENT.
39+
*
4140
* @return PrimaryKey
4241
*/
4342
public static function primaryKey()
4443
{
4544
$obj = new PrimaryKey();
46-
$obj->type = "int";
45+
$obj->type = 'int';
4746
$obj->size = 10;
4847
$obj->auto_increment = true;
4948
$obj->primary_key = true;
49+
5050
return $obj;
5151
}
5252

5353
/**
5454
* REFERENCES:
5555
* ON DELETE (RESTRICT, CASCADE, NO ACTION, SET NULL)
56-
* ON UPDATE (RESTRICT, CASCADE, NO ACTION, SET NULL)
56+
* ON UPDATE (RESTRICT, CASCADE, NO ACTION, SET NULL).
57+
*
58+
* @param string $table -- name of table for foreign key
59+
* @param string $field -- field in table for foreign key
60+
* @param array $references -- array with settings of references for foreign key.
61+
* If it not sent, will be set from default values (restrict)
5762
*
58-
* @param string $table -- name of table for foreign key
59-
* @param string $field -- field in table for foreign key
60-
* @param array $references -- array with settings of references for foreign key.
61-
* If it not sent, will be set from default values (restrict)
6263
* @return ForeignKey
6364
*/
64-
public static function foreignKey($table, $field, $references = ["on_delete" => "restrict", "on_update" => "restrict"])
65+
public static function foreignKey($table, $field, $references = ['on_delete' => 'restrict', 'on_update' => 'restrict'])
6566
{
6667
$obj = new ForeignKey();
6768
$obj->table = $table;
6869
$obj->field = $field;
69-
$obj->on_delete = $references["on_delete"];
70-
$obj->on_update = $references["on_update"];
70+
$obj->on_delete = $references['on_delete'];
71+
$obj->on_update = $references['on_update'];
72+
7173
return $obj;
7274
}
7375

7476
/**
75-
* String field with type varchar
77+
* String field with type varchar.
78+
*
7679
* @param int $length
80+
*
7781
* @return StringField
7882
*/
7983
public static function varchar($length = 255)
8084
{
81-
return self::stringField("varchar", $length);
85+
return self::stringField('varchar', $length);
8286
}
8387

8488
/**
85-
* String field with type varchar
89+
* String field with type varchar.
90+
*
8691
* @param int $length
92+
*
8793
* @return StringField
8894
*/
8995
public static function text($length = 65535)
9096
{
91-
return self::stringField("text", $length);
97+
return self::stringField('text', $length);
9298
}
9399

94100
/**
95-
* datetime field
101+
* datetime field.
102+
*
96103
* @param string $format
104+
*
97105
* @return DateTime
98106
*/
99-
public static function dateTime($format = "%Y-%M-%d %h:%m:%s")
107+
public static function dateTime($format = '%Y-%M-%d %h:%m:%s')
100108
{
101-
return self::dateTimeUniversal("datetime", $format);
109+
return self::dateTimeUniversal('datetime', $format);
102110
}
103111

104112
/**
105-
* date field
113+
* date field.
114+
*
106115
* @param string $format
116+
*
107117
* @return DateTime
108118
*/
109-
public static function date($format = "%Y-%M-%d")
119+
public static function date($format = '%Y-%M-%d')
110120
{
111-
return self::dateTimeUniversal("date", $format);
121+
return self::dateTimeUniversal('date', $format);
112122
}
113123

114124
/**
115-
* time field
125+
* time field.
126+
*
116127
* @param string $format
128+
*
117129
* @return DateTime
118130
*/
119-
public static function time($format = "%h:%m:%s")
131+
public static function time($format = '%h:%m:%s')
120132
{
121-
return self::dateTimeUniversal("time", $format);
133+
return self::dateTimeUniversal('time', $format);
122134
}
123135

124136
/**
125-
* describe universal field of datetime
137+
* describe universal field of datetime.
138+
*
126139
* @param $type
127140
* @param $format
141+
*
128142
* @return DateTime
129143
*/
130144
private static function dateTimeUniversal($type, $format)
131145
{
132146
$obj = new DateTime();
133147
$obj->type = $type;
134148
$obj->format = $format;
149+
135150
return $obj;
136151
}
137152

138153
/**
139-
* describe universal string field
154+
* describe universal string field.
155+
*
140156
* @param $type
141157
* @param $size
158+
*
142159
* @return StringField
143160
*/
144161
private static function stringField($type, $size)
145162
{
146163
$obj = new StringField();
147164
$obj->type = $type;
148165
$obj->size = $size;
149-
$obj->encoding = "utf8_general_ci";
166+
$obj->encoding = 'utf8_general_ci';
167+
150168
return $obj;
151169
}
152-
153-
}
170+
}

lib/orm/DataBase/fields/DateTime.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace orm\DataBase\fields;
44

5-
65
/**
7-
* Class DateTime
8-
* @package orm\DataBase\fields
6+
* Class DateTime.
97
*/
108
class DateTime
119
{
12-
1310
/**
1411
* @var
1512
*/
@@ -18,5 +15,4 @@ class DateTime
1815
* @var
1916
*/
2017
public $format;
21-
22-
}
18+
}

lib/orm/DataBase/fields/ForeignKey.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace orm\DataBase\fields;
44

5-
65
/**
7-
* Class ForeignKey
8-
* @package orm\DataBase\fields
6+
* Class ForeignKey.
97
*/
108
class ForeignKey
119
{
12-
1310
/**
1411
* @var
1512
*/
@@ -26,5 +23,4 @@ class ForeignKey
2623
* @var
2724
*/
2825
public $on_update;
29-
3026
}

0 commit comments

Comments
 (0)