提交 3ec72a08 编写于 作者: weixin_47267244's avatar weixin_47267244

模型检查字符串字段长度改为可选项

上级 682a336f
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
`-basePath` 指定命名空间对应的基准路径,可选 `-basePath` 指定命名空间对应的基准路径,可选
`-entity` 序列化时是否使用驼峰命名(`true` or `false`),默认`true`,可选 `-entity` 序列化时是否使用驼峰命名(`true` or `false`),默认`true`,可选
`-sqlSingleLine` 生成的SQL为单行,默认`false`,可选 `-sqlSingleLine` 生成的SQL为单行,默认`false`,可选
`-lengthCheck` 是否检查字符串字段长度,可选
示例: 示例:
......
...@@ -37,6 +37,7 @@ class ModelGenerate ...@@ -37,6 +37,7 @@ class ModelGenerate
* @Arg(name="basePath", type=ArgType::STRING, default=null, comments="指定命名空间对应的基准路径,可选") * @Arg(name="basePath", type=ArgType::STRING, default=null, comments="指定命名空间对应的基准路径,可选")
* @Arg(name="entity", type=ArgType::BOOLEAN, default=true, comments="序列化时是否使用驼峰命名(true or false),默认true,可选") * @Arg(name="entity", type=ArgType::BOOLEAN, default=true, comments="序列化时是否使用驼峰命名(true or false),默认true,可选")
* @Arg(name="sqlSingleLine", type=ArgType::BOOLEAN, default=false, comments="生成的SQL为单行,默认false,可选") * @Arg(name="sqlSingleLine", type=ArgType::BOOLEAN, default=false, comments="生成的SQL为单行,默认false,可选")
* @Arg(name="lengthCheck", type=ArgType::BOOLEAN, default=false, comments="是否检查字符串字段长度,可选")
* *
* @param string $namespace * @param string $namespace
* @param string $baseClass * @param string $baseClass
...@@ -50,10 +51,11 @@ class ModelGenerate ...@@ -50,10 +51,11 @@ class ModelGenerate
* @param string|null $basePath * @param string|null $basePath
* @param bool $entity * @param bool $entity
* @param bool $sqlSingleLine * @param bool $sqlSingleLine
* @param bool $lengthCheck
* *
* @return void * @return void
*/ */
public function generate($namespace, $baseClass, $database, $poolName, $prefix, $include, $exclude, $override, $config, $basePath, $entity, $sqlSingleLine) public function generate($namespace, $baseClass, $database, $poolName, $prefix, $include, $exclude, $override, $config, $basePath, $entity, $sqlSingleLine, $lengthCheck)
{ {
$override = (string) $override; $override = (string) $override;
switch ($override) switch ($override)
...@@ -220,6 +222,7 @@ class ModelGenerate ...@@ -220,6 +222,7 @@ class ModelGenerate
'poolName' => $poolName, 'poolName' => $poolName,
'ddl' => $ddl, 'ddl' => $ddl,
'tableComment' => '' === $item['TABLE_COMMENT'] ? $table : $item['TABLE_COMMENT'], 'tableComment' => '' === $item['TABLE_COMMENT'] ? $table : $item['TABLE_COMMENT'],
'lengthCheck' => $lengthCheck,
]; ];
$fields = $query->execute(sprintf('show full columns from `%s`.`%s`', $database, $table))->getArray(); $fields = $query->execute(sprintf('show full columns from `%s`.`%s`', $database, $table))->getArray();
$this->parseFields($fields, $data, 'VIEW' === $item['TABLE_TYPE']); $this->parseFields($fields, $data, 'VIEW' === $item['TABLE_TYPE']);
......
...@@ -65,7 +65,7 @@ abstract class <?php echo $className; ?>Base extends Model ...@@ -65,7 +65,7 @@ abstract class <?php echo $className; ?>Base extends Model
*/ */
public function set<?php echo ucfirst($field['varName']); ?>($<?php echo $field['varName']; ?>) public function set<?php echo ucfirst($field['varName']); ?>($<?php echo $field['varName']; ?>)
{ {
<?php if ($length = [ <?php if ($lengthCheck && $length = [
'char' => $field['length'], 'char' => $field['length'],
'varchar' => $field['length'], 'varchar' => $field['length'],
'tinyblob' => 2 ** 8 - 1, 'tinyblob' => 2 ** 8 - 1,
...@@ -77,7 +77,7 @@ abstract class <?php echo $className; ?>Base extends Model ...@@ -77,7 +77,7 @@ abstract class <?php echo $className; ?>Base extends Model
'longblob' => 2 ** 32 - 1, 'longblob' => 2 ** 32 - 1,
'longtext' => 2 ** 32 - 1, 'longtext' => 2 ** 32 - 1,
][$field['type']] ?? null) { ?> ][$field['type']] ?? null) { ?>
if (isset($<?php echo $field['varName']; ?>[<?php echo $length; ?>])) if (mb_strlen($<?php echo $field['varName']; ?>) > <?php echo $length; ?>)
{ {
throw new \InvalidArgumentException('The maximum length of $<?php echo $field['varName']; ?> is <?php echo $length; ?>'); throw new \InvalidArgumentException('The maximum length of $<?php echo $field['varName']; ?> is <?php echo $length; ?>');
} }
......
...@@ -83,7 +83,7 @@ abstract class ArticleBase extends Model ...@@ -83,7 +83,7 @@ abstract class ArticleBase extends Model
*/ */
public function setTitle($title) public function setTitle($title)
{ {
if (isset($title[255])) if (mb_strlen($title) > 255)
{ {
throw new \InvalidArgumentException('The maximum length of $title is 255'); throw new \InvalidArgumentException('The maximum length of $title is 255');
} }
...@@ -120,7 +120,7 @@ abstract class ArticleBase extends Model ...@@ -120,7 +120,7 @@ abstract class ArticleBase extends Model
*/ */
public function setContent($content) public function setContent($content)
{ {
if (isset($content[16777215])) if (mb_strlen($content) > 16777215)
{ {
throw new \InvalidArgumentException('The maximum length of $content is 16777215'); throw new \InvalidArgumentException('The maximum length of $content is 16777215');
} }
......
...@@ -83,7 +83,7 @@ abstract class MemberBase extends Model ...@@ -83,7 +83,7 @@ abstract class MemberBase extends Model
*/ */
public function setUsername($username) public function setUsername($username)
{ {
if (isset($username[32])) if (mb_strlen($username) > 32)
{ {
throw new \InvalidArgumentException('The maximum length of $username is 32'); throw new \InvalidArgumentException('The maximum length of $username is 32');
} }
...@@ -121,7 +121,7 @@ abstract class MemberBase extends Model ...@@ -121,7 +121,7 @@ abstract class MemberBase extends Model
*/ */
public function setPassword($password) public function setPassword($password)
{ {
if (isset($password[255])) if (mb_strlen($password) > 255)
{ {
throw new \InvalidArgumentException('The maximum length of $password is 255'); throw new \InvalidArgumentException('The maximum length of $password is 255');
} }
......
...@@ -81,7 +81,7 @@ abstract class PerformanceBase extends Model ...@@ -81,7 +81,7 @@ abstract class PerformanceBase extends Model
*/ */
public function setValue($value) public function setValue($value)
{ {
if (isset($value[255])) if (mb_strlen($value) > 255)
{ {
throw new \InvalidArgumentException('The maximum length of $value is 255'); throw new \InvalidArgumentException('The maximum length of $value is 255');
} }
......
...@@ -81,7 +81,7 @@ abstract class TestListBase extends Model ...@@ -81,7 +81,7 @@ abstract class TestListBase extends Model
*/ */
public function setList($list) public function setList($list)
{ {
if (isset($list[255])) if (mb_strlen($list) > 255)
{ {
throw new \InvalidArgumentException('The maximum length of $list is 255'); throw new \InvalidArgumentException('The maximum length of $list is 255');
} }
......
...@@ -82,7 +82,7 @@ abstract class TestSoftDeleteBase extends Model ...@@ -82,7 +82,7 @@ abstract class TestSoftDeleteBase extends Model
*/ */
public function setTitle($title) public function setTitle($title)
{ {
if (isset($title[255])) if (mb_strlen($title) > 255)
{ {
throw new \InvalidArgumentException('The maximum length of $title is 255'); throw new \InvalidArgumentException('The maximum length of $title is 255');
} }
......
...@@ -115,7 +115,7 @@ abstract class TreeBase extends Model ...@@ -115,7 +115,7 @@ abstract class TreeBase extends Model
*/ */
public function setName($name) public function setName($name)
{ {
if (isset($name[32])) if (mb_strlen($name) > 32)
{ {
throw new \InvalidArgumentException('The maximum length of $name is 32'); throw new \InvalidArgumentException('The maximum length of $name is 32');
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册