3

I'm currently using Symfony 4. I have written an entity with ORM and annotions

#src/Entity/User.php
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * 
 * User
 * 
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $motdepasse;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $nom;
...

I generate the entity using "php bin/console doctrine:generate:entities App\Entity\User". Unfortunately, it generates an entity in the php file "src/App/Entity/User.php" with the namespace "App\Entity". However the namespace should be "App\App\Entity" because of psr-4 rules. If I add a second "App", I get problems linked to repository and to console generations. If I let one "App", Symfony thinks the namespace is wrong. This is my service configuration : config/services.yml This is my doctrine configuration : config/packages/doctrine.yml

I've looked for the answers for 2 days everywhere (Documentation, tutorials, stackoverflow,...). Symfony 4 has really changed compared to Symfony 3. Thanks for the help. (Any link can help me)

6
  • In Symfony 4 use "bin/console make:entity User" Might need to install the new maker bundle with "composer require maker" And yes, lots of little changes in S4. Commented Feb 1, 2018 at 21:38
  • See this github.com/doctrine/DoctrineBundle/issues/729 as I guess will answer your problem. Commented Feb 1, 2018 at 21:47
  • Thanks Cerad, but unfortunaltely. I create my entity (the one w/ ORM annotation) with this command. Commented Feb 1, 2018 at 22:05
  • Thanks dlondero. I'm going to follow what I found in this URL : create a symfony 3.3 installation doctrine:mapping:import from existing database copy and paste Entity files in my new symfony 4 project (after change "AppBundle" with "App" in each one) doctrine:schema:update --force in new symfony 4 project Commented Feb 1, 2018 at 22:06
  • Makes sense if you need to use an existing database. If you're doing that just to have getters/setters created automagically you could just achieve that using a proper IDE. Commented Feb 2, 2018 at 7:50

3 Answers 3

0

Symfony 4 Src is default App folder.

Sign up to request clarification or add additional context in comments.

Comments

0

Composer also respects a memory limit defined by the COMPOSER_MEMORY_LIMIT environment variable. You can set it to COMPOSER_MEMORY_LIMIT=-1 and then run:

composer require doctrine maker

Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors

Comments

-3

I found the solution:

1) You must require doctrine maker through

composer require doctrine maker

2) You would like to create entities through this command below but make sure to achieve the 3rd and the 4th steps. Then go back here. Then you can go to the step #5

php bin/console make:entity YourEntity

3) Go to ' vendor/doctrine/doctrine-bundle/Mapping/DisconnecteMetadataFacroty.php' between the 150th and 170th lines. Then add the condition between the '///' slashes

#/vendor/doctrine/doctrine-bundle/mapping/disconnectedMetadataFactory.php

private function getBasePathForClass($name, $namespace, $path)
{
    $namespace = str_replace('\\', '/', $namespace);
    $search = str_replace('\\', '/', $path);
    $destination = str_replace('/'.$namespace, '', $search, $c);

    ///
    if ($namespace === 'App/Entity') {
        $destination = str_replace('/Entity', '', $search, $c);
    } else {
        $destination = str_replace('/'.$namespace, '', $search, $c);
    }
    ////


    if ($c != 1) {
        throw new \RuntimeException(sprintf('Can\'t find base path for "%s" (path: "%s", destination: "%s").', $name, $path, $destination));
    }

    return $destination;
}

4) Go to ' vendor/doctrine/orm/lib/Doctrine/ORM/Tools/EntityGenerator.php' between the 367th and 375th lines. Then replace the commented line by the two following ones.

#/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/EntityGenerator.php

/*$path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->extension;*/
    $metaNamePath = substr($metadata->name, 0, 4) === 'App\\' ? substr($metadata->name, 4) : $metadata->name;
    $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metaNamePath) . $this->extension;

5) Here you are, you can generate the getters and the setters through

php bin/console doctrine:generate:entities App:YourEntity

1 Comment

Modifying the vendor code should be avoided, it will be replaced when there is any update of the package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.