2013-04-20 54 views
0

首先我解釋一下我的問題:行走和symfony的:加入表不起作用

我有兩個表工作和JobCategory:

job: 
    ------------------------------------------------- 
    | id | category_job_id | job_name | keywords | 
    ------------------------------------------------- 

JobCategory: 
    ------------------------- 
    | id | categoty_name | 
    ------------------------- 

壽兩個表的外鍵「category_job_id」有關。

在這個應用程序中,我正在使用Propel ORM。我希望使用三個字段關鍵字job_name和category_name進行搜索。

第一個字段是關鍵字是「輸入」誰可以寫關鍵字,第二個字段是Category_name是一個「select」,類別列表。第三個字段是Job_name,是「選擇」,作業名稱列表,如果不是空的,關鍵字字段將被忽略。

我作出這樣的搜索功能,但它不爲我工作:

public function searchFilter($job,$category,$keyword) 
    { 

$order = isset($this->order) ? $this->order : Criteria::ASC; 

$job = '%' .$job. '%'; 
$category = '%' .$category. '%'; 

$c = new Criteria(); 

$c->addJoin(JobPeer::CATEGORY_JOB_ID, JobCategoryPeer::ID); 
if((null !== $category) AND ($category !== "")) 
{ 
$c->addOr(JobCategoryPeer::CATEGORY_NAME,$category, Criteria::LIKE);  
} 
if((null !== $job) AND ($job !== "")) 
{ 
$c->addOr(JobPeer::JOB_NAME,$job, Criteria::LIKE); 
} 

$query = JobQuery::create(null, $c) 
     ->joinWith('Job.JobCategory') 
     ->orderByDateOfJob($order); 

    if((null !== $keyword) AND ($keyword !== "")){ 
    $keyword = '%' .$keyword. '%'; 
    $query->filterByKeywords($keyword, Criteria::LIKE); 
    }  

$results = $query->find(); 


return $results; 

} 

但搜索是所有情況下是錯誤的!

+0

我不認爲這是你的問題的解決方案,但在你的文章中有一些錯誤。你的表很可能是'job_category',而不是'JobCategory' - 後者是你對應的模型類。我應該想'categoty_name'也應該是'category_name'。 – halfer 2013-04-21 18:33:50

+0

看看Propel docs btw:有一個'toString'方法(或類似的)可以應用到'$ query',它會給你它生成的SQL。這對調試非常有用。 – halfer 2013-04-21 18:35:35

回答

1

我認爲這樣的事情會起作用。如果沒有,您可以在發出find()(見下文)之前獲得生成的SQL,以便您(和我們)可以看到輸出結果。

public function searchFilter($job,$category,$keyword) 
{ 

    $order = isset($this->order) ? $this->order : Criteria::ASC; 

    $query = JobQuery::create()->joinWith('JobCategory'); 
    $conditions = array(); 

    if((null !== $category) AND ($category !== "")) 
    { 
    $query->condition('catName', "JobCategory.CategoryName LIKE ?", "%$category%"); 
    $conditions[] = 'catName'; 
    } 
    if((null !== $job) AND ($job !== "")) 
    { 
    $query->condition('jobName', "Job.JobName LIKE ?", "%$job%"); 
    $conditions[] = 'jobName'; 
    } 
    if (sizeOf($conditions) > 1) 
    { 
    // join your conditions with an "or" if there are multiple 
    $query->combine($conditions, Criteria::LOGICAL_OR, 'allConditions'); 
    // redefine this so we have the combined conditions 
    $conditions = array('allConditions'); 
    } 

    // add all conditions to query (might only be 1) 
    $query->where($conditions); 

    if((null !== $keyword) AND ($keyword !== "")) 
    { 
    $query->filterByKeywords("%$keyword%", Criteria::LIKE); 
    } 

    $query->orderByDateOfJob($order); 
    $sql = $query->toString(); // log this value so we can see the SQL if there is a problem 
    return $query->find(); 
} 
+0

非常感謝jakerellla – Nll 2013-04-23 00:08:48

+0

當然,希望它適合你。 – jakerella 2013-04-23 01:34:01

+0

是的一些變化後,它的作品非常好,非常感謝,但我有另一個愚蠢的問題,如果你可以幫助我們... – Nll 2013-04-23 01:35:14