tacno/populate-mysql-db

安装: 17

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

类型:数据库

dev-master 2018-01-24 12:31 UTC

This package is not auto-updated.

Last update: 2024-09-24 05:42:43 UTC


README

此类通过随机值填充数据库中的表。在某些情况下,我们需要查看带有数据的程序,此类解决了这个问题。

需求

  • PDO
  • PHP >= 5.3

如何安装

您需要使用composer

{
	"require": {
		"tokenpost/populate-mysql-db": "dev-master"
	}
}

如何使用

类Populate为以下类型生成随机数:

  • int
  • bigint
  • mediumint
  • smallint
  • tinyint
  • float
  • double

并为以下类型生成文本:

  • varchar
  • text

还有一个用于生成Lorem impsum文本的类。

如果您想用固定值代替Lorem ipsum生成器,可以使用方法 ->setFixValue('<fieldname>','<valuename>');

// Example:
....
$populate->setFixValue('image_field','/assets/image/photo.png');

示例

段落由空行分隔。

try {

	$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8",'root','');

	$populate = new Populate();
	$populate->beginWithLoremIpsum(false); // If you want the text beginning with lorem ipsum
	$populate->setPDO($pdo); // set the PDO class

	$populate->setTable('table_test'); // Set the table to populate
	$populate->setFixValue('image_example','/assets/image/photo.jpg'); // if you want a fix value
	echo $populate->insert(100); // The number of inserts
}
catch(Exception $e)
{
	echo $e->getMessage();
}

另一个示例

$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8",'root','');

$populate = new Populate();
$populate->beginWithLoremIpsum(false); // If you want the text beginning with lorem ipsum
$populate->setPDO($pdo); // set the PDO class

$populate->setTable('table_test'); // Set the table to populate
$populate->setFixValue('image_example','/assets/image/photo.jpg'); // if you want a fix value
echo $populate->clean(); // Truncate the table

您可以通过php cli或任务执行。

附加信息

如果您愿意,可以在项目中使用Lorem Ipsum生成器。类Populate\LoremIpsumGenerator使用Singleton作为设计模式。

示例

$lorem = \Populate\LoremIpsumGenerator::getInstance();

// If u want to generate by a number of words
echo $lorem->generateByWords(30);

// If u want to generate by a number of chars
echo $lorem->generateByChars(30);

// If u want to generate by a paragraph number. The second parameter is if you want to separe by html (tag: <p>)
echo $lorem->generateByParagraph(2,true);

// And if u want to generate a random word
echo $lorem->generateRandomWord();