我有MoneyChanger
实体,它与AddTransaction
实体具有多对多关系。
我想从属于MoneyChanger
实体AddTransaction
实体中获取所有行。
以下是我的表的结构。
MoneyChanger
待定
id | created_at
----------------
1 | 2017-08-28
2 | 2017-08-28
3 | 2017-08-28
money_changer_add_transaction
待定
money_changer_id: | add_transaction_id
---------------------------------------
1 | 1
---------------------------------------
1 | 2
---------------------------------------
1 | 3
---------------------------------------
2 | 4
---------------------------------------
3 | 5
add_transaction
待定
id
---
1
2
3
4
5
这是我到目前为止所做的,但我没有运气。
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('MontealBundle:MoneyChanger');
$query = $repository->createQueryBuilder('p')
->where('p.createdAt BETWEEN :startDate AND :endDate')
->andWhere('p.type = :type')
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
->setParameter('type', $type)
->getQuery();
$records = $query->getResult();
foreach ($records as $record) {
echo "</b>".$record->getId()."</b></br>";
$transactions = $em->getRepository('MontealBundle:AddTransaction');
$qry = $repository->createQueryBuilder('a')
->innerJoin('a.money_changer_add_transaction', 'm')
->where('a.money_changer_id = :money_changer_id')
->setParameter('money_changer_id', $record->getId())
->getQuery();
$transactions = $qry->getResult();
dump($transactions);
foreach ($transactions as $trans) {
echo "</b>".$trans->getId()."</b></br>";
}
}
这是货币兑换商实体
/**
* @ORMEntity
* @ORMTable(name="money_changer")
* @ORMEntity(repositoryClass="MontealBundleRepositoryMoneyChangerRepository")
* @ORMHasLifecycleCallbacks()
*/
class MoneyChanger
{
/**
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMManyToMany(targetEntity="AddTransaction", inversedBy="moneyChanger", cascade={"persist"})
* @ORMJoinTable(name="money_changer_add_transaction",
* joinColumns={@ORMJoinColumn(name="money_changer_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="add_transaction_id", referencedColumnName="id", unique=true)}
* )
*/
private $addTransaction;
public function __construct()
{
$this->addTransaction = new ArrayCollection();
}
这是AddTransaction
实体
/**
* @ORMEntity
* @ORMTable(name="add_transaction")
*/
class AddTransaction
{
/**
* @ORMManyToMany(targetEntity="MoneyChanger", mappedBy="addTransaction", cascade={"persist"})
*
*/
private $moneyChanger;
public function __construct(){
$this->moneyChanger = new ArrayCollection();
}
错误:
Class MontealBundleEntityMoneyChanger has no association named money_changer_add_transaction
试试这个;
$qry = $repository->createQueryBuilder('a')
->innerJoin('a.addTransaction', 'm')
->where('a.id = :money_changer_id')
->setParameter('money_changer_id', $record->getId())
->getQuery();
要获取MoneyChanger
对象的相关事务,您的第一个查询生成器很好,当您循环访问这些记录时,它会为您提供MoneyChanger
对象 无需再次运行查询生成器来获取相关的AddTransaction
对象,因为每个MoneyChanger
对象都将具有相关AddTransaction
对象的集合, 你只需要调用 getter 方法并在AddTransaction
对象上循环,例如
$records = $query->getResult();
foreach ($records as $record) {
/* your other code */
$transactions = $record->getAddTransaction();
foreach ($transactions as $trans) {
echo "</b>".$trans->getId()."</b></br>";
}
}
还要确保MoneyChanger
类应具有以下方法,该方法将具有相关AddTransaction
对象的ArrayCollection
public function getAddTransaction()
{
return $this->addTransaction;
}
参考文献 5.9.多对多,双向