gpt4 book ai didi

php - 使用 parameter.yml 值的 Symfony 3 Datafixtures

转载 作者:行者123 更新时间:2023-12-04 02:06:24 26 4
gpt4 key购买 nike

我在我的 User 数据装置中使用 LDAP,我不想硬编码 LDAP 登录选项。最初,我试过这个:

$options = array(
'host' => '%ldap_host%',
'port' => '%ldap_port%',
'useSsl' => true,
'username' => '%ldap_username%',
'password' => '%ldap_password%',
'baseDn' => '%ldap_baseDn_users%'
);

但这没有用。我做了一些研究,意识到我需要 include the container in my fixtures .但是,此时我不确定我的下一步是什么。

据我所知,我需要使用容器,它是 get 方法来获取包含参数的服务,但我不知道那是什么:

$this->container->get('parameters');

不起作用,所以我想知道我应该使用什么。

我的完整数据夹具如下:

class LoadFOSUsers extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}


public function load(ObjectManager $manager)
{
$this->container->get('parameters');

// Not sure how to access param values.
$options = array(
'host' => '%ldap_host%',
'port' => '%ldap_port%',
'useSsl' => true,
'username' => '%ldap_username%',
'password' => '%ldap_password%',
'baseDn' => '%ldap_baseDn_users%'
);

$ldap = new Ldap($options);
$ldap->bind();

$baseDn = '%ldap_baseDn_users%';
$filter = '(&(&(ObjectClass=user))(samaccountname=*))';
$attributes=['samaccountname', 'dn', 'mail','memberof'];
$result = $ldap->searchEntries($filter, $baseDn, Ldap::SEARCH_SCOPE_SUB, $attributes);

foreach ($result as $item) {
echo $item["dn"] . ': ' . $item['samaccountname'][0] . PHP_EOL;
}
}

public function getOrder()
{
// the order in which fixtures will be loaded
// the lower the number, the sooner that this fixture is loaded
return 8;
}
}

最佳答案

您只需通过 getParameter('name') 从容器中获取它们,或者通过 getParameterBag() 将它们全部放入包中。

所以:

    $options = array(
'host' => $this->container->getParameter('ldap_host'),
'port' => $this->container->getParameter('ldap_port'),
'useSsl' => true,
'username' => $this->container->getParameter('ldap_username'),
'password' => $this->container->getParameter('ldap_password'),
'baseDn' => $this->container->getParameter('ldap_baseDn_users')
);

等等

关于php - 使用 parameter.yml 值的 Symfony 3 Datafixtures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43107608/

26 4 0