我改变了主意并使用了注释,因为 yaml 让我感到困惑。
至今 :
对于来宾实体
<?php
// xxxxBundle/Entity/Guest.php
namespace xxxxBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="guest")
*/
class Guest
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToMany(targetEntity="Category", inversedBy="guests")
* @JoinTable(name="guests_categories")
*/
protected $categories;
/**
* @ManyToMany(targetEntity="Event", inversedBy="events")
* @JoinTable(name="guests_events")
*/
protected $events;
/**
* @ORM\Column(type="string", length=30)
*/
protected $name;
/**
* @ORM\Column(type="string", length=30)
*/
protected $surname;
/**
* @ORM\Column(type="string", length=30)
*/
protected $email;
/**
* @ORM\Column(type="string", length=100)
*/
protected $address;
/**
* @ORM\Column(type="string", length=10)
*/
protected $phone;
/**
* @ORM\Column(type="string", length=10)
*/
protected $mobile;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\Column(type="datetime")
*/
protected $created_at;
/**
* @ORM\Column(type="datetime")
*/
protected $updated_at;
/**
* @ORM\Column(type="string")
*/
protected $token;
/**
* @ORM\Column(type="boolean")
*/
protected $is_activated;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('surname', new NotBlank());
$metadata->addPropertyConstraint('email', new Email(array(
'message' => 'I do not like invalid emails. Give me a real one!')));
$metadata->addPropertyConstraint('phone', new MaxLength(10));
$metadata->addPropertyConstraint('mobile', new MaxLength(10));
}
}
对于类别实体
<?php
// xxxxBundle/Entity/Category.php
namespace xxxxBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToMany(targetEntity="Guest", mappedBy="categories")
*/
protected $guests;
/**
* @ORM\Column(type="string", length=30)
*/
protected $name;
/**
* @ORM\Column(type="text")
*/
protected $description;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
}
}
对于事件实体
<?php
// xxxxBundle/Entity/Event.php
namespace xxxxBundle\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="event")
*/
class Event
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToMany(targetEntity="Guest", mappedBy="categories")
*/
protected $guests;
/**
* @ORM\Column(type="string", length=30)
*/
protected $name;
/**
* @ORM\Column(type="string", length=100)
*/
protected $location;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\Column(type="datetime")
*/
protected $scheduled_at;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('location', new NotBlank());
}
}
我对出勤实体感到困惑。出勤实体将具有以下变量:
- Guest_id
- Event_id
- Will_attend(是/否/也许)
- 评论
- Replied_at
- 更新_at
我不知道哪个是主键(或主键?)。db 表不需要有单独的 Id(或没有?)。
<?php
// xxxxBundle/Entity/Attendance.php
namespace xxxxBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="attendance")
*/
class Attendance
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ManyToMany(targetEntity="Guest", mappedBy="categories")
*/
protected $guests;
/**
* @ManyToMany(targetEntity="Event", mappedBy="events")
*/
protected $events;
/**
* @ORM\Column(type="string", length=3)
*/
protected $will_attend;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\Column(type="datetime")
*/
protected $replied_at;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
}