以前在创建夹具文档以构建网站时(用于测试或没有 CMS 的网站)。我一直按照示例文档中显示的方式进行操作。所以一个灯具页面会有很多重复的代码。像这样(这是一个简短的!):
class codeExampleFixtures extends AbstractFixture implements OrderedFixtureInterface
{
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
/*** Begin data fixture ***/
$code1 = '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';
// The $exampleX variable can be left empty if not needed.
$example1 = '<p>In this example. The code is different for the example itself.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';
$lang1 = 'html'; // choices are 'html', 'css' and 'js'
// Do not edit anything else in this fixture.
$codeExample1 = new codeExample();
$codeExample1->setlang($lang1);
$codeExample1->setcodeExample($code1);
$exampleFixture1 = (empty($example1)) ? $code1 : $example1;
$codeExample1->settextExample($exampleFixture);
$manager->persist($codeExample1);
$this->setReference('ce1', $codeExample1);
/*** End data fixture ***/
/*** Begin data fixture ***/
$code2 = '<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>';
// The $exampleX variable can be left empty if not needed.
$example2 = Null;
$lang2 = 'css'; // choices are 'html', 'css' and 'js'
// Do not edit anything else in this fixture.
$codeExample2 = new codeExample();
$codeExample2->setlang($lang2);
$codeExample2->setcodeExample($code2);
$exampleFixture2 = (empty($example2)) ? $code2 : $example2;
$codeExample2->settextExample($exampleFixture2);
$manager->persist($codeExample2);
$this->setReference('ce2', $codeExample2);
/*** End data fixture ***/
$manager->flush(); //Upload the data to the database.
}
我决定重构它,我认为我的结果可能对其他做类似事情的人有用。我相信有办法改进它(我很高兴听到它们!)而且我相信会有很多人已经考虑过自己做类似的事情,但如果它对你有用,请随意使用它 :)