Sylius - 在新实体中实现 Slugable 和 Timestampable
Sylius - Implements Slugable and Timestampable in new entity
我正在尝试使用新的 Sylius 版本 (1.0) 创建一些实体。
我正在阅读他们的文档,但我有点生气。
获得了一个新实体 Book.php
。
这个实体有三个字段,author、title 和 content.
如何使用此实体上的 Slugable 和 Timestampable 来使用注释在此实体上创建两个新字段?
我尝试创建一个扩展 Slugable 和 Timestampable 接口的接口 BookInterface.php
,但显然,Doctrine 不映射接口的字段。
Book.php
<?php
namespace Acme\SyliusBookBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Model\ResourceInterface;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Book
*
* @ORM\Table(name="book")
* @ORM\Entity(repositoryClass="App\SyliusBookBundle\Repository\BookRepository")
*/
class Book implements ResourceInterface
{
public function __construct()
{
$this->createdAt = new \DateTime();
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\ManyToOne(targetEntity="Sylius\Component\User\Model\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $author;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
已解决
使用 Gedmo 实体特征非常有效。
Gedmo\Timestampable\Traits\TimestampableEntity;
然后在你的实体上使用它 class:
class Post implements ResourceInterface
{
use TimestampableEntity;
查看 Component/Product/Model/Product
它使用 TimestampableTrait
并使用 SlugAwareInterface
(通过 ProductInterface
)。这些界面保存在Component/Resource/Model/
文件夹
所以使用特征,并扩展SlugAwareInterface
。然后添加必要的 slug 函数。 getSlug() setSlug()
Interface ProductInterface extends
AttributeSubjectInterface,
SlugAwareInterface,
TimestampableInterface,
ToggleableInterface,
ProductTranslationInterface,
AssociableInterface,
CodeAwareInterface,
TranslatableInterface,
VariableInterface
{
我认为您不能将注释与来自 Sylius 的 TimestampableTrait
混合使用。一个更简单的解决方案是定义您自己的特征或使用 Gedmo 提供的特征。
不管怎样,你为什么要把注释和代码混在一起?将映射保存在 xml/yaml 文件中可以拆分逻辑并使代码更易于理解。将数据库映射保留在实体内部也违反了单一责任原则(SOLID 原则)。
PS。当你解决了你的问题时,我正在写它。我就把它留在这里,也许有人会发现 link 有用。
我正在尝试使用新的 Sylius 版本 (1.0) 创建一些实体。
我正在阅读他们的文档,但我有点生气。
获得了一个新实体 Book.php
。
这个实体有三个字段,author、title 和 content.
如何使用此实体上的 Slugable 和 Timestampable 来使用注释在此实体上创建两个新字段?
我尝试创建一个扩展 Slugable 和 Timestampable 接口的接口 BookInterface.php
,但显然,Doctrine 不映射接口的字段。
Book.php
<?php
namespace Acme\SyliusBookBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Model\ResourceInterface;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Book
*
* @ORM\Table(name="book")
* @ORM\Entity(repositoryClass="App\SyliusBookBundle\Repository\BookRepository")
*/
class Book implements ResourceInterface
{
public function __construct()
{
$this->createdAt = new \DateTime();
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\ManyToOne(targetEntity="Sylius\Component\User\Model\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $author;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
已解决
使用 Gedmo 实体特征非常有效。
Gedmo\Timestampable\Traits\TimestampableEntity;
然后在你的实体上使用它 class:
class Post implements ResourceInterface
{
use TimestampableEntity;
查看 Component/Product/Model/Product
它使用 TimestampableTrait
并使用 SlugAwareInterface
(通过 ProductInterface
)。这些界面保存在Component/Resource/Model/
文件夹
所以使用特征,并扩展SlugAwareInterface
。然后添加必要的 slug 函数。 getSlug() setSlug()
Interface ProductInterface extends
AttributeSubjectInterface,
SlugAwareInterface,
TimestampableInterface,
ToggleableInterface,
ProductTranslationInterface,
AssociableInterface,
CodeAwareInterface,
TranslatableInterface,
VariableInterface
{
我认为您不能将注释与来自 Sylius 的 TimestampableTrait
混合使用。一个更简单的解决方案是定义您自己的特征或使用 Gedmo 提供的特征。
不管怎样,你为什么要把注释和代码混在一起?将映射保存在 xml/yaml 文件中可以拆分逻辑并使代码更易于理解。将数据库映射保留在实体内部也违反了单一责任原则(SOLID 原则)。
PS。当你解决了你的问题时,我正在写它。我就把它留在这里,也许有人会发现 link 有用。