DataFixture不使用nelmio / alice保存(带有flex的sf 3.3)

问题描述 投票:2回答:2

您好我正在使用flex与Symfony合作进行学习。在我安装了一些食谱之后,我想添加nelmio / alice来为教条夹具生成假数据但是在加载灯具之后没有数据保存到mysql中。我有什么想法,我做错了什么?

<?php  
namespace App\DataFixtures\ORM;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Nelmio\Alice\Loader\NativeLoader;

class LoadFixtures extends Fixture
{
    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param ObjectManager $manager
     */
    public function load(ObjectManager $manager)
    {   
        $loader = new NativeLoader();
        $obj = $loader->loadFile(__DIR__ . 'fixtures.yml');   
    }

fixtures.yml:

App\Entity\BaseUser:
    user{1..10}:
        email: <email()>

BaseUser实体

<?php
namespace App\Entity;


use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * Class BaseUser
 * @package App\Entity
 * @ORM\Entity
 * @ORM\Table()
 */
class BaseUser implements AdvancedUserInterface
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     * @return BaseUser
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param mixed $email
     * @return BaseUser
     */
    public function setEmail($email)
    {
        $this->email = $email;
        return $this;
    }

    /**
     * Checks whether the user's account has expired.
     *
     * Internally, if this method returns false, the authentication system
     * will throw an AccountExpiredException and prevent login.
     *
     * @return bool true if the user's account is non expired, false otherwise
     *
     * @see AccountExpiredException
     */
    public function isAccountNonExpired()
    {
        // TODO: Implement isAccountNonExpired() method.
    }

    /**
     * Checks whether the user is locked.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a LockedException and prevent login.
     *
     * @return bool true if the user is not locked, false otherwise
     *
     * @see LockedException
     */
    public function isAccountNonLocked()
    {
        // TODO: Implement isAccountNonLocked() method.
    }

    /**
     * Checks whether the user's credentials (password) has expired.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a CredentialsExpiredException and prevent login.
     *
     * @return bool true if the user's credentials are non expired, false otherwise
     *
     * @see CredentialsExpiredException
     */
    public function isCredentialsNonExpired()
    {
        // TODO: Implement isCredentialsNonExpired() method.
    }

    /**
     * Checks whether the user is enabled.
     *
     * Internally, if this method returns false, the authentication system
     * will throw a DisabledException and prevent login.
     *
     * @return bool true if the user is enabled, false otherwise
     *
     * @see DisabledException
     */
    public function isEnabled()
    {
        // TODO: Implement isEnabled() method.
    }

    /**
     * Returns the roles granted to the user.
     *
     * <code>
     * public function getRoles()
     * {
     *     return array('ROLE_USER');
     * }
     * </code>
     *
     * Alternatively, the roles might be stored on a ``roles`` property,
     * and populated in any number of different ways when the user object
     * is created.
     *
     * @return (Role|string)[] The user roles
     */
    public function getRoles()
    {
        return ['ROLE_USER'];
    }

    /**
     * Returns the password used to authenticate the user.
     *
     * This should be the encoded password. On authentication, a plain-text
     * password will be salted, encoded, and then compared to this value.
     *
     * @return string The password
     */
    public function getPassword()
    {
        // TODO: Implement getPassword() method.
    }

    /**
     * Returns the salt that was originally used to encode the password.
     *
     * This can return null if the password was not encoded using a salt.
     *
     * @return string|null The salt
     */
    public function getSalt()
    {
        // TODO: Implement getSalt() method.
    }

    /**
     * Returns the username used to authenticate the user.
     *
     * @return string The username
     */
    public function getUsername()
    {
        return $this->email;
    }

    /**
     * Removes sensitive data from the user.
     *
     * This is important if, at any given point, sensitive information like
     * the plain-text password is stored on this object.
     */
    public function eraseCredentials()
    {
        // TODO: Implement eraseCredentials() method.
    }
}

bundles.php

<?php

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
    Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
    Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['all' => true],
];

并在frameworky.yml我添加了这个

nelmio_alice:
    locale: 'en_US' # Default locale for the Faker Generator
    seed: 6399 # Value used make sure Faker generates data consistently across
            # runs, set to null to disable.
    functions_blacklist: # Some Faker formatter may have the same name as PHP
        - 'current'      # native functions. PHP functions have the priority,
                         # so if you want to use a Faker formatter instead,
                         # blacklist this function here
    loading_limit: 5 # Alice may do some recursion to resolve certain values.
                     # This parameter defines a limit which will stop the
                     # resolution once reached.
    max_unique_values_retry: 150 # Maximum number of time Alice can try to
                                   # generate a unique value before stopping and
                                   # failing.

composer.yml

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.0.8",
        "doctrine/doctrine-fixtures-bundle": "^2.4",
        "doctrine/doctrine-migrations-bundle": "^1.2",
        "sensio/framework-extra-bundle": "^5.0",
        "sensiolabs/security-checker": "^4.1",
        "symfony/console": "^3.3",
        "symfony/framework-bundle": "^3.3",
        "symfony/orm-pack": "^1.0",
        "symfony/security-core": "^3.3",
        "symfony/yaml": "^3.3"
    },
    "require-dev": {
        "nelmio/alice": "^3.1",
        "symfony/dotenv": "^3.3",
        "symfony/flex": "^1.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd",
            "security-checker security:check": "script"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*",
        "symfony/twig-bundle": "<3.3",
        "symfony/debug": "<3.3"
    },
    "extra": {
        "symfony": {
            "id": "01BX9RZX7RBK5CNHP741EVCXB5",
            "allow-contrib": false
        }
    }
}
php mysql symfony
2个回答
1
投票

我通过安装theofidry / alice-data-fixture解决了这个问题,因为看起来nelmio / alice 3.x与旧版本的工作方式不同。 (它不再处理数据库查询)。

LE:也许这是我的错,但是自从sf 4正式发布以来,我对装载部分有一些问题。这是一个适合任何人的修复你需要这样的服务来加载灯具

App\DataFixtures\ORM\DataFixtures:
    arguments: ['@fidry_alice_data_fixtures.doctrine.purger_loader']
    calls:
        - [load, ['@doctrine.orm.entity_manager']]
    tags: [doctrine.fixture.orm]

并在方法中

    $files = [
        __DIR__ . '/fixtures.yml',
    ];
    $this->loader->load($files);

0
投票

我试图跟随KNP Symfony video tutorial并迷失在Nelmio/Alice回购。 Bogdan's的回答对我有用。

你需要添加Alice Data Fixtures repo。

添加之后这是一个快速指南,为所有失去的人:

namespace AppBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DataFixtures extends Controller implements FixtureInterface
{
   public function load(ObjectManager $manager)
   {
    //File Path/s needs to be in array
    $dummyFilePath =[__DIR__.'/DataFixtures.yml'];
    //get fidry loader
    $loader = $this->get('fidry_alice_data_fixtures.doctrine.loader');
    //execute
    $loader->load($dummyFilePath);
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.