我正在使用symfony2进行项目。我有一个date_started和date_ended,我打算使用BETWEEN而不是> =和< =。我只是想问一下如何使用symfony2实现它?因为我的工作不起作用。这是我的示例代码:
存储库:
public function getAllTransaction($rNumber,$gNumber,$senderId,$receiverId,$docType,$direction,$startDate,$endDate,$page = 1, $limit = 10) {
$em = $this->getEntityManager();
$query = $em->createQuery(
'SELECT partial a.{supplierTransactionDetailId, rNumber, pNumber, invNumber, anNumber, adNumber, amount},
partial b.{supplierTransactionId, transactionDate, senderId, receiverId, isaNumber, gsNumber, fileName},
partial c.{supplierDocTypeId, docType, direction}
FROM SupplierBundle:SupplierTransactionDetail a
JOIN a.supplierTransaction b
JOIN b.supplierDocType c
WHERE (a.rNumber LIKE :rNumber OR a.pNumber LIKE :rNumber
OR a.invNumber LIKE :rNumber
OR a.anNumber LIKE :rNumber
OR a.adNumber LIKE :rNumber )
AND b.gsNumber LIKE :gsNumber
AND b.senderId LIKE :senderId
AND b.receiverId LIKE :receiverId
AND c.docType LIKE :docType
AND c.direction LIKE :direction
AND b.transactionDate BETWEEN :startDate AND :endDate
ORDER BY b.transactionDate desc'
)
->setParameter('rNumber', "%$rNumber%")
->setParameter('gsNumber', "%$gsNumber%")
->setParameter('senderId', "%$senderId%")
->setParameter('receiverId' , "%$receiverId%")
->setParameter('docType',"%$docType%")
->setParameter('direction', "%$direction%")
->setParameter('startDate', "%$startDate%")
->setParameter('endDate', "%$endDate%")
->setFirstResult(($page - 1) *$limit )
->setMaxResults($limit);
$paginator = new Paginator ($query, $fetchJoinCollection = false );
$paginator->setUseOutputWalkers(false);
return $paginator;
}
此代码显示所有带分页的交易,也可以搜索,但是当我将最后一部分放在 transactionDate with Between it doesn't display nothing.
时。
控制器:
public function trackAction(Request $request,$page){
$rNumber = $request->query->get('rNumber');
$gsNumber = $request->query->get('gsNumber');
$senderId = $request->query->get('senderId');
$receiverId = $request->query->get('receiverId');
$docType = $request->query->get('docType');
$direction = $request->query->get('direction');
$startDate = $request->query->get('startDate');
$endDate = $request->query->get('endDate');
$max_records = 10;
$em = $this->getDoctrine()->getManager();
$trans = $em->getRepository('SupplierBundle:SupplierTransactionDetail')->getAllTransaction($rNumber,$gsNumber,$senderId,$receiverId,$docType,$direction,$startDate,$endDate,$page , 10);
$count = $trans->count();
$pagination = array(
'page' => $page,
'route' => 'supplier_tracking',
'route_params' => array()
);
if ($max_records > 0 ){
$pagination['pages_count'] = max(ceil($count / $max_records), 1);
}
return $this->render('SupplierBundle:Supplier:tracking.html.twig',array('trans' => $trans, 'pagination' => $pagination));
}
更新
在我的数据库中,transactionDate的格式如下:
2015-05-18 18:10:00因为它是日期时间。在我的形式,,, startdate和enddate是日期的输入类型。
答案 0 :(得分:2)
现在您已根据@EdCottrell回答删除了BETWEEN语法的LIKE。我认为您还需要更改这两行以删除LIKE语法中使用的%
。
->setParameter('startDate', "$startDate")
->setParameter('endDate', "$endDate")
你真的需要一切LIKE
这样的。这一切似乎都有些毛茸茸。由于大多数字段似乎都是数字,我怀疑LIKE语法实际上是必需的,尽管我只是猜测它们是来自它们名字的数字。
重新评论数字
然后尝试这个,删除所有喜欢并将值作为数字而不是字符串传递。
public function getAllTransaction(
$rNumber,$gNumber,$senderId,$receiverId,
$docType,$direction,$startDate,$endDate,
$page = 1, $limit = 10
)
{
$em = $this->getEntityManager();
$query = $em->createQuery(
'SELECT partial a.{supplierTransactionDetailId, rNumber, pNumber, invNumber, anNumber, adNumber, amount},
partial b.{supplierTransactionId, transactionDate, senderId, receiverId, isaNumber, gsNumber, fileName},
partial c.{supplierDocTypeId, docType, direction}
FROM SupplierBundle:SupplierTransactionDetail a
JOIN a.supplierTransaction b
JOIN b.supplierDocType c
WHERE ( a.rNumber = :rNumber
OR a.pNumber = :rNumber
OR a.invNumber = :rNumber
OR a.anNumber = :rNumber
OR a.adNumber = :rNumber )
AND b.gsNumber = :gsNumber
AND b.senderId = :senderId
AND b.receiverId = :receiverId
AND c.docType = :docType
AND c.direction = :direction
AND b.transactionDate BETWEEN :startDate AND :endDate
ORDER BY b.transactionDate desc'
)
->setParameter('rNumber', $rNumber)
->setParameter('gsNumber', $gsNumber)
->setParameter('senderId', $senderId)
->setParameter('receiverId' , $receiverId)
->setParameter('docType', $docType)
->setParameter('direction', $direction)
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
->setFirstResult(($page - 1) *$limit )
->setMaxResults($limit);
// add this debug code to check the generated query
// you could try and run this using phpMyadmin or
// whatever mysql tools you use. If taht gets errors
// come back here an mod the generator
echo $query->getSQL();
$paginator = new Paginator ($query, $fetchJoinCollection = false );
$paginator->setUseOutputWalkers(false);
return $paginator;
}
RE:我发现了
这是echo $query->getSQL();
的结果,是QueryBuilder从您的所有输入中生成的查询。
我会将其复制/粘贴到像phpMyAdmin或用于查看mysql数据库的任何工具中,将?
替换为您正在使用的实际参数并尝试运行它。
我的猜测是,因为它是一个如此复杂的查询,你创建了一个不可能的要求,它要么在编译阶段失败,要么就是没有返回任何结果。然后,您可以使用查询,直到您正确完成,然后相应地更改PHP脚本。
SELECT e0_.supplier_transaction_detail_id AS supplier_transaction_detail_id0,
e0_.r_number AS r_number1,
e0_.p_number AS p_number2,
e0_.inv_number AS inv_number3,
e0_.an_number AS an_number4,
e0_.ad_number AS ad_number5,
e1_.supplier_transaction_id AS supplier_transaction_id7,
e1_.sender_id AS sender_id8,
e1_.receiver_id AS receiver_id9,
e1_.transaction_date AS transaction_date11,
e1_.gs_number AS gs_number12,
e2_.supplier_doc_type_id AS supplier_doc_type_id14,
e2_.doc_type AS doc_type15,
e2_.direction AS direction16,
e0_.supplier_transaction_id AS supplier_transaction_id17,
e1_.supplier_doc_type_id AS supplier_doc_type_id18
FROM supplier_transaction_detail e0_
INNER JOIN supplier_transaction e1_ ON e0_.supplier_transaction_id = e1_.supplier_transaction_id
INNER JOIN supplier_doc_type e2_ ON e1_.supplier_doc_type_id = e2_.supplier_doc_type_id
WHERE (e0_.r_number = ? OR
e0_.p_number = ? OR
e0_.inv_number = ? OR
e0_.an_number = ? OR
e0_.ad_number = ?)
AND e1_.gs_number = ?
AND e1_.sender_id = ?
AND e1_.receiver_id = ?
AND e2_.doc_type = ?
AND e2_.direction = ?
OR e1_.transaction_date BETWEEN ? AND ?
ORDER BY e1_.transaction_date DESC LIMIT 10 OFFSET 0
答案 1 :(得分:1)
您不能在LIKE
条款中使用BETWEEN
;它只是BETWEEN x AND y
:
SELECT a,b,c
FROM SupplierBundle:SupplierTransactionDetail a
JOIN a.supplierTransaction b
JOIN b.supplierDocType c
WHERE b.transactionDate
BETWEEN :startDate AND :endDate
ORDER BY b.transactionDate desc
答案 2 :(得分:0)
嘿,要设置正确的查询,它必须如下所示:
SELECT ..
FROM ..
WHERE (SELECT .. FROM ..) BETWEEN .. AND ..
我根据您的代码为您设置示例。请记住添加更多查询。 因此,具有子查询的此查询的示例应该类似于以下内容:
$subQueryDQL = $qbv->select('a.parameter')
->from('SupplierBundle:SupplierTransactionDetail', 'a')
...
->getDQL();
$qb->andWhere($qb->expr()->between(
sprintf('(%s)', $subQueryDQL),
$filter->getStartDate()->getTimestamp(),
$filter->getEndDate()->getTimestamp()
));
您的代码看起来像: $ em = $ this-> getEntityManager(); $ qb = $ em-> createQueryBuilder();
$query = $qb->select('a.supplierTransactionDetailId, a.rNumber, a.pNumber, a.invNumber, a.anNumber, a.adNumber, a.amount,
b.supplierTransactionId, b.transactionDate, b.senderId, b.receiverId, b.isaNumber, b.gsNumber, b.fileName,
c.supplierDocTypeId, c.docType, c.direction}')
->from("SupplierBundle:SupplierTransactionDetail", 'a')
->innerJoin('a.supplierTransaction', 'b')
->innerJoin('b.supplierDocType', 'c')
->add('where', $qb->expr()->between(
'b.transactionDate',
':startDate',
':endDate'
)
)
->andWhere(
$qb->expr()->like('b.senderId', ':senderId')
)
->setParameter('rNumber', "%$rNumber%")
->setParameter('gsNumber', "%$gsNumber%")
->setParameter('senderId', "%$senderId%")
->setParameter('receiverId', "%$receiverId%")
->setParameter('docType', "%$docType%")
->setParameter('direction', "%$direction%")
->setParameter('startDate', "%$startDate%")
->setParameter('endDate', "%$endDate%")
->setFirstResult(($page - 1) * $limit)
->setMaxResults($limit);
$paginator = new Paginator ($query, $fetchJoinCollection = false);
$paginator->setUseOutputWalkers(false);
return $paginator;
您必须自己添加更多内容。