如何在Sylius中为新模型创建存储库?

时间:2015-04-18 10:13:26

标签: model doctrine repository entity sylius

我正在创建一个功能,可以在Sylius中为产品添加评论。

所以我在CommentProduct

中创建了一个新模型src/Sylius/Component/Product/Model/CommentProduct.php

我为CommentProduct定义了ORM映射:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping
  xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping"                  
  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                      http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>  
  <mapped-superclass name="Sylius\Component\Product\Model\CommentProduct" table="sylius_product_comment">
    <id name="id" type="integer" column="id">
      <generator strategy="AUTO"/>
    </id>
    <field name="createdAt" type="datetime" column="created_at">
        <gedmo:timestampable on="create"/>
    </field>
    <field name="updatedAt" type="datetime" column="updated_at" nullable="true">
        <gedmo:timestampable on="update"/>
    </field>
    <field name="commentParentId" type="integer" column="comment_parent_id"/>
    <field name="commentText" type="string" column="comment_text" length="1000"/>

    <field name="userId" type="integer" column="created_by"/>

    <many-to-one field="product" target-entity="Sylius\Component\Product\Model\ProductInterface" inversed-by="comments">
        <join-column name="product_id" referenced-column-name="id" nullable="false" />
    </many-to-one>

  </mapped-superclass>
</doctrine-mapping>

Product.orm.xml中,我为产品和评论添加了一对多的关系

<one-to-many field="comments" target-entity="Sylius\Component\Product\Model\CommentProduct" mapped-by="product" orphan-removal="true">
    <cascade>
        <cascade-all/>
    </cascade>
</one-to-many>

所以现在我想拥有一个模型ProductComment的存储库,我看到另一个存储库,调用就像:

$country = $this->get('sylius.repository.country');

但是当我打电话时:

$commentRepo = $this->container->get('sylius.repository.commentproduct');

我有一个例外:

  

您已请求不存在的服务“sylius.repository.commentproduct”。

所以我必须为我的模型设置存储库?有人可以帮帮我吗?非常感谢你

1 个答案:

答案 0 :(得分:0)