我几天前开始学习Sylius。我需要在Symfony 3项目中使用电子商务模块。所以我要用Sylius扩展我的项目,我只是使用Core / Components堆栈。
看起来很简单,但最初没有工作,因为Composer安装了一个不再支持的稳定版本(0.19)......无论如何,我终于安装了sylius / sylius~1.0@dev包,所以我的composer.json看起来像那样:
"minimum-stability" : "dev",
"prefer-stable" : true,
"require" : {
"php" : "^7.0",
"sylius/sylius" : "^1.0@beta",
"friendsofsymfony/user-bundle" : "~2.0@dev",
"symfony/assetic-bundle" : "^2.8",
"gedmo/doctrine-extensions" : "^2.4"
},
=>第一个问题:这是正确的方法???
在SyliusBundle / app / config.yml中:
imports:
- { resource: "@SyliusProductBundle/Resources/config/app/config.yml" }
# Doctrine
stof_doctrine_extensions:
default_locale: "%locale%"
orm:
default:
tree: true
sluggable: true
timestampable: true
loggable: false
sortable: true
# Product
sylius_product:
driver: doctrine/orm
resources:
product:
classes:
model: OfferBundle\Entity\Offer
repository: OfferBundle\Entity\OfferRepository
form: OfferBundle\Form\OfferType
我遵循Sylius指南来自定义继承自Sylius Product模型的Offer模型(非常简单)
在OfferBundle / Entity / Offer.php中:<?php
namespace OfferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Product as BaseProduct;
/**
* @ORM\Table(name="offer")
* @ORM\Entity(repositoryClass="OfferRepository")
*/
class Offer extends BaseProduct
{
=&GT;问题2:听起来不错?
在OfferBundle / Form / OfferType.php中:
<?php
namespace OfferBundle\Form;
use Symfony\Component\OptionsResolver\OptionsResolver;
use OfferBundle\Entity\Offer;
use Symfony\Component\Form\AbstractType;
class OfferType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => Offer::class]);
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'offer';
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return \Sylius\Bundle\ProductBundle\Form\Type\ProductType::class;
}
}
在OfferBundle / Form / Extension / OfferTypeExtension.php中:
<?php
namespace OfferBundle\Form\Extension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Form\Type\WysiwygType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use OfferBundle\Form\Type\TagsType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use OfferBundle\Entity\Offer;
use MediaBundle\Form\MediaFileType;
use MediaBundle\Form\MediaVideoType;
use CompanyBundle\Entity\Company;
use Tetranz\Select2EntityBundle\Form\Type\Select2EntityType;
use Admin\CompanyBundle\Controller\CompanyController;
use Symfony\Component\Form\AbstractTypeExtension;
use OfferBundle\Form\ContactType;
use OfferBundle\Form\PriceType;
use OfferBundle\Form\OfferType;
final class OfferTypeExtension extends AbstractTypeExtension
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('advantage', WysiwygType::class)
->add('ecommerce')
->add('enabled')
->add('bestseller')
->add('company', Select2EntityType::class, [
'multiple' => false,
'remote_route' => 'admin_company_company_autocomplete',
'class' => Company::class,
'primary_key' => 'id',
'text_property' => 'name',
'minimum_input_length' => 0,
'page_limit' => CompanyController::NB_PER_PAGE,
'allow_clear' => false,
'delay' => 250,
'cache' => false,
'cache_timeout' => 60000,
'language' => 'fr',
'placeholder' => 'form.choose',
'required' => true,
])
->add('state', ChoiceType::class, [
'choices' => Offer::getStates(),
'placeholder' => 'form.choose',
])
...
表单扩展由服务加载。 在OfferBundle / Resources / config / services.yml中:
offer.form.extension.type.offer:
class: OfferBundle\Form\Extension\OfferTypeExtension
tags:
- { name: form.type_extension, extended_type: OfferBundle\Form\OfferType }
问题3:它的工作原理但是看到Sylius \ Bundle \ CoreBundle \ Form \ Extension \ ProductTypeExtension包含额外的字段,如$ images,$ variantSelectionMethod ......但它并没有出现在我的表单中。所以我想某个地方没有加载service.yml?
希望有人可以帮助我!
答案 0 :(得分:0)
我认为问题是因为你覆盖了表单,而没有扩展它。因此,您只能看到表单字段,而不是“产品”表单字段(因为此表单不再使用)。 如果您希望对产品表单进行一些更改,您只需要制作表单扩展即可修改现有的产品表单。
http://docs.sylius.org/en/latest/customization/form.html
希望这有帮助! :)
答案 1 :(得分:0)
您可能还需要覆盖表单的模板以显示这些添加的字段。