Created
September 11, 2017 02:39
-
-
Save LeoOnTheEarth/107181ec02bd847a851462dd8654fda1 to your computer and use it in GitHub Desktop.
Symfony3 + Doctrine Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// AppBundle/Entity/Article.php | |
namespace AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use AppBundle\Entity\Author; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="articles") | |
*/ | |
class Article | |
{ | |
/** | |
* @ORM\Column(type="integer", name="id") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="string", name="title", length=100, nullable=false) | |
*/ | |
protected $title; | |
/** | |
* @ORM\Column(type="text", name="content") | |
*/ | |
protected $content; | |
/** | |
* @var Author | |
* | |
* @ORM\ManyToOne(targetEntity="Author") | |
* @ORM\JoinColumn(name="author_id", referencedColumnName="id") | |
*/ | |
protected $author; | |
/** | |
* @return mixed | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getTitle() | |
{ | |
return $this->title; | |
} | |
/** | |
* @param mixed $title | |
* @return Article | |
*/ | |
public function setTitle($title) | |
{ | |
$this->title = $title; | |
return $this; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getContent() | |
{ | |
return $this->content; | |
} | |
/** | |
* @param mixed $content | |
* @return Article | |
*/ | |
public function setContent($content) | |
{ | |
$this->content = $content; | |
return $this; | |
} | |
/** | |
* @return Author | |
*/ | |
public function getAuthor() | |
{ | |
return $this->author; | |
} | |
/** | |
* @param Author $author | |
* @return Article | |
*/ | |
public function setAuthor(Author $author) | |
{ | |
$this->author = $author; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// AppBundle/Controller/DefaultController.php | |
namespace AppBundle\Controller; | |
use AppBundle\Entity\Article; | |
use AppBundle\Entity\Author; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
class DefaultController extends Controller | |
{ | |
/** | |
* 編輯文章和作者 | |
* | |
* @Route("/edit/{id}", name="edit", requirements={"id"="\d+"}, defaults={"id"=0}) | |
* | |
* @param Request $request | |
* @param int $id | |
* | |
* @return \Symfony\Component\HttpFoundation\Response | |
*/ | |
public function editAction(Request $request, $id = 0) | |
{ | |
// 取得 Entity Manager 物件 | |
$em = $this->getDoctrine()->getEntityManager(); | |
if ($id > 0) { | |
$article = $em->getRepository(Article::class)->find($id); | |
} | |
// 處理 Form 表單送出的資料 | |
if ($request->isMethod('POST')) { | |
$articleData = $request->request->get('article', []); | |
$authorData = $request->request->get('author', []); | |
// TODO: 要做一些資料驗證 | |
// 資料驗證這步驟先跳過,先假設帶入的資料都正確 | |
if (empty($article)) { | |
// 新增資料的情形時,新增 Author 和 Article 物件 | |
$author = new Author(); | |
$article = new Article(); | |
// 建立 Article 和 Author 的關聯 | |
$article->setAuthor($author); | |
} else { | |
// 編輯資料的情形時,從 Article 物件取得 Author 物件 | |
$author = $article->getAuthor(); | |
} | |
$article->setTitle($articleData['title']); | |
$article->setContent($articleData['content']); | |
$author->setName($authorData['name']); | |
$em = $this->getDoctrine()->getManager(); | |
// 先 persist Author 物件, 再 persist Article 物件,確保關聯的正確性 | |
$em->persist($author); | |
$em->persist($article); | |
$em->flush(); | |
// 更新成功後,重新導向到編輯頁 | |
return $this->redirect($this->generateUrl('edit', ['id' => $article->getId()])); | |
} | |
if (empty($article)) { | |
$article = new Article(); | |
$article->setAuthor(new Author()); | |
} | |
return $this->render('AppBundle:Default:edit.html.twig', ['article' => $article]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# AppBundle/Resources/views/Default/edit.html.twig #} | |
<html> | |
<body> | |
<form method="post"> | |
<input type="hidden" name="article[id]" value="{{ article.id }}" /> | |
<input type="hidden" name="author[id]" value="{{ article.author.id }}" /> | |
Article title: <input name="article[title]" value="{{ article.title }}" /> | |
<br> | |
Article content: <textarea name="article[content]">{{ article.content }}</textarea> | |
<br> | |
Author: <input name="author[name]" value="{{ article.author.name }}" /> | |
<br> | |
<button>Submit</button> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment