Symfony学习十分钟入门经典教程(3)

这个操作,已经帮助你通过Article.php建立了数据库和数据表,你不需要自己操作这个过程,下面我们还会对Article.php进行改造,而到时候只需要重新 执行上面的这个操作,Symfony会帮助你自动修改数据库的表结构。

添加约束

上面我们创建了Article.php,既然这个实体代表了具体的业务逻辑,因此我们要考虑几个现实的问题:

1. 用户必须填写标题和内容
2. 用户填写的标题不能超过200个字
3. 用户可以不填写作者

这些就属于业务逻辑,而我们可以修改Article.php如下,以增加相应的业务逻辑的约束:

namespace Symfony\Bundle\SampleBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * Article * * @ORM\Table() * @ORM\Entity(repositoryClass="Symfony\Bundle\SampleBundle\Entity\ArticleRepository") */ class Article { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * @Assert\NotBlank(message="标题不可为空") * @Assert\Length( * max=200, * maxMessage="标题不能超过200个字" * ) * @ORM\Column(name="title", type="string", length=200) */ private $title; /** * @var string * * @Assert\NotBlank(message="文章内容不可为空") * @ORM\Column(name="content", type="text") */ private $content; /** * @var string * * @ORM\Column(name="author", type="string", length=20,nullable=true) */ private $author; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set title * * @param string $title * @return Article */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } /** * Set content * * @param string $content * @return Article */ public function setContent($content) { $this->content = $content; return $this; } /** * Get content * * @return string */ public function getContent() { return $this->content; } /** * Set author * * @param string $author * @return Article */ public function setAuthor($author) { $this->author = $author; return $this; } /** * Get author * * @return string */ public function getAuthor() { return $this->author; } }

然后执行同步数据库的操作:

$ php app/console doctrine:schema:update --force Updating database schema... Database schema updated successfully! "1" queries were executed

增删改查

好了,我们来做一个针对文章的增删改查操作。首先请执行下面的命令:

$ php app/console generate:doctrine:crud Welcome to the Doctrine2 CRUD generator This command helps you generate CRUD controllers and templates. First, you need to give the entity for which you want to generate a CRUD. You can give an entity that does not exist yet and the wizard will help you defining it. You must use the shortcut notation like AcmeBlogBundle:Post. The Entity shortcut name: SymfonySampleBundle:Article By default, the generator creates two actions: list and show. You can also ask it to generate "write" actions: new, update, and delete. Do you want to generate the "write" actions [no]? yes Determine the format to use for the generated CRUD. Configuration format (yml, xml, php, or annotation) [annotation]: yml Determine the routes prefix (all the routes will be "mounted" under this prefix: /prefix/, /prefix/new, ...). Routes prefix [/article]: /article Summary before generation You are going to generate a CRUD controller for "SymfonySampleBundle:Article" using the "yml" format. Do you confirm generation [yes]? yes CRUD generation Generating the CRUD code: OK Generating the Form code: OK You can now start using the generated code!

然后请编辑DefaultController.php中的indexAction如下:

/** * @Route("https://www.jb51.net/",name="welcome") * @Template() */ public function indexAction() { return array(); }

编辑Resource/views/Default/index.html.twig内容如下:

<a href="{{path('article')}}">文章管理</a>

让我们看看神奇的事情,启动内置的测试服务器:

$php app/console server:run

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c8c8c1db8c5709d5859a400347b9b320.html