Asked  7 Months ago    Answers:  5   Viewed   35 times

I have around 40 entities and many bidirectional relationships. Whenever i use var_dump($user) or any entity my browser gets loaded with too much data of arrays and variables then it just crashed.

i want to whats the problem.

The data is being inserted fine. Can i cause issue in production.

 Answers

31

Replace var_dump() with the debug method dump() provided by Doctrine Common.

DoctrineCommonUtilDebug::dump($user);

It works for single objects and Doctrine collections and should prevent browser displaying issues you are having.

Wednesday, March 31, 2021
 
matthy
answered 7 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
22

The parent entity needs to give visibility of it's properties to it's children.

Change your $id property visibility to "protected".

Saturday, May 29, 2021
 
Kenny
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
48

Answer was less complex than I expected.

It seem to be sufficent call $this->entity_manager->clear(); that will clear this entity map and force it to reload from database into a brand new object!

$this->entity_manager->clear();
$aCopy = $this->entity_manager
                       ->getRepository('MyBundle:A')
                       ->find($a->getId());
            $this->logger->debug('Original Obj: '.spl_object_hash($a));
            $this->logger->debug('Copied Obj:      '.spl_object_hash($aCopy));

this will print

[2013-02-08 12:07:20] app.DEBUG: Original Obj: 000000006523645c000000004b1160d1 [] [] [2013-02-08 12:07:20] app.DEBUG: Copied Obj: 00000000652366e3000000004b1160d1 [] []

Monday, August 23, 2021
 
Jeff Mercado
answered 2 Months 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