Propel和symfony:Join Table不起作用

时间:2013-04-20 16:38:41

标签: symfony1 symfony-1.4 propel

首先我解释一下我的问题:

我有两个表Job和JobCategory:

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

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

这两个表与外键相关" category_job_id"。

在此应用程序中,我使用的是Propel ORM。我希望使用三个字段关键字job_name和category_name进行搜索。

第一个字段是关键字是"输入"谁我可以写关键字,第二个字段是Category_name是"选择",类别列表。第三个字段是Job_name,是" select",作业名称列表,如果不为空,则忽略关键字字段。

我像这样制作搜索功能,但它对我不起作用:

  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;

}

但搜索是所有情况都是错误的!

1 个答案:

答案 0 :(得分: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();
}