Alice捆绑创建自定义faker

时间:2017-11-06 20:26:54

标签: symfony alice-fixtures

我使用alice-bundle并拥有一些灯具

AppBundle\Entity\Admin:
admin{1..3}:
    username: admin<current()>
    firstName: <firstName('male')>
    lastName: <lastName()>
    password: <encodePassword(@self, @self->username)>

在旧版本中,我创建FixtureLoader类,从DataFixtureLoader延伸,只为我的自定义faker创建公共函数encodePassword,我猜非常简单

use Hautelook\AliceBundle\Alice\DataFixtureLoader;
use Symfony\Component\Security\Core\User\UserInterface;
class FixtureLoader extends DataFixtureLoader
{
/**
 * {@inheritDoc}
 */
protected function getFixtures()
{
    $prefix = __DIR__ . '/../Fixture/';
    return [
        $prefix . 'admin.yml',
        $prefix . 'city.yml',
        $prefix . 'country.yml',
        $prefix . 'location.yml',
        $prefix . 'user.yml',
        $prefix . 'voteSetting.yml',            
        $prefix . 'project.yml'
    ];
}

/**
 * @param UserInterface $user
 * @param $plainPassword
 * @return string
 */
public function encodePassword(UserInterface $user, $plainPassword)
{
    return $this->container->get('security.password_encoder')->encodePassword($user, $plainPassword);
}
}

但是atm,当我安装捆绑包时,我感到很惊讶,DataFixtureLoader未找到,我猜不推荐。我的问题,如何创建我的自定义faker?

1 个答案:

答案 0 :(得分:0)

U起诉"hautelook/alice-bundle": "^1.3.1"并从抽象类扩展

use Hautelook\AliceBundle\Doctrine\DataFixtures\AbstractLoader;
use Nelmio\Alice\Fixtures;
use Symfony\Component\Security\Core\User\UserInterface;

class FixtureLoader extends AbstractLoader
{
/**
 * {@inheritdoc}
 */
public function getFixtures()
{
    return [
        __DIR__.'/../Fixtures/admin.yml',
    ];
}

/**
 * @param UserInterface $user
 * @param $plainPassword
 *
 * @return string
 */
public function encodePassword(UserInterface $user, $plainPassword)
{
    return $this->container->get('security.password_encoder')->encodePassword($user, $plainPassword);
}