之前学会了怎么创建entity和数据库的常用配置,现在来看看entity定义column时的常用类型以及一些属性,这样就可以完全掌控定义entity了。
使用@ORM\Column 定义字段
type:用来定义字段的类型
一些字段常用的type:
string:字符串类型,对应varchar
integer:int类型
smallint:短int类型
bigint:长int类型
boolean:布尔类型
decimal:对应数据库decmail类型,双精度类型
datetime:日前datetime类型
time:time类型
text:文本类型
float:浮点类
name: 字段的名称
length : 字段的长度
unique :字段在数据库中的长度,只支持string类型使用
nullable : 数据库是否为空
options:条件组合
下面常见的有
default :字段默认值
unsigned : 非负数
comment : 字段说明注释
example:
<?php
/**
* @ORM\var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=32,
unique=true, nullable=false)
*/
protected $username;
/**
* @ORM\Column(type="string", columnDefinition="CHAR(2)
NOT NULL")
*/
protected $country;
/**
* @ORM\Column(type="decimal", precision=2,
scale=1)
*/
protected $height;
/**
* @ORM\Column(type="string", length=2,
options={"fixed":true, "comment":"Initial letters of first and last name"})
*/
protected $initials;
/**
* @ORM\Column(type="integer", name="login_count"
nullable=false, options={"unsigned":true, "default":0})
*/
protected $loginCount;
使用@ORM\Entity指定repository
<?php
/**
* @ORM\Entity(repositoryClass="MyProject\UserRepository")
*/
classUser
{
//...
}
使用@ORM\Table对表重新定义
name : 定义表的名字
indexes : 定义一组索引
uniqueConstraints : 定义一组约束
<?php
/**
* @Entity
* @Table(name="user",
* uniqueConstraints={@UniqueConstraint(name="user_unique",columns={"username"})},
* indexes={@Index(name="user_idx",
columns={"email"})}
* )
*/
classUser{}
使用@ORM\Index定义一个索引
<?php
/**
* @ORM\Entity
* @ORM\Table(name="ecommerce_products",indexes={@ORM\Index(name="search_idx",
columns={"name", "email"}, options={"where": "(((id IS NOT NULL) AND (name IS NULL)) AND (email IS NULL))"})})
*/
classECommerceProduct
{
}
使用@ORM\HasLifecycleCallbacks 做事件回调
<?php
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
classUser
{
/**
* @ORM\PostPersist
*/
publicfunctionsendOptinMail(){}
}
可以使用@ORM\PostLoad, @ORM\PrePersist, @ORM\PostPersist, @ORM\PreRemove, @ORM\PostRemove, @ORM\PreUpdate or @ORM\PostUpdate 当这些事件发生的时候,触发一个回调。
转载于:https://blog.51cto.com/ww123/1868553