我有MoneyChanger
实体与AddTransaction
实体有多对多关系。
我希望获取属于AddTransaction
实体的MoneyChanger
实体的所有行。
这是我桌子的结构。
MoneyChanger
tbl
id | created_at
----------------
1 | 2017-08-28
2 | 2017-08-28
3 | 2017-08-28
money_changer_add_transaction
tbl
money_changer_id: | add_transaction_id
---------------------------------------
1 | 1
---------------------------------------
1 | 2
---------------------------------------
1 | 3
---------------------------------------
2 | 4
---------------------------------------
3 | 5
add_transaction
tbl
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>";
}
}
这是MoneyChanger实体
/**
* @ORM\Entity
* @ORM\Table(name="money_changer")
* @ORM\Entity(repositoryClass="MontealBundle\Repository\MoneyChangerRepository")
* @ORM\HasLifecycleCallbacks()
*/
class MoneyChanger
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="AddTransaction", inversedBy="moneyChanger", cascade={"persist"})
* @ORM\JoinTable(name="money_changer_add_transaction",
* joinColumns={@ORM\JoinColumn(name="money_changer_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="add_transaction_id", referencedColumnName="id", unique=true)}
* )
*/
private $addTransaction;
public function __construct()
{
$this->addTransaction = new ArrayCollection();
}
这是AddTransaction
实体
/**
* @ORM\Entity
* @ORM\Table(name="add_transaction")
*/
class AddTransaction
{
/**
* @ORM\ManyToMany(targetEntity="MoneyChanger", mappedBy="addTransaction", cascade={"persist"})
*
*/
private $moneyChanger;
public function __construct(){
$this->moneyChanger = new ArrayCollection();
}
错误:
Class MontealBundle\Entity\MoneyChanger has no association named money_changer_add_transaction
答案 0 :(得分:0)
试试这个;
$qry = $repository->createQueryBuilder('a')
->innerJoin('a.addTransaction', 'm')
->where('a.id = :money_changer_id')
->setParameter('money_changer_id', $record->getId())
->getQuery();
答案 1 :(得分:0)
要获取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
课程具有以下方法,该方法将包含ArrayCollection
个相关AddTransaction
个对象
public function getAddTransaction()
{
return $this->addTransaction;
}