Asked  7 Months ago    Answers:  5   Viewed   43 times

Is it possible to generate a single entity from database using the Symfony2 console tool?

In the middle of coding I had to add a table and there are modifications made to the existing entity classes. So I don't want all my entities regenerated.

Any suggestions will be appreciated!

 Answers

22

I had the same problem, you've to do this way:

php app/console doctrine:mapping:convert metadata_format 
    ./src/App/MyBundle/Resources/config/doctrine 
    --from-database 
    --filter="Yourtablename"

Then

php app/console doctrine:mapping:import AppMyBundle 
    metadata_format --filter="Yourtablename"

Where metadata_format is the file ending you want to generate (e.g. xml, yml, annotation)

And finally

php app/console doctrine:generate:entities AppMyBundle --no-backup

Like this doctrine will load only the entity you need. Just be carefull on the filter you must use the CamelCase !

Hope this will help you

Wednesday, March 31, 2021
 
Ultimater
answered 7 Months ago
11

It seems to me that using Doctrines ODM is not the right way to approach this. You can still use Doctrine to connect to databases and query them. But if you have no entity classes the use of an entity manager seems to be inappropriate.

Use Doctrine for Connection handling

Here is how you create a connection to a Database with the doctrine Connection class:

/** @var DoctrineBundleDoctrineBundleConnectionFactory $connectionFactory */
$connectionFactory = $this->getContainer()->get('doctrine.dbal.connection_factory');
$connection = $connectionFactory->createConnection(
    array('pdo' => new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password))
);

Now you can use $connection as a simple PDO object:

$connection->executeQuery('SELECT * FROM your_table');

You could add this code as a service to make it accessible everywhere.
If you want to connect to a different database for a different domain you can use this code to identify the domain:

$this->getRequest()->getHost();

To access the domain in an action do this:

public function yourAction(Request $request, /* ... */)
{
    // the Controller extends the Container. So need to get it here:
    $connectionFactory = $this->get('doctrine.dbal.connection_factory');

    // also access the domain like this:
    $domain = $request->getHost();
}
Saturday, May 29, 2021
 
Lorav
answered 5 Months ago
98

you can do this by add the following lines in

vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php

 $tables = array();
    $tables[] = $schemaManager->listTableDetails("country");
    $tables[] = $schemaManager->listTableDetails("provider_country");
    $tables[] = $schemaManager->listTableDetails("provider");
    $this->setTables($schemaManager->listTables(), $tables);

inside __construct

public function __construct(AbstractSchemaManager $schemaManager)

Note: That will override OneToMany annotation.

Saturday, May 29, 2021
 
alez
answered 5 Months ago
85

Simply this is change inside doctrine orm lib,

you can fix that by changes in

vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php

replace the following statements

foreach ($foreignKeys as $foreignKey) {
          $foreignTable = $foreignKey->getForeignTableName();
          $cols = $foreignKey->getColumns();
          $fkCols = $foreignKey->getForeignColumns();

          $localColumn = current($cols);
          $associationMapping = array();
          $associationMapping['fieldName'] = $this->getFieldNameForColumn($tableName, $localColumn, true);
          $associationMapping['targetEntity'] = $this->getClassNameForTable($foreignTable);

          if ($primaryKeyColumns && in_array($localColumn, $primaryKeyColumns)) {
          $associationMapping['id'] = true;
          }

          for ($i = 0; $i < count($cols); $i++) {
          $associationMapping['joinColumns'][] = array(
          'name' => $cols[$i],
          'referencedColumnName' => $fkCols[$i],
          );
          }

          //Here we need to check if $cols are the same as $primaryKeyColums
          if (!array_diff($cols, $primaryKeyColumns)) {
          $metadata->mapOneToOne($associationMapping);
          } else {
          $metadata->mapManyToOne($associationMapping);
          }
          }

change to

foreach ($foreignKeys as $foreignKey) {
            $foreignTable = $foreignKey->getForeignTableName();
            $cols = $foreignKey->getColumns();
            $fkCols = $foreignKey->getForeignColumns();

            $localColumn = current($cols);
            $associationMapping = array();
            $associationMapping['fieldName'] = $this->getFieldNameForColumn($tableName, $localColumn, true);
            $associationMapping['targetEntity'] = $this->getClassNameForTable($foreignTable);

            for ($i = 0; $i < count($cols); $i++) {
                $associationMapping['joinColumns'][] = array(
                    'name' => $cols[$i],
                    'referencedColumnName' => $fkCols[$i],
                );
            }
            $metadata->mapManyToOne($associationMapping);
        }

        foreach ($this->tables as $tableCandidate) {
            if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
                $foreignKeysCandidate = $tableCandidate->getForeignKeys();
            } else {
                $foreignKeysCandidate = array();
            }

            foreach ($foreignKeysCandidate as $foreignKey) {
                $foreignTable = $foreignKey->getForeignTableName();

                if ($foreignTable == $tableName && !isset($this->manyToManyTables[$tableCandidate->getName()])) {

                    $fkCols = $foreignKey->getForeignColumns();
                    $cols = $foreignKey->getColumns();


                    $localColumn = current($cols);

                    $associationMapping = array();
                    $associationMapping['fieldName'] = $this->getFieldNameForColumn($tableCandidate->getName(), $tableCandidate->getName(), true);
                    $associationMapping['targetEntity'] = $this->getClassNameForTable($tableCandidate->getName());
                    $associationMapping['mappedBy'] = $this->getFieldNameForColumn($tableCandidate->getName(), $localColumn, true);

                    try {
                        $primaryKeyColumns = $tableCandidate->getPrimaryKey()->getColumns();
                        if (count($primaryKeyColumns) == 1) {
                            $indexColumn = current($primaryKeyColumns);
                            $associationMapping['indexBy'] = $indexColumn;
                        }
                    } catch (SchemaException $e) {

                    }

                    $metadata->mapOneToMany($associationMapping);
                }
            }
        }

now if you run doctrine:mapping:import

you will find OneToMany annotation.

then run doctrine:generate:entities

you will find A unidirectional relationship on both sides.

Tuesday, August 3, 2021
 
Timur Mustafaev
answered 3 Months ago
97

Try fixing the bundle's version:

[DoctrineMongoDBBundle]
    git=http://github.com/symfony/DoctrineMongoDBBundle.git
    target=/bundles/Symfony/Bundle/DoctrineMongoDBBundle
    version=v2.0.0

And remember to always use bin/vendors install — not bin/vendors update. Unless you are developing Symfony itself, of course.

Wednesday, September 15, 2021
 
Peanut
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share