Symfony 中的多数据库连接查询
Multiple database join query in Symfony
我是 symfony 的新手,我需要从多个数据库加入查询。我遇到过很多类似的问题,但我无法解决这个问题。我尝试在存储库中加入查询,但出现错误“在链配置的命名空间 App\Entity\Steuer\StUserSendAuth 中找不到 class 'App\Entity\customerdata\CustomerAction'”。
下面我添加了代码,使用。
我的doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
steuer:
# configure these for your database server
url: '%env(resolve:DATABASE_STEUER_URL)%'
driver:
kundendaten:
# configure these for your database server
url: '%env(resolve:DATABASE_CUSTOMERDATA_URL)%'
driver:
orm:
default_entity_manager: default
entity_managers:
steuer:
connection: steuer
mappings:
Steuer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Steuer'
prefix: 'App\Entity\Steuer'
alias: Steuern
customerdata:
connection: customerdata
mappings:
Customerdata:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customerdata'
prefix: 'App\Entity\Customerdata'
alias: Customerdata
第一个数据库实体:-
namespace App\Entity\Steuer;
use Doctrine\ORM\Mapping as ORM;
/**
* StUserSendAuth
* @ORM\Entity(repositoryClass="App\Repository\Steuern\StUserSendAuthRepository")
* @ORM\Table(name="st_user_send_auth")
*/
class StUserSendAuth
{
/**
* @var int
*
* @ORM\Column(name="abgabe_sendauth_id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $abgabeSendauthId = '0';
/**
* @var int
*
* @ORM\Column(name="customeraction_id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $CustomeractionId = '0';
第二个数据库连接实体:-
namespace App\Entity\customerdata;
use Doctrine\ORM\Mapping as ORM;
/**
* CustomerAction
* @ORM\Entity(repositoryClass="App\Repository\customerdata\CustomerActionRepository")
* @ORM\Table(name="customer_action")
*/
class CustomerAction
{
/**
* @var int
*
* @ORM\Column(name="customeraction_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $CustomeractionId;
仓库加入查询:-
namespace App\Repository\Steuer;
use App\Entity\Steuer\StUserSendAuth;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method StUserSendAuth|null find($id, $lockMode = null, $lockVersion = null)
* @method StUserSendAuth|null findOneBy(array $criteria, array $orderBy = null)
* @method StUserSendAuth[] findAll()
* @method StUserSendAuth[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
* @method StUserSendAuth|null findstatistikOnline()
*/
class StUserSendAuthRepository extends ServiceEntityRepository
{
/**
* @return StUserSendAuth[] Returns an array of StUserSendAuth objects
*/
public function findstatistikOnline()
{
return $this->getEntityManager()
->createQueryBuilder('s')
->select('p.updatedAt')
->from('App\Entity\Steuern\StUserSendAuth', 'p')
->leftJoin('App\Entity\customerdata\CustomerAction', 'uas', 'WITH', 'uas.CustomeractionId = p.CustomeractionId')
->getQuery()
->getArrayResult()
;
控制器class:-
namespace App\Controller;
use App\Repository\Steuer\StUserSendAuthRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class OnlineAuswertungController extends AbstractController
{
#[Route('/count_statistik/onlineAuswertung', name: 'onlineAuswertung')]
public function index(StUserSendAuthRepositoryRepository $StUserSendAuthRepositoryRepository): Response
{
$Table = $StUserSendAuthRepositoryRepository->findstatistikOnline();
return $this->render('online_auswertung/index.html.twig', [
'controller_name' => 'OnlineAuswertungController',
'table' => $Table,
]);
}
}
请告诉我如何在多个数据库上进行查询。谢谢!
1.IF你想实现数据库的多连接请参考这个文档。
https://symfony.com/doc/current/doctrine/multiple_entity_managers.html.
请通过架构验证来验证您在项目中创建的实体。
如果您尝试从相同的 database.then 中获取两个不同的 table 语法是 here
示例:
...
$this->getEntityManager()
->createQueryBuilder('s')
->select('p.updatedAt')
->from('Steuern:StUserSendAuth', 'p')
->leftJoin('customerdata:CustomerAction', 'uas', 'WITH', 'uas.CustomeractionId = p.CustomeractionId')
->getQuery()
->getArrayResult()
...
注意:
请检查这个 reference
终于找到symfony多数据库查询的解决方法了
- 首先需要在所有实体 class.
中将 table 名称重命名为 databasename.tablename
namespace App\Entity\Steuer;
use Doctrine\ORM\Mapping as ORM;
/**
* StUserSendAuth
* @ORM\Entity(repositoryClass="App\Repository\Steuer\StUserSendAuthRepository")
* @ORM\Table(name="steuer.st_user_send_auth")
*/
class StUserSendAuth
{
/**
* @var int
*
* @ORM\Column(name="abgabe_sendauth_id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $abgabeSendauthId = '0';
/**
* @var int
*
* @ORM\Column(name="customeraction_id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $CustomeractionId = '0';
- 我尝试使用 raw_sql 在控制器中使用。
class OnlineAuswertungController extends AbstractController
{
#[Route('/count_statistik/onlineAuswertung', name: 'onlineAuswertung')]
public function index(Connection $connection): Response
{
$query = "SELECT p.updated_at
FROM steuer.st_user_pdfs_sendauth p
LEFT JOIN customerdata.CustomerAction sa on sa. customeraction_id = p.customeraction_id"
$statement = $connection->prepare($query);
$statement->execute();
return $this->render('online_auswertung/index.html.twig', [
'controller_name' => 'OnlineAuswertungController',
'table' => $Table,
]);
}
}
我无法使用 $statement->fetchAll();因为它显示 fetchAll() 已被弃用。现在这段代码工作正常。
我是 symfony 的新手,我需要从多个数据库加入查询。我遇到过很多类似的问题,但我无法解决这个问题。我尝试在存储库中加入查询,但出现错误“在链配置的命名空间 App\Entity\Steuer\StUserSendAuth 中找不到 class 'App\Entity\customerdata\CustomerAction'”。 下面我添加了代码,使用。
我的doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
steuer:
# configure these for your database server
url: '%env(resolve:DATABASE_STEUER_URL)%'
driver:
kundendaten:
# configure these for your database server
url: '%env(resolve:DATABASE_CUSTOMERDATA_URL)%'
driver:
orm:
default_entity_manager: default
entity_managers:
steuer:
connection: steuer
mappings:
Steuer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Steuer'
prefix: 'App\Entity\Steuer'
alias: Steuern
customerdata:
connection: customerdata
mappings:
Customerdata:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customerdata'
prefix: 'App\Entity\Customerdata'
alias: Customerdata
第一个数据库实体:-
namespace App\Entity\Steuer;
use Doctrine\ORM\Mapping as ORM;
/**
* StUserSendAuth
* @ORM\Entity(repositoryClass="App\Repository\Steuern\StUserSendAuthRepository")
* @ORM\Table(name="st_user_send_auth")
*/
class StUserSendAuth
{
/**
* @var int
*
* @ORM\Column(name="abgabe_sendauth_id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $abgabeSendauthId = '0';
/**
* @var int
*
* @ORM\Column(name="customeraction_id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $CustomeractionId = '0';
第二个数据库连接实体:-
namespace App\Entity\customerdata;
use Doctrine\ORM\Mapping as ORM;
/**
* CustomerAction
* @ORM\Entity(repositoryClass="App\Repository\customerdata\CustomerActionRepository")
* @ORM\Table(name="customer_action")
*/
class CustomerAction
{
/**
* @var int
*
* @ORM\Column(name="customeraction_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $CustomeractionId;
仓库加入查询:-
namespace App\Repository\Steuer;
use App\Entity\Steuer\StUserSendAuth;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method StUserSendAuth|null find($id, $lockMode = null, $lockVersion = null)
* @method StUserSendAuth|null findOneBy(array $criteria, array $orderBy = null)
* @method StUserSendAuth[] findAll()
* @method StUserSendAuth[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
* @method StUserSendAuth|null findstatistikOnline()
*/
class StUserSendAuthRepository extends ServiceEntityRepository
{
/**
* @return StUserSendAuth[] Returns an array of StUserSendAuth objects
*/
public function findstatistikOnline()
{
return $this->getEntityManager()
->createQueryBuilder('s')
->select('p.updatedAt')
->from('App\Entity\Steuern\StUserSendAuth', 'p')
->leftJoin('App\Entity\customerdata\CustomerAction', 'uas', 'WITH', 'uas.CustomeractionId = p.CustomeractionId')
->getQuery()
->getArrayResult()
;
控制器class:-
namespace App\Controller;
use App\Repository\Steuer\StUserSendAuthRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class OnlineAuswertungController extends AbstractController
{
#[Route('/count_statistik/onlineAuswertung', name: 'onlineAuswertung')]
public function index(StUserSendAuthRepositoryRepository $StUserSendAuthRepositoryRepository): Response
{
$Table = $StUserSendAuthRepositoryRepository->findstatistikOnline();
return $this->render('online_auswertung/index.html.twig', [
'controller_name' => 'OnlineAuswertungController',
'table' => $Table,
]);
}
}
请告诉我如何在多个数据库上进行查询。谢谢!
1.IF你想实现数据库的多连接请参考这个文档。 https://symfony.com/doc/current/doctrine/multiple_entity_managers.html.
请通过架构验证来验证您在项目中创建的实体。
如果您尝试从相同的 database.then 中获取两个不同的 table 语法是 here
示例:
...
$this->getEntityManager()
->createQueryBuilder('s')
->select('p.updatedAt')
->from('Steuern:StUserSendAuth', 'p')
->leftJoin('customerdata:CustomerAction', 'uas', 'WITH', 'uas.CustomeractionId = p.CustomeractionId')
->getQuery()
->getArrayResult()
...
注意: 请检查这个 reference
终于找到symfony多数据库查询的解决方法了
- 首先需要在所有实体 class. 中将 table 名称重命名为 databasename.tablename
namespace App\Entity\Steuer;
use Doctrine\ORM\Mapping as ORM;
/**
* StUserSendAuth
* @ORM\Entity(repositoryClass="App\Repository\Steuer\StUserSendAuthRepository")
* @ORM\Table(name="steuer.st_user_send_auth")
*/
class StUserSendAuth
{
/**
* @var int
*
* @ORM\Column(name="abgabe_sendauth_id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $abgabeSendauthId = '0';
/**
* @var int
*
* @ORM\Column(name="customeraction_id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*/
private $CustomeractionId = '0';
- 我尝试使用 raw_sql 在控制器中使用。
class OnlineAuswertungController extends AbstractController
{
#[Route('/count_statistik/onlineAuswertung', name: 'onlineAuswertung')]
public function index(Connection $connection): Response
{
$query = "SELECT p.updated_at
FROM steuer.st_user_pdfs_sendauth p
LEFT JOIN customerdata.CustomerAction sa on sa. customeraction_id = p.customeraction_id"
$statement = $connection->prepare($query);
$statement->execute();
return $this->render('online_auswertung/index.html.twig', [
'controller_name' => 'OnlineAuswertungController',
'table' => $Table,
]);
}
}
我无法使用 $statement->fetchAll();因为它显示 fetchAll() 已被弃用。现在这段代码工作正常。