gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:43:16 30 4
gpt4 key购买 nike

您好,出于学习目的,我正在使用 flex 与 Symfony 一起工作。在我安装了一些 recipes 之后,我想添加 nelmio/alice 来为 doctrine fixtures 生成假数据,但是在我加载 fixtures 之后,没有数据保存到 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
}
}
}

最佳答案

我试图跟随 KNP Symfony video tutorial迷路了 Nelmio/Alice repo 。 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);
}
}

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com