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

标签 php mysql symfony

您好,我正在使用 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');   
    }

fixture.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
        }
    }
}

最佳答案

我试图遵循 KNP Symfony video tutorial并在 Nelmio/Alice 上迷路了 repo 。 Bogdan's答案对我有用。

您需要添加Alice Data Fixtures repo 协议(protocol)。

添加后 这是给所有迷路的人的快速指南:

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);
   }
}

关于php - DataFixture 不使用 nelmio/alice 保存(带有 flex 的 sf 3.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47014496/

相关文章:

php - 如何使用 ExcelBundle [Symfony Bundle] 读取日期

php - 打开所需文件失败

php - 动态pdf需要直接打开打印框

php - 数据库存储与 Cookies - 存储表单数据

php - 我想在回显表单元格中放置一个按钮

mysql - 为过程传入表

mysql - 查询在 Oracle 中工作而不在 Mysql 中工作

php - Symfony3 创建服务错误文件但类不在其中

php - 如何从 Symfony 中的 Sentry 中排除异常?

php - Symfony2 : Unable to use DoctrineExtensions Mysql Year function