Skip to content

Commit 0a7dabd

Browse files
committed
Correct defaults generated for CURRENT_TIMESTAMP and CURRENT_DATE
1 parent bbc7ec4 commit 0a7dabd

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

Tests/Fixtures/Record/Definition/Order.php

+21-21
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,27 @@ abstract class Order extends \PHPFUI\ORM\Record
3838

3939
/** @var array<string, array<mixed>> */
4040
protected static array $fields = [
41-
// MYSQL_TYPE, PHP_TYPE, LENGTH, NULL, DEFAULT, KEY
42-
'customer_id' => ['integer', 'int', 0, true, null, false, ],
43-
'employee_id' => ['integer', 'int', 0, true, null, false, ],
44-
'notes' => ['longtext', 'string', 4294967295, true, null, false, ],
45-
'order_date' => ['datetime', 'string', 20, true, 'CURRENT_TIMESTAMP', false, ],
46-
'order_id' => ['integer', 'int', 0, false, null, true, ],
47-
'order_status_id' => ['integer', 'int', 0, true, 0, false, ],
48-
'order_tax_status_id' => ['integer', 'int', 0, true, null, false, ],
49-
'paid_date' => ['datetime', 'string', 20, true, null, false, ],
50-
'payment_type' => ['varchar(50)', 'string', 50, true, null, false, ],
51-
'ship_address' => ['longtext', 'string', 4294967295, true, null, false, ],
52-
'ship_city' => ['varchar(50)', 'string', 50, true, null, false, ],
53-
'ship_country_region' => ['varchar(50)', 'string', 50, true, null, false, ],
54-
'ship_name' => ['varchar(50)', 'string', 50, true, null, false, ],
55-
'ship_state_province' => ['varchar(50)', 'string', 50, true, null, false, ],
56-
'ship_zip_postal_code' => ['varchar(50)', 'string', 50, true, null, false, ],
57-
'shipped_date' => ['datetime', 'string', 20, true, null, false, ],
58-
'shipper_id' => ['integer', 'int', 0, true, null, false, ],
59-
'shipping_fee' => ['decimal(19,4)', 'float', 19, true, 0, false, ],
60-
'tax_rate' => ['double', 'float', 0, true, 0, false, ],
61-
'taxes' => ['decimal(19,4)', 'float', 19, true, 0, false, ],
41+
// MYSQL_TYPE, PHP_TYPE, LENGTH, KEY, ALLOWS_NULL, DEFAULT
42+
'customer_id' => ['integer', 'int', 0, false, true, NULL, ],
43+
'employee_id' => ['integer', 'int', 0, false, true, NULL, ],
44+
'notes' => ['longtext', 'string', 4294967295, false, true, 'NULL', ],
45+
'order_date' => ['datetime', 'string', 20, false, false, ],
46+
'order_id' => ['integer', 'int', 0, true, false, ],
47+
'order_status_id' => ['integer', 'int', 0, false, true, 0, ],
48+
'order_tax_status_id' => ['integer', 'int', 0, false, true, NULL, ],
49+
'paid_date' => ['datetime', 'string', 20, false, true, 'NULL', ],
50+
'payment_type' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
51+
'ship_address' => ['longtext', 'string', 4294967295, false, true, 'NULL', ],
52+
'ship_city' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
53+
'ship_country_region' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
54+
'ship_name' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
55+
'ship_state_province' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
56+
'ship_zip_postal_code' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
57+
'shipped_date' => ['datetime', 'string', 20, false, true, 'NULL', ],
58+
'shipper_id' => ['integer', 'int', 0, false, true, NULL, ],
59+
'shipping_fee' => ['decimal(19,4)', 'float', 19, false, true, 0, ],
60+
'tax_rate' => ['double', 'float', 0, false, true, 0, ],
61+
'taxes' => ['decimal(19,4)', 'float', 19, false, true, 0, ],
6262
];
6363

6464
/** @var array<string, true> */

Tests/Fixtures/Record/Definition/OrderDetail.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ abstract class OrderDetail extends \PHPFUI\ORM\Record
2828

2929
/** @var array<string, array<mixed>> */
3030
protected static array $fields = [
31-
// MYSQL_TYPE, PHP_TYPE, LENGTH, NULL, DEFAULT, KEY
32-
'date_allocated' => ['datetime', 'string', 20, true, null, false, ],
33-
'discount' => ['double', 'float', 0, false, 0, false, ],
34-
'inventory_transaction_id' => ['integer', 'int', 0, true, null, false, ],
35-
'order_detail_id' => ['integer', 'int', 0, false, null, true, ],
36-
'order_detail_status_id' => ['integer', 'int', 0, true, null, false, ],
37-
'order_id' => ['integer', 'int', 0, false, null, false, ],
38-
'product_id' => ['integer', 'int', 0, true, null, false, ],
39-
'purchase_order_id' => ['integer', 'int', 0, true, null, false, ],
40-
'quantity' => ['decimal(18,4)', 'float', 18, false, 0, false, ],
41-
'unit_price' => ['decimal(19,4)', 'float', 19, true, 0, false, ],
31+
// MYSQL_TYPE, PHP_TYPE, LENGTH, KEY, ALLOWS_NULL, DEFAULT
32+
'date_allocated' => ['datetime', 'string', 20, false, true, 'NULL', ],
33+
'discount' => ['double', 'float', 0, false, false, 0, ],
34+
'inventory_transaction_id' => ['integer', 'int', 0, false, true, NULL, ],
35+
'order_detail_id' => ['integer', 'int', 0, true, false, ],
36+
'order_detail_status_id' => ['integer', 'int', 0, false, true, NULL, ],
37+
'order_id' => ['integer', 'int', 0, false, false, ],
38+
'product_id' => ['integer', 'int', 0, false, true, NULL, ],
39+
'purchase_order_id' => ['integer', 'int', 0, false, true, NULL, ],
40+
'quantity' => ['decimal(18,4)', 'float', 18, false, false, 0, ],
41+
'unit_price' => ['decimal(19,4)', 'float', 19, false, true, 0, ],
4242
];
4343

4444
/** @var array<string, true> */

Tests/Fixtures/Record/Definition/Product.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ abstract class Product extends \PHPFUI\ORM\Record
2626

2727
/** @var array<string, array<mixed>> */
2828
protected static array $fields = [
29-
// MYSQL_TYPE, PHP_TYPE, LENGTH, NULL, DEFAULT, KEY
30-
'attachments' => ['longblob', 'string', 0, true, null, false, ],
31-
'category' => ['varchar(50)', 'string', 50, true, null, false, ],
32-
'description' => ['longtext', 'string', 4294967295, true, null, false, ],
33-
'discontinued' => ['integer', 'int', 0, false, 0, false, ],
34-
'list_price' => ['decimal(19,4)', 'float', 19, false, 0, false, ],
35-
'minimum_reorder_quantity' => ['integer', 'int', 0, true, null, false, ],
36-
'product_code' => ['varchar(25)', 'string', 25, true, null, false, ],
37-
'product_id' => ['integer', 'int', 0, false, null, true, ],
38-
'product_name' => ['varchar(50)', 'string', 50, true, null, false, ],
39-
'quantity_per_unit' => ['varchar(50)', 'string', 50, true, null, false, ],
40-
'reorder_level' => ['integer', 'int', 0, true, null, false, ],
41-
'standard_cost' => ['decimal(19,4)', 'float', 19, true, 0, false, ],
42-
'target_level' => ['integer', 'int', 0, true, null, false, ],
29+
// MYSQL_TYPE, PHP_TYPE, LENGTH, KEY, ALLOWS_NULL, DEFAULT
30+
'attachments' => ['longblob', 'string', 0, false, true, 'NULL', ],
31+
'category' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
32+
'description' => ['longtext', 'string', 4294967295, false, true, 'NULL', ],
33+
'discontinued' => ['integer', 'int', 0, false, false, 0, ],
34+
'list_price' => ['decimal(19,4)', 'float', 19, false, false, 0, ],
35+
'minimum_reorder_quantity' => ['integer', 'int', 0, false, true, NULL, ],
36+
'product_code' => ['varchar(25)', 'string', 25, false, true, 'NULL', ],
37+
'product_id' => ['integer', 'int', 0, true, false, ],
38+
'product_name' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
39+
'quantity_per_unit' => ['varchar(50)', 'string', 50, false, true, 'NULL', ],
40+
'reorder_level' => ['integer', 'int', 0, false, true, NULL, ],
41+
'standard_cost' => ['decimal(19,4)', 'float', 19, false, true, 0, ],
42+
'target_level' => ['integer', 'int', 0, false, true, NULL, ],
4343
];
4444

4545
/** @var array<string, true> */

docs/1. Setup.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ Use the [\PHPFUI\ORM\Migrator](http://phpfui.com/?n=PHPFUI\ORM&c=Migrator) class
5858
## Generating Models
5959
Once you have a compatible schema, or have modified a schema, you need to generate code. Use the [\PHPFUI\ORM\Tool\Generate\CRUD](http://phpfui.com/?n=PHPFUI%5CORM%5CTool%5CGenerate&c=CRUD) class to generate all or individual models. Check out the scripts folder for an example.
6060

61-
Once Record and Table models are generated, they will not be overwritten by the generation tools. Feel free to add methods by hand for your application needs.
61+
Once **Record** and **Table** models are generated, they will not be overwritten by the generation tools. Feel free to add methods by hand for your application needs.
62+
63+
You should regenerate models when ever you update this library. Generation will only ever overwrite files in the **Definition** namespace, which should not be edited by hand. **Record**, **Table**, and **Validation** namespaces will only be generated if the file is missing.
6264

6365
## Validation
6466
You can also generate initial validators with the [\PHPFUI\ORM\Tool\Generate\Validator](http://phpfui.com/?n=PHPFUI%5CORM%5CTool%5CGenerate&c=Validator) class. Once generated, you should modify by hand for your application.

docs/2. Active Record.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ $customer->update();
2525
```
2626

2727
### Record::__construct
28-
A **Record** can be constructed 4 ways:
29-
- **int** primary key value, will load object values if the primary key value exists
30-
- **string** primary key value, will load object values if the primary key value exists
31-
- **array** object will be initialized to these values, but not read from the database
32-
- **null** (default) constructs an empty object
28+
A **Record** constructor attempts to read the specified row from the table. It can be constructed 4 ways:
29+
- **int** primary key value, will load object values if the primary key value exists.
30+
- **string** primary key value, will load object values if the primary key value exists.
31+
- **array** record is attempted to be read from database using the values of the fields provided.
32+
- **null** (default) constructs an empty object.
33+
34+
Both int and string parameters to the constructor are type checked. Calling the constructor with a parameter can be see as the same as, but with type checking:
35+
```php
36+
$customer = new \App\Record\Customer();
37+
$customer->read($value);
38+
```
3339

3440
### The basic CRUD methods:
3541
- **insert**() or **create**()

src/PHPFUI/ORM/Tool/Generate/CRUD.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,11 @@ protected function getLine(\PHPFUI\ORM\Schema\Field $field) : string
161161
case 'bool':
162162
case 'datetime':
163163
case 'string':
164-
if ('CURRENT_TIMESTAMP' == $field->defaultValue)
164+
if ('CURRENT_TIMESTAMP' == $field->defaultValue || 'CURRENT_DATE' == $field->defaultValue)
165165
{
166-
$defaultValue = '';
167-
$allowNulls = 'true';
166+
$defaultValue = null;
168167
}
169-
170-
if (\str_contains($field->defaultValue ?? '', "'"))
168+
elseif (\str_contains($field->defaultValue ?? '', "'"))
171169
{
172170
$defaultValue = "\"{$field->defaultValue}\"";
173171
}

0 commit comments

Comments
 (0)