提交 723ed991 编写于 作者: weixin_47267244's avatar weixin_47267244

新增模型字段赋值,判断字符串长度

上级 f63b049f
<?= '<?php' ?>
<?php echo '<?php'; ?>
namespace <?= $namespace ?>\Base;
namespace <?php echo $namespace; ?>\Base;
use <?= $baseClassName ?> as Model;
use <?php echo $baseClassName; ?> as Model;
use Imi\Model\Annotation\DDL;
use Imi\Model\Annotation\Table;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
/**
* <?= $tableComment ?> 基类
* <?php if($entity === true):?>@Entity<?php else:?>@Entity(false)<?php endif;?>
* <?php echo $tableComment; ?> 基类
* <?php if (true === $entity) { ?>@Entity<?php }
else
{ ?>@Entity(false)<?php }?>
* @Table(name="<?= $table['name'] ?>"<?php if(isset($table['id'][0])):?>, id={<?= '"', implode('", "', $table['id']), '"' ?>}<?php endif;?><?php if($poolName):?>, dbPoolName="<?=$poolName?>"<?php endif;?>)
* @DDL("<?= str_replace('"', '""', $ddl) ?>")
<?php foreach($fields as $field):?>
* @property <?= $field['phpType'] ?> $<?= $field['varName'] ?> <?= '' === $field['comment'] ? '' : $field['comment'] ?>
* @Table(name="<?php echo $table['name']; ?>"<?php if (isset($table['id'][0])) { ?>, id={<?php echo '"', implode('", "', $table['id']), '"'; ?>}<?php }?><?php if ($poolName) { ?>, dbPoolName="<?php echo $poolName; ?>"<?php }?>)
* @DDL("<?php echo str_replace('"', '""', $ddl); ?>")
<?php foreach ($fields as $field) { ?>
* @property <?php echo $field['phpType']; ?> $<?php echo $field['varName']; ?> <?php echo '' === $field['comment'] ? '' : $field['comment']; ?>
<?php endforeach;?>
<?php }?>
*/
abstract class <?= $className ?>Base extends Model
abstract class <?php echo $className; ?>Base extends Model
{
<?php
foreach($fields as $field):
?>
foreach ($fields as $field)
{
?>
/**
<?php if('' === $field['comment']):?>
* <?= $field['name'] ?>
<?php else: ?>
* <?= $field['comment'] ?>
<?php if ('' === $field['comment']) { ?>
* <?php echo $field['name']; ?>
<?php }
else
{ ?>
* <?php echo $field['comment']; ?>
* <?= $field['name'] ?>
<?php endif;?>
* <?php echo $field['name']; ?>
<?php } ?>
* @Column(name="<?= $field['name'] ?>", type="<?= $field['type'] ?>", length=<?= $field['length'] ?>, accuracy=<?= $field['accuracy'] ?>, nullable=<?= json_encode($field['nullable']) ?>, default="<?= $field['default'] ?>", isPrimaryKey=<?= json_encode($field['isPrimaryKey']) ?>, primaryKeyIndex=<?= $field['primaryKeyIndex'] ?>, isAutoIncrement=<?= json_encode($field['isAutoIncrement']) ?>)
* @var <?= $field['phpType'] ?>
* @Column(name="<?php echo $field['name']; ?>", type="<?php echo $field['type']; ?>", length=<?php echo $field['length']; ?>, accuracy=<?php echo $field['accuracy']; ?>, nullable=<?php echo json_encode($field['nullable']); ?>, default="<?php echo $field['default']; ?>", isPrimaryKey=<?php echo json_encode($field['isPrimaryKey']); ?>, primaryKeyIndex=<?php echo $field['primaryKeyIndex']; ?>, isAutoIncrement=<?php echo json_encode($field['isAutoIncrement']); ?>)
* @var <?php echo $field['phpType']; ?>
*/
protected $<?= $field['varName'] ?>;
protected $<?php echo $field['varName']; ?>;
/**
* 获取 <?= $field['varName'] ?><?= '' === $field['comment'] ? '' : (' - ' . $field['comment']) ?>
* 获取 <?php echo $field['varName']; ?><?php echo '' === $field['comment'] ? '' : (' - ' . $field['comment']); ?>
*
* @return <?= $field['phpType'] ?>
* @return <?php echo $field['phpType']; ?>
*/
public function get<?= ucfirst($field['varName']) ?>()
*/
public function get<?php echo ucfirst($field['varName']); ?>()
{
return $this-><?= $field['varName'] ?>;
return $this-><?php echo $field['varName']; ?>;
}
/**
* 赋值 <?= $field['varName'] ?><?= '' === $field['comment'] ? '' : (' - ' . $field['comment']) ?>
* 赋值 <?php echo $field['varName']; ?><?php echo '' === $field['comment'] ? '' : (' - ' . $field['comment']); ?>
* @param <?= $field['phpType'] ?> $<?= $field['varName'] ?> <?= $field['name'] ?>
* @param <?php echo $field['phpType']; ?> $<?php echo $field['varName']; ?> <?php echo $field['name']; ?>
* @return static
*/
public function set<?= ucfirst($field['varName']) ?>($<?= $field['varName'] ?>)
*/
public function set<?php echo ucfirst($field['varName']); ?>($<?php echo $field['varName']; ?>)
{
$this-><?= $field['varName'] ?> = $<?= $field['varName'] ?>;
<?php if ($length = [
'char' => $field['length'],
'varchar' => $field['length'],
'tinyblob' => 2 ^ 8 - 1,
'tinytext' => 2 ^ 8 - 1,
'blob' => 2 ^ 16 - 1,
'text' => 2 ^ 16 - 1,
'mediumblob' => 2 ^ 24 - 1,
'mediumtext' => 2 ^ 24 - 1,
'longblob' => 2 ^ 32 - 1,
'longtext' => 2 ^ 32 - 1,
][$field['type']] ?? null) { ?>
if (isset($<?php echo $field['varName']; ?>[<?php echo $length - 1; ?>]))
{
throw new \InvalidArgumentException('The maximum length of $<?php echo $field['varName']; ?> is <?php echo $length; ?>');
}
<?php } ?>
$this-><?php echo $field['varName']; ?> = $<?php echo $field['varName']; ?>;
return $this;
}
<?php
endforeach;
}
?>
}
......@@ -6,7 +6,7 @@ use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\DDL;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
use Imi\Model\Model;
use Imi\Model\Model as Model;
/**
* tb_article 基类.
......@@ -83,6 +83,10 @@ abstract class ArticleBase extends Model
*/
public function setTitle($title)
{
if (isset($title[254]))
{
throw new \InvalidArgumentException('The maximum length of $title is 255');
}
$this->title = $title;
return $this;
......@@ -116,6 +120,10 @@ abstract class ArticleBase extends Model
*/
public function setContent($content)
{
if (isset($content[20]))
{
throw new \InvalidArgumentException('The maximum length of $content is 21');
}
$this->content = $content;
return $this;
......
......@@ -6,7 +6,7 @@ use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\DDL;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
use Imi\Model\Model;
use Imi\Model\Model as Model;
/**
* tb_member 基类.
......@@ -83,6 +83,10 @@ abstract class MemberBase extends Model
*/
public function setUsername($username)
{
if (isset($username[31]))
{
throw new \InvalidArgumentException('The maximum length of $username is 32');
}
$this->username = $username;
return $this;
......@@ -117,6 +121,10 @@ abstract class MemberBase extends Model
*/
public function setPassword($password)
{
if (isset($password[254]))
{
throw new \InvalidArgumentException('The maximum length of $password is 255');
}
$this->password = $password;
return $this;
......
......@@ -6,7 +6,7 @@ use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\DDL;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
use Imi\Model\Model;
use Imi\Model\Model as Model;
/**
* tb_performance 基类.
......@@ -81,6 +81,10 @@ abstract class PerformanceBase extends Model
*/
public function setValue($value)
{
if (isset($value[254]))
{
throw new \InvalidArgumentException('The maximum length of $value is 255');
}
$this->value = $value;
return $this;
......
......@@ -6,17 +6,17 @@ use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\DDL;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
use Imi\Model\Model;
use Imi\Model\Model as Model;
/**
* tb_test_json 基类.
*
* @Entity
* @Table(name="tb_test_json", id={"id"})
* @DDL("CREATE TABLE `tb_test_json` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `json_data` text NOT NULL COMMENT 'json数据', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8")
* @DDL("CREATE TABLE `tb_test_json` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `json_data` json NOT NULL COMMENT 'json数据', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8")
*
* @property int $id
* @property string $jsonData json数据
* @property int $id
* @property \Imi\Util\LazyArrayObject $jsonData json数据
*/
abstract class TestJsonBase extends Model
{
......@@ -57,16 +57,16 @@ abstract class TestJsonBase extends Model
* json数据
* json_data.
*
* @Column(name="json_data", type="text", length=0, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
* @Column(name="json_data", type="json", length=0, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
*
* @var string
* @var \Imi\Util\LazyArrayObject
*/
protected $jsonData;
/**
* 获取 jsonData - json数据.
*
* @return string
* @return \Imi\Util\LazyArrayObject
*/
public function getJsonData()
{
......@@ -76,7 +76,7 @@ abstract class TestJsonBase extends Model
/**
* 赋值 jsonData - json数据.
*
* @param string $jsonData json_data
* @param \Imi\Util\LazyArrayObject $jsonData json_data
*
* @return static
*/
......
......@@ -6,7 +6,7 @@ use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\DDL;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
use Imi\Model\Model;
use Imi\Model\Model as Model;
/**
* tb_test_list 基类.
......@@ -81,6 +81,10 @@ abstract class TestListBase extends Model
*/
public function setList($list)
{
if (isset($list[254]))
{
throw new \InvalidArgumentException('The maximum length of $list is 255');
}
$this->list = $list;
return $this;
......
......@@ -82,6 +82,10 @@ abstract class TestSoftDeleteBase extends Model
*/
public function setTitle($title)
{
if (isset($title[254]))
{
throw new \InvalidArgumentException('The maximum length of $title is 255');
}
$this->title = $title;
return $this;
......
......@@ -6,7 +6,7 @@ use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\DDL;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
use Imi\Model\Model;
use Imi\Model\Model as Model;
/**
* tb_tree 基类.
......@@ -115,6 +115,10 @@ abstract class TreeBase extends Model
*/
public function setName($name)
{
if (isset($name[31]))
{
throw new \InvalidArgumentException('The maximum length of $name is 32');
}
$this->name = $name;
return $this;
......
......@@ -6,7 +6,7 @@ use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\DDL;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
use Imi\Model\Model;
use Imi\Model\Model as Model;
/**
* tb_update_time 基类.
......
......@@ -20,7 +20,35 @@ class TestList extends TestListBase
*
* @Column(name="list", type="list", listSeparator=",", length=255, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false)
*
* @var string
* @var array
*/
protected $list;
/**
* 获取 list.
*
* @phpstan-ignore-next-line
*
* @return array
*/
public function getList()
{
return $this->list;
}
/**
* 赋值 list.
*
* @phpstan-ignore-next-line
*
* @param array $list list
*
* @return static
*/
public function setList($list)
{
$this->list = $list;
return $this;
}
}
......@@ -447,7 +447,6 @@ class ModelTest extends BaseTest
'id' => null,
'jsonData' => [1, 2, 3],
], $record->convertToArray());
// @phpstan-ignore-next-line
$this->assertEquals([1, 2, 3], $record->getJsonData()->toArray());
$id = $record->insert()->getLastInsertId();
$this->assertGreaterThan(0, $id);
......@@ -456,7 +455,6 @@ class ModelTest extends BaseTest
'id' => $id,
'jsonData' => [1, 2, 3],
], $record->convertToArray());
// @phpstan-ignore-next-line
$this->assertEquals([1, 2, 3], $record->getJsonData()->toArray());
$list = TestJson::query()->where('id', '=', $id)->select()->getArray();
$this->assertEquals([[
......@@ -471,7 +469,6 @@ class ModelTest extends BaseTest
'id' => null,
'json_data' => [4, 5, 6],
], $record->convertToArray());
// @phpstan-ignore-next-line
$this->assertEquals([4, 5, 6], $record->getJsonData()->toArray());
$id = $record->insert()->getLastInsertId();
$this->assertGreaterThan(0, $id);
......@@ -480,7 +477,6 @@ class ModelTest extends BaseTest
'id' => $id,
'json_data' => [4, 5, 6],
], $record->convertToArray());
// @phpstan-ignore-next-line
$this->assertEquals([4, 5, 6], $record->getJsonData()->toArray());
$list = TestJsonNotCamel::query()->where('id', '=', $id)->select()->getArray();
$this->assertEquals([[
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册