Skip to content

Commit a88c937

Browse files
committed
fix support size properties for non-integer primary keys
1 parent da410dc commit a88c937

14 files changed

+25
-14
lines changed

src/lib/AttributeResolver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ protected function resolveProperty($propertyName, SpecObjectInterface $property,
204204
$phpType = SchemaTypeResolver::schemaToPhpType($foreignPkProperty);
205205
$attribute->setPhpType($phpType)
206206
->setDbType($this->guessDbType($foreignPkProperty, true, true));
207+
$attribute->setSize($foreignPkProperty->maxLength ?? null);
208+
[$min, $max] = $this->guessMinMax($foreignPkProperty);
209+
$attribute->setLimits($min, $max, $foreignPkProperty->minLength ?? null);
207210

208211
$relation = (new AttributeRelation($propertyName, $relatedTableName, $relatedClassName))
209212
->asHasOne([$foreignPk => $attribute->columnName])->asSelfReference();
@@ -227,7 +230,9 @@ protected function resolveProperty($propertyName, SpecObjectInterface $property,
227230
$phpType = SchemaTypeResolver::schemaToPhpType($foreignPkProperty);
228231
$attribute->setPhpType($phpType)
229232
->setDbType($this->guessDbType($foreignPkProperty, true, true));
230-
233+
$attribute->setSize($foreignPkProperty->maxLength ?? null);
234+
[$min, $max] = $this->guessMinMax($foreignPkProperty);
235+
$attribute->setLimits($min, $max, $foreignPkProperty->minLength ?? null);
231236
$relation = (new AttributeRelation($propertyName, $relatedTableName, $relatedClassName))
232237
->asHasOne([$foreignPk => $attribute->columnName]);
233238
$this->relations[$propertyName] = $relation;

src/lib/FakerStubResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ private function fakeForString():?string
8181
&& $this->attribute->size < 10) {
8282
return '$faker->title';
8383
}
84+
if ($this->attribute->primary || $this->attribute->isReference()) {
85+
$size = $this->attribute->size ?? 255;
86+
return 'substr($uniqueFaker->sha256, 0, ' . $size . ')';
87+
}
8488

8589
$patterns = [
8690
'~_id$~' => '$uniqueFaker->numberBetween(0, 1000000)',

tests/fixtures/blog.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
'description' => 'A blog post (uid used as pk for test purposes)',
6262
'attributes' => [
6363
'uid' => (new Attribute('uid', ['phpType' => 'string', 'dbType' => 'string']))
64-
->setReadOnly()->setRequired()->setIsPrimary()->setSize(255)->setFakerStub('substr($faker->text(255), 0, 255)'),
64+
->setReadOnly()->setRequired()->setIsPrimary()->setSize(128)
65+
->setFakerStub('substr($uniqueFaker->sha256, 0, 128)'),
6566
'title' => (new Attribute('title', ['phpType' => 'string', 'dbType' => 'string']))
6667
->setRequired()->setSize(255)->setFakerStub('substr($faker->sentence, 0, 255)'),
6768
'slug' => (new Attribute('slug', ['phpType' => 'string', 'dbType' => 'string']))
@@ -106,9 +107,10 @@
106107
->setFakerStub('$uniqueFaker->numberBetween(0, 2147483647)'),
107108
'post' => (new Attribute('post', ['phpType' => 'string', 'dbType' => 'string']))
108109
->setRequired()
110+
->setSize(128)
109111
->asReference('Post')
110112
->setDescription('A blog post (uid used as pk for test purposes)')
111-
->setFakerStub('$uniqueFaker->numberBetween(0, 1000000)'),
113+
->setFakerStub('substr($uniqueFaker->sha256, 0, 128)'),
112114
'author' => (new Attribute('author', ['phpType' => 'int', 'dbType' => 'integer']))
113115
->setRequired()
114116
->asReference('User')

tests/specs/blog.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ components:
128128
properties:
129129
uid:
130130
type: string
131-
maxLength: 255
131+
maxLength: 128
132132
x-db-type: varchar
133133
readOnly: True
134134
title:

tests/specs/blog/migrations/m200000_000002_create_table_blog_posts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class m200000_000002_create_table_blog_posts extends \yii\db\Migration
88
public function up()
99
{
1010
$this->createTable('{{%blog_posts}}', [
11-
'uid' => $this->string(255)->notNull(),
11+
'uid' => $this->string(128)->notNull(),
1212
'title' => $this->string(255)->notNull(),
1313
'slug' => $this->string(200)->null()->defaultValue(null),
1414
'category_id' => $this->integer()->notNull(),

tests/specs/blog/migrations/m200000_000004_create_table_post_comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function up()
99
{
1010
$this->createTable('{{%post_comments}}', [
1111
'id' => $this->bigPrimaryKey(),
12-
'post_id' => $this->string()->notNull(),
12+
'post_id' => $this->string(128)->notNull(),
1313
'author_id' => $this->integer()->notNull(),
1414
'message' => 'json NOT NULL DEFAULT \'[]\'',
1515
'meta_data' => 'json NOT NULL DEFAULT \'[]\'',

tests/specs/blog/migrations_maria_db/m200000_000002_create_table_blog_posts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class m200000_000002_create_table_blog_posts extends \yii\db\Migration
88
public function up()
99
{
1010
$this->createTable('{{%blog_posts}}', [
11-
'uid' => $this->string(255)->notNull(),
11+
'uid' => $this->string(128)->notNull(),
1212
'title' => $this->string(255)->notNull(),
1313
'slug' => $this->string(200)->null()->defaultValue(null),
1414
'category_id' => $this->integer()->notNull(),

tests/specs/blog/migrations_maria_db/m200000_000004_create_table_post_comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function up()
99
{
1010
$this->createTable('{{%post_comments}}', [
1111
'id' => $this->bigPrimaryKey(),
12-
'post_id' => $this->string()->notNull(),
12+
'post_id' => $this->string(128)->notNull(),
1313
'author_id' => $this->integer()->notNull(),
1414
'message' => 'json NOT NULL DEFAULT \'[]\'',
1515
'meta_data' => 'json NOT NULL DEFAULT \'[]\'',

tests/specs/blog/migrations_mysql_db/m200000_000002_create_table_blog_posts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class m200000_000002_create_table_blog_posts extends \yii\db\Migration
88
public function up()
99
{
1010
$this->createTable('{{%blog_posts}}', [
11-
'uid' => $this->string(255)->notNull(),
11+
'uid' => $this->string(128)->notNull(),
1212
'title' => $this->string(255)->notNull(),
1313
'slug' => $this->string(200)->null()->defaultValue(null),
1414
'category_id' => $this->integer()->notNull(),

tests/specs/blog/migrations_mysql_db/m200000_000004_create_table_post_comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function up()
99
{
1010
$this->createTable('{{%post_comments}}', [
1111
'id' => $this->bigPrimaryKey(),
12-
'post_id' => $this->string()->notNull(),
12+
'post_id' => $this->string(128)->notNull(),
1313
'author_id' => $this->integer()->notNull(),
1414
'message' => 'json NOT NULL',
1515
'meta_data' => 'json NOT NULL',

0 commit comments

Comments
 (0)