0
從GET參數中,我想從我的實體獲取信息。使用Symfony2過濾信息和自定義存儲庫?
我認爲我創建了一個表格3種選擇(不是多個的),是這樣的:
http://pix.toile-libre.org/upload/original/1393414663.png
如果我只由用戶本人在url得到這個過濾:類別= 0 &用戶= 6個&狀態= 0
我不得不處理0值...
這種形式用於過濾我的任務。
這是我在我的控制器行動的一部分:
if($request->query->has('user')) {
$category_id = $request->query->get('category');
$user_id = $request->query->get('user');
$status_id = $request->query->get('status');
// A little test to see if it works.
echo $category_id . '<br>' . $user_id . '<br>' . $status_id;
// I will pass these variables to a repository
$tasks = $em->getRepository('LanCrmBundle:Task')->findFiltered($category_id, $user_id, $status_id);
} else {
$tasks = $em->getRepository('LanCrmBundle:Task')->findAll();
}
我創建了一個倉庫用這種方法:
public function findFiltered($category_id, $user_id, $status_id)
{
/**
* Get filtered tasks.
*
* Get only title, priority, created_at, category_id, user_id and status_id fields (optimization)
*
* Where field category_id = $category_id unless $category_id is smaller than 1 (not secure enough)
* Where field user_id = $user_id unless $user_id is smaller than 1 (not secure enough)
* Where field status_id = $status_id unless $status_id is smaller than 1 (not secure enough)
* Should I do these tests here or in the controller?
*/
}
如何做到這一點的查詢?你有沒有其他優雅的建議來解決這個問題?
由於它工作得很好。我剛剛意識到,這不是任務的狀態,而是擁有任務的項目的狀態。我怎樣才能做到這一點?我在Task實體中有一個項目屬性,我的項目實體有一個狀態屬性(與ProjectStatus實體有關)。 – user3064931