1

Doctrine Create Schemaイベントのデータベース(外部キー制約に使用されるデフォルトデータ)にいくつかのレコードを挿入したいと思います。

この関数は、コマンドライン(Symfony2)から次のコマンドを実行して呼び出されます。

php app/console doctrine:schema:update --force

このコマンドを実行すると、Doctrine2はデータベースのスキーマを生成します。

これに対応してデータベースにデータを挿入する方法はありますか?

ありがとう!

4

2 に答える 2

2

新しいコンソールコマンドを作成すると、探していることが達成されます。

コマンドコード

MyCool / Bundle / Command / GenerateSchemaUpdateCommand.php

<?php
namespace MyCool\Bundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateSchemaUpdateCommand extends ContainerAwareCommand
{
    /**
     * Configure command, set parameters definition and help.
     */
    protected function configure()
    {
        $this
            ->setName('mycoolbundle:schema:update')
            ->setDescription('Perform my custom schema update.')
            ->setHelp(sprintf(
            'Performs the doctrine:schema:update plus adds our custom records. \n' .
                PHP_EOL
        ));
    }

    /**
     * Execution Code
     * @param \Symfony\Component\Console\Input\InputInterface $input
     * @param \Symfony\Component\Console\Output\OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output = system('php -f app/console doctrine:schema:update --force');
        // do your custom things here.
        echo $output;
    }
}

コマンドを実行する

php -f app/console mycoolbundle:schema:update

引数の追加

次の方法でコマンドに引数を追加できます。

addArgument('var_name', InputArgument::OPTIONAL, 'Help message...')
// InputArgument::REQUIRED is also available here

ノート

このようにすることで、コンソールコマンドに多くの機能を追加できるようになります。さらに、モデルとエンティティが必要になった場合に、それらに直接アクセスできるようになります。

于 2013-01-09T20:52:03.667 に答える
1

DoctrineFixtureBundleを見てください!

初期データの読み込みに役立ちます。

于 2013-01-12T23:01:51.080 に答える