Roo*_*eyl 7 propel symfony sonata-admin
我有Sonata Admin并运行使用Propel,定义了两个模型/ Admin类; Portfolio
并且Image
,投资组合项目可以有很多图像.
我有一个ImageAdmin
允许上传图像,需要与投资组合项目相关联.
在PortfolioAdmin
我可以使用模型表单映射器类型将现有图像添加到项目组合项.
有没有办法在添加/编辑项目组合项目时添加添加/删除图像的功能,而不是只选择现有项目,或者只是添加/删除相关项目的方法而不是删除图像对象,如同我此刻
我知道我可以选择为组合类编写自定义管理控制器,但有没有预先构建的方法来实现这种行为?
作为参考,一些代码摘录我所做的事情;
Schema.xml的
<database name="default" namespace="MyBundle\Model" defaultIdMethod="native">
<table name="portfolio">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="title" type="varchar" primaryString="1" size="100" />
<column name="description" type="LONGVARCHAR" />
<behavior name="sluggable" />
<behavior name="timestampable" />
<behavior name="archivable" />
</table>
<table name="image">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="portfolio_id" type="integer" required="true" />
<column name="title" type="varchar" primaryString="1" size="100" />
<column name="path" type="varchar" size="255" />
<column name="description" type="LONGVARCHAR" />
<foreign-key foreignTable="portfolio">
<reference local="portfolio_id" foreign="id"/>
</foreign-key>
<behavior name="sluggable" />
<behavior name="timestampable" />
<behavior name="archivable" />
</table>
</database>
Run Code Online (Sandbox Code Playgroud)
PortfolioAdmin.php
class PortfolioAdmin extends Admin
{
protected $baseRouteName = 'portfolio';
protected $baseRoutePattern = 'portfolio';
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', 'text', array('label' => 'Title'))
->add('Images', 'model', array(
'class' => 'MyBundle\Model\Image',
'multiple' => true,
'expanded' => true,
), array())
->add('description', 'text', array('label' => 'Description'))
;
}
}
Run Code Online (Sandbox Code Playgroud)
ImageAdmin.php
class ImageAdmin extends Admin
{
protected $baseRouteName = 'image';
protected $baseRoutePattern = 'image';
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', 'text', array('label' => 'Title'))
->add('portfolio', 'sonata_type_model', array('label' => 'Job'))
->add('description', 'text', array('label' => 'Description'))
->add('file', 'file', array('required' => false))
;
}
}
Run Code Online (Sandbox Code Playgroud)
admin.yml
services:
sonata.admin.portfolio:
class: MyBundle\Admin\PortfolioAdmin
tags:
- { name: sonata.admin, manager_type: propel, group: "Content", label: "Portfolio" }
arguments:
- ~
- MyBundle\Model\Portfolio
- ~
calls:
- [ setTranslationDomain, [MyBundle]]
sonata.admin.image:
class: MyBundle\Admin\ImageAdmin
tags:
- { name: sonata.admin, manager_type: propel, group: "Images", label: "Portfolio Image" }
arguments:
- ~
- MyBundle\Model\Image
- ~
calls:
- [ setTranslationDomain, [MyBundle]]
Run Code Online (Sandbox Code Playgroud)
小智 2
您可以使用普通的旧collection
字段类型。
鉴于以下情况ImageType
:
class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text');
$builder->add('description', 'textarea');
$builder->add('file', 'file', array('required' => false));
}
public function getName()
{
return 'image';
}
}
Run Code Online (Sandbox Code Playgroud)
该类PortfolioAdmin
变为:
class PortfolioAdmin extends Admin
{
protected $baseRouteName = 'portfolio';
protected $baseRoutePattern = 'portfolio';
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', 'text', array('label' => 'Title'))
->add('images', 'collection', array(
'type' => new ImageType(),
'by_reference' => false,
'allow_add' => true,
'allow_delete' => true,
))
->add('description', 'text', array('label' => 'Description'))
;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
590 次 |
最近记录: |