/** * Generate the entity PHP code. * * @param BundleInterface $bundle * @param string $name * @param array $fields * @param string $namePrefix * @param string $dbPrefix * @param string|null $extendClass * * @return array * @throws \RuntimeException */ protected function generateEntity(BundleInterface $bundle, $name, $fields, $namePrefix, $dbPrefix, $extendClass = null) { // configure the bundle (needed if the bundle does not contain any Entities yet) $config = $this->registry->getManager(null)->getConfiguration(); $config->setEntityNamespaces(array_merge(array($bundle->getName() => $bundle->getNamespace() . '\\Entity\\' . $namePrefix), $config->getEntityNamespaces())); $entityClass = $this->registry->getAliasNamespace($bundle->getName()) . '\\' . $namePrefix . '\\' . $name; $entityPath = $bundle->getPath() . '/Entity/' . $namePrefix . '/' . str_replace('\\', '/', $name) . '.php'; if (file_exists($entityPath)) { throw new \RuntimeException(sprintf('Entity "%s" already exists.', $entityClass)); } $class = new ClassMetadataInfo($entityClass, new UnderscoreNamingStrategy()); foreach ($fields as $fieldSet) { foreach ($fieldSet as $fieldArray) { foreach ($fieldArray as $field) { if (array_key_exists('joinColumn', $field)) { $class->mapManyToOne($field); } elseif (array_key_exists('joinTable', $field)) { $class->mapManyToMany($field); } else { $class->mapField($field); } } } } $class->setPrimaryTable(array('name' => strtolower($dbPrefix . strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name))) . 's')); $entityCode = $this->getEntityGenerator($extendClass)->generateEntityClass($class); return array($entityCode, $entityPath); }
/** * Generate Fixture from bundle name, entity name, fixture name and ids * * @param BundleInterface $bundle * @param string $entity * @param string $name * @param array $ids * @param string|null $connectionName */ public function generate(BundleInterface $bundle, $entity, $name, array $ids, $order, $connectionName = null) { // configure the bundle (needed if the bundle does not contain any Entities yet) $config = $this->registry->getManager($connectionName)->getConfiguration(); $config->setEntityNamespaces(array_merge(array($bundle->getName() => $bundle->getNamespace() . '\\Entity'), $config->getEntityNamespaces())); $fixtureFileName = $this->getFixtureFileName($entity, $name, $ids); $entityClass = $this->registry->getAliasNamespace($bundle->getName()) . '\\' . $entity; $fixturePath = $bundle->getPath() . '/DataFixtures/ORM/' . $fixtureFileName . '.php'; $bundleNameSpace = $bundle->getNamespace(); if (file_exists($fixturePath)) { throw new \RuntimeException(sprintf('Fixture "%s" already exists.', $fixtureFileName)); } $class = new ClassMetadataInfo($entityClass); $fixtureGenerator = $this->getFixtureGenerator(); $fixtureGenerator->setFixtureName($fixtureFileName); $fixtureGenerator->setBundleNameSpace($bundleNameSpace); $fixtureGenerator->setMetadata($class); $fixtureGenerator->setFixtureOrder($order); /** @var EntityManager $em */ $em = $this->registry->getManager($connectionName); $repo = $em->getRepository($class->rootEntityName); if (empty($ids)) { $items = $repo->findAll(); } else { $items = $repo->findById($ids); } $fixtureGenerator->setItems($items); $fixtureCode = $fixtureGenerator->generateFixtureClass($class); $this->filesystem->mkdir(dirname($fixturePath)); file_put_contents($fixturePath, $fixtureCode); }