symfony4从数据库生成实体和源数据
sf4目录结构和之前的版本有很大差别,基本MVC结构和Repository。
sf4生成实体和源数据和sf3、2版本的方式差不多。只是bundle在sf4中不在建议管理应用程序。
官方给的建议是临时创建一个bundle,我会试一下。
但是还有别的办法,在symfony issues中有人提出了别的办法
- 数据结构
-- ----------------------------
-- Table structure for comment
-- ----------------------------
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`author_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`content` text,
`published_at` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for post
-- ----------------------------
DROP TABLE IF EXISTS `post`;
CREATE TABLE `post` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`author_id` bigint(20) unsigned NOT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`slug` varchar(255) NOT NULL DEFAULT '',
`summary` varchar(255) NOT NULL DEFAULT '',
`content` text NOT NULL,
`published_at` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for post_tag
-- ----------------------------
DROP TABLE IF EXISTS `post_tag`;
CREATE TABLE `post_tag` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`tag_id` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`full_name` varchar(255) NOT NULL DEFAULT '',
`username` varchar(255) NOT NULL DEFAULT '',
`email` varchar(255) NOT NULL DEFAULT '',
`password` varchar(255) NOT NULL DEFAULT '',
`roles` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 配置orm
框架根目录下的.env文件
DATABASE_URL=mysql://root:root@127.0.0.1:3306/blog
- 按照官方给的办法试一下
创建一个bundle
mkdir src/Acme
mkdir src/Acme/AcmeBlogBundle
vi src/Acme/AcmeBlogBundle/AcmeBlogBundle.php
<?php
namespace App\Acme\AcmeBlogBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeBlogBundle extends Bundle
{
}
vi /home/he/dev/skeleton/config/bundles.php
App\Acme\AcmeBlogBundle\AcmeBlogBundle::class => ['all' => true],
- 生成源数据和实体
php bin/console doctrine:mapping:import --force AcmeBlogBundle yml
php bin/console doctrine:mapping:convert annotation ./src
- 不创建bundle生成实体
php bin/console doctrine:mapping:convert --from-database annotation ./src/Entity
ls -l src/Entity/
-rw-rw-r-- 1 he he 952 3月 31 17:32 Comment.php
-rw-rw-r-- 1 he he 1207 3月 31 17:32 Post.php
-rw-rw-r-- 1 he he 656 3月 31 17:32 PostTag.php
-rw-rw-r-- 1 he he 1062 3月 31 17:32 User.php
但是这种方法只能生成属性,不能生成getter&setter。而且没有命名空间
Comment.php
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Comment
*
* @ORM\Table(name="comment")
* @ORM\Entity
*/
class Comment
{
/**
* @var int
*
* @ORM\Column(name="id", type="bigint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="post_id", type="bigint", nullable=false, options={"unsigned"=true})
*/
private $postId = '0';
/**
* @var int
*
* @ORM\Column(name="author_id", type="bigint", nullable=false, options={"unsigned"=true})
*/
private $authorId = '0';
/**
* @var string|null
*
* @ORM\Column(name="content", type="text", length=65535, nullable=true)
*/
private $content;
/**
* @var int
*
* @ORM\Column(name="published_at", type="integer", nullable=false, options={"unsigned"=true})
*/
private $publishedAt = '0';
}
可以通过IDE生成getter&setter

1219

被折叠的 条评论
为什么被折叠?



