我有一个Page实体,它与Sonata MediaBundle的Media实体有多对一的关联
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity
name="Application\Sonata\PageBundle\Entity\Page"
table="page__page"
>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="shortContent" type="text" column="short_content" nullable="true" />
<many-to-one field="media" target-entity="Application\Sonata\MediaBundle\Entity\Media">
<join-column name="media_id" />
</many-to-one>
<lifecycle-callbacks>
<lifecycle-callback type="postPersist" method="postPersist"/>
</lifecycle-callbacks>
</entity>
</doctrine-mapping>
当我以管理员身份登录时 - 一切正常,我可以使用{{ page.getMedia() }}
访问twig中的媒体
但是,如果我在隐身窗口中打开此页面,该页面的媒体将是null
任何人有任何想法?
我找到了这种情况的解决方案。
如果我们没有以管理员身份登录,我们会看到某个页面的快照,当我们向Page实体添加新字段时,快照创建者对此字段一无所知,因此我们需要扩展Sonata \ PageBundle \ Model \ Transformer模型
Page.orm.xml
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity
name="Application\Sonata\PageBundle\Entity\Page"
table="page__page"
>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="shortContent" type="text" column="short_content" nullable="true" />
<many-to-one field="media" target-entity="Application\Sonata\MediaBundle\Entity\Media">
<join-column name="media_id" />
</many-to-one>
<lifecycle-callbacks>
<lifecycle-callback type="postPersist" method="postPersist"/>
</lifecycle-callbacks>
</entity>
</doctrine-mapping>
Snapshot.orm.xml
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity
name="Application\Sonata\PageBundle\Entity\Snapshot"
table="page__snapshot"
>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<many-to-one field="media" target-entity="Application\Sonata\MediaBundle\Entity\Media">
<join-column name="media_id" />
</many-to-one>
<gedmo:loggable log-entry-class="Gedmo\Loggable\Entity\LogEntry"/>
</entity>
</doctrine-mapping>
并且页面到快照变换器类
PageTransformer.php
<?php
namespace Application\Sonata\PageBundle\Entity;
use Sonata\PageBundle\Entity\Transformer;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Model\SnapshotInterface;
class PageTransformer extends Transformer
{
public function create(PageInterface $page)
{
/** @var Snapshot $snapshot */
$snapshot = parent::create($page);
$content = $snapshot->getContent();
$snapshot->setContent($content);
$snapshot->setMedia($page->getMedia());
return $snapshot;
}
public function load(SnapshotInterface $snapshot)
{
$page = parent::load($snapshot);
$page->setMedia($snapshot->getMedia());
return $page;
}
}
差点忘了!!!我们需要说Sonata使用我们的Transformer类
parameters.yml
parameters:
sonata.page.transformer.class: Application\Sonata\PageBundle\Entity\PageTransformer