【问题标题】:DataFixture aren't saved using nelmio/alice ( sf 3.3 with flex)DataFixture 不使用 nelmio/alice 保存(带有 flex 的 sf 3.3)
【发布时间】:2017-10-30 11:46:56
【问题描述】:

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

基本用户实体

<?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


    【解决方案1】:

    我试图关注 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);
       }
    }
    

    【讨论】:

      【解决方案2】:

      我通过安装 theofidry/alice-data-fixtures 解决了这个问题,因为 nelmio/alice 3.x 似乎与旧版本不一样。 (它不再负责数据库查询)。

      乐: 也许这是我的错,但自从 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);
      

      【讨论】:

        猜你喜欢
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-07
        • 2019-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多