PHP/Symfony 2 - 从数据库中获取数据,编辑它并重新发送到另一台服务器
PHP/Symfony 2 - Fetch data from db, edit it and resend to another server
在我的 symfony 2 应用程序中,我想通过使用控制台命令从数据库 table 中获取数据。循环遍历我想稍微改变它们(例如乘以一些值)然后将它发送到另一台服务器上的数据库(通过 curl)。
如何设置列的新名称并将这些数据分配给它们?以及如何将其作为 .sql 文件发送?
这里只是为了给你一个简短的概念,这是我的命令的原始版本:
class MyCommand extends ContainerAwareCommand
{
private $input;
private $output;
protected function configure()
{
// my configs
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$entityManager = $this->getContainer()->get('doctrine')->getManager('default');
$queryBuilder = $entityManager->createQueryBuilder();
$query = $queryBuilder
-> // my query params
foreach ($query->iterate() as $e)
{
// loop through results of the query in order to create a new DB table
}
$this->sendData($output);
}
}
提前感谢您的帮助和建议!
生成一个 sql 文件并通过 curl 发送到另一台服务器执行它似乎根本不是最好的方法。如果其他人发送恶意 sql 文件在服务器上执行?
Doctrine 提供了连接多个数据库的功能。也许您可以使用此功能而不是发送 sql 文件。
回答你的问题,在你的循环语句中,你可以生成你需要的 INSERTS/UPDATES
sql 语句,并使用 fwrite
[= 之类的函数将每个语句保存在文本文件中13=]
您可以创建另一个连接到不同数据库的实体管理器。您可以 see that here,这是一个相当简单的 yaml 配置。
只需带上两个实体管理器(您的 'default' 和您的 'other'),然后获取您需要的数据并保存/刷新到您的其他数据库。
像这样:
class MyCommand extends ContainerAwareCommand
{
private $input;
private $output;
protected function configure()
{
// my configs
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$entityManager = $this->getContainer()->get('doctrine')->getManager('default');
$otherEntityManager = $this->getContainer()->get('doctrine')->getManager('other');
$queryBuilder = $entityManager->createQueryBuilder();
$query = $queryBuilder
-> // my query params
foreach ($query->iterate() as $e)
{
// loop through results
$otherEntity = new OtherEntity()
->setFirstProperty($first)
->setSecondProperty($second)
;
$otherEntityManaer->persist($otherEntity);
}
$otherEntityManager->flush();
}
}
在我的 symfony 2 应用程序中,我想通过使用控制台命令从数据库 table 中获取数据。循环遍历我想稍微改变它们(例如乘以一些值)然后将它发送到另一台服务器上的数据库(通过 curl)。
如何设置列的新名称并将这些数据分配给它们?以及如何将其作为 .sql 文件发送?
这里只是为了给你一个简短的概念,这是我的命令的原始版本:
class MyCommand extends ContainerAwareCommand
{
private $input;
private $output;
protected function configure()
{
// my configs
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$entityManager = $this->getContainer()->get('doctrine')->getManager('default');
$queryBuilder = $entityManager->createQueryBuilder();
$query = $queryBuilder
-> // my query params
foreach ($query->iterate() as $e)
{
// loop through results of the query in order to create a new DB table
}
$this->sendData($output);
}
}
提前感谢您的帮助和建议!
生成一个 sql 文件并通过 curl 发送到另一台服务器执行它似乎根本不是最好的方法。如果其他人发送恶意 sql 文件在服务器上执行?
Doctrine 提供了连接多个数据库的功能。也许您可以使用此功能而不是发送 sql 文件。
回答你的问题,在你的循环语句中,你可以生成你需要的 INSERTS/UPDATES
sql 语句,并使用 fwrite
[= 之类的函数将每个语句保存在文本文件中13=]
您可以创建另一个连接到不同数据库的实体管理器。您可以 see that here,这是一个相当简单的 yaml 配置。
只需带上两个实体管理器(您的 'default' 和您的 'other'),然后获取您需要的数据并保存/刷新到您的其他数据库。
像这样:
class MyCommand extends ContainerAwareCommand
{
private $input;
private $output;
protected function configure()
{
// my configs
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$entityManager = $this->getContainer()->get('doctrine')->getManager('default');
$otherEntityManager = $this->getContainer()->get('doctrine')->getManager('other');
$queryBuilder = $entityManager->createQueryBuilder();
$query = $queryBuilder
-> // my query params
foreach ($query->iterate() as $e)
{
// loop through results
$otherEntity = new OtherEntity()
->setFirstProperty($first)
->setSecondProperty($second)
;
$otherEntityManaer->persist($otherEntity);
}
$otherEntityManager->flush();
}
}