symfony php 更新内容,Symfony框架中的Doctrine
在新的標準版本的Symfony框架中已經集成了Doctrine,Doctrine就是一種對象關系映射(ORM)同時也是一種數據庫抽象層(DBAL),使用ORM和DBAL能讓我們很輕易的操作數據庫。本文章重點在于在Symfony中使用Doctrine——如何創建數據庫表的模型,如何創建表的關系,怎么查詢數據等等。
創建和配置數據庫
Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: utf8mb4
collate: utf8mb4_unicode_ci
Doctrine作為PDO的頂層可以操作任何支持PDO的數據庫操作系統(比如MySQL,PostgreSQL,Microsoft SQL,MongoDB和MariaDB)。這里推薦設置默認的數據庫字符集和排序集為utf8mb4。有別于老的utf8字符集,utf8mb4支持4字節的Unicode編碼。數據庫的主機地址、端口、數據庫名稱、用戶名和密碼通常在composer安裝的時候已經設置好,但是你仍然可以在app/config/parameters.yml中編輯。
創建實體類
每一個實體(數據表)應表示為一個類,并且文件放置在AppBundle/Entity目錄下:
// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class User
{
private $id;
private $name;
private $email;
private $password;
private $createdAt;
}
這個類表示了用戶數據表,它擁有id,name,email,password和created_at字段。為了給Doctrine提供每一個字段的類型和關系,我們還要寫一些注釋:
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $password;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\ManyToOne(targetEntity="Group", inversedBy="users")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
*/
private $group;
}
表名的注釋是可選的,如果你省略了,Doctrine會根據類的名稱去自動生成一個對應的表名。在上面的代碼中,只展示了一些字段的屬性。
現在讓我們來創建一個表用來描述用戶組,每一個用戶只能在其中一個用戶組中:
// src/AppBundle/Entity/Group.php
/**
* @ORM\Entity
* @ORM\Table(name="group")
*/
class Group
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @ORM\Column(nullable=true)
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="group")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
}
請注意,表關系同樣被寫在了注釋當中。OneToMany是一個關系的名稱,targetEntity是一個實體類,JoinColumn定義了外鍵。同時,在OneToMany的關系中,我們需要在構造方法中聲明一個變量ArrayCollection。
類中的變量被定義為了私有變量,為了獲取到這些私有變量,我們需要創建一個設置變量和獲取變量的方法。你可以手動創建這些方法,但是在你的項目目錄中使用下面的命令可以輕松的實現:
php app/console doctrine:generate:entities AppBundle/Entity/User
在執行了上面的命令之后,User類會變成下面的樣子:
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $password;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\ManyToOne(targetEntity="Group", inversedBy="users")
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
*/
private $group;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
/**
* @param mixed $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @param mixed $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return mixed
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @param mixed $createdAt
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}
}
創建和更新數據庫模式
在Doctrine中,在已經有了一個實體咧的基礎上執行下面的命令可以輕易的創建一個數據庫模式:
php app/console doctrine:schema:update --force
上面的命令不僅可以創建數據庫模式,同時在你的開發過程中還會更新數據庫模式。在數據庫中執行SQL查詢的時候,不推薦使用數據庫模式,推薦在生產環境中更新。
數據庫查詢
到這一步,我們已經創建了一個實體類,定義了它的表關系并且創建了數據庫模式。現在我們將要學習如何在數據庫中創建、查詢、更新和刪除數據。
$group = new Group();
$group->setName('Administrator');
$group->setDescription('Users with highest privileges in the system');
$em = $this->getDoctrine()->getManager();
// Tells Doctrine you want to save the group (no queries yet)
$em->persist($group);
// This line actually executes the SQL query
$em->flush();
在剛剛創建的用戶組表中查詢id:
$group->getId();
從數據庫中查詢數據:
$group = $this->getDoctrine()
->getRepository('AppBundle:Group)
->find($groupId);
更新數據:
// Fetch the group
$em = $this->getDoctrine()->getManager();
$group = $em->getRepository('AppBundle:Group)->find($groupId);
// If the group does not exist, throw an exception
if (!$group) {
throw $this->createNotFoundException(
'Group not found'
);
}
// Update data
$group>setDescription('New group description.');
$em->flush();
從數據庫中刪除數據:
// Fetch group
$em = $this->getDoctrine()->getManager();
$group = $em->getRepository('AppBundle:Group)->find($groupId);
// Remove group
$em->remove($group);
$em->flush();
總結
以上是生活随笔為你收集整理的symfony php 更新内容,Symfony框架中的Doctrine的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java多字段排序,Java8对多个字段
- 下一篇: php byte转 宽字符,C++宽字符