Symfony Doctrine 多对多获取数据



我有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.多对多,双向

相关内容

  • 没有找到相关文章

最新更新