以编程方式在SimpleCMSBundle中添加页面

时间:2013-12-26 08:02:52

标签: symfony symfony-cmf

我正在寻找一种在SimpleCMSBundle中以编程方式添加页面的方法。我找到了以下命令行。

php app/console doctrine:phpcr:migrator page --identifier=/cms/simple/test

我需要的是首先是上述命令的程序化等价物,其次上面的命令假设存在一个位于'app / Resources / data /的.yml文件 pages / test.yml,我也想以编程方式提供这些信息。

我正在使用Symfony CMF标准版。

1 个答案:

答案 0 :(得分:2)

SimpleCmsBundle提供了两种创建页面的方法:

1。创建一个将被解析的yaml文件

这是由DataFixture完成的。 AcmeDemoBundle附带了一个yaml文件,您可以使用它来添加自己的页面:

# src/Acme/MainBundle/Resources/data/page.yml
static:
    # ...

    my_new_page:
        name: "my_page"
        label: "My Page"
        title: "My new Page"
        body: "I've added this when following instructions on SO"

现在你需要执行php app/console doctrine:phpcr:fixtures:load,它将执行DataFixtures,并创建新页面!

2。保留文件

对yaml数据的真正做法是在PHPCR树中创建并保留Page文档。在DataFixture内部,或者在要添加页面的控制器/管理员中,执行以下操作:

use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;

// ...
$page = new Page();
$page->setName('my_other_page');
$page->setLabel('My other Page');
$page->setTitle('My other Page');
$page->setBody('I\'ve added this myself!!');

$documentManager = ...; // get the document manager, the first argument of DataFixture::load or $container->get('doctrine_phpcr')->getManager()

$root = $documentManager->find(null, '/cms/simple'); // get the root document
$page->setParent($root); // add document to the root

$documentManager->persist($page); // add the page in the queue for persisting
$documentManager->flush(); // execute the queries

有关详细信息:http://symfony.com/doc/current/cmf/book/database_layer.html