Symfony1 推进和symfony:联接表不';行不通

Symfony1 推进和symfony:联接表不';行不通,symfony1,symfony-1.4,propel,Symfony1,Symfony 1.4,Propel,首先,我解释我的问题: 我有两个表Job和JobCategory: job: ------------------------------------------------- | id | category_job_id | job_name | keywords | ------------------------------------------------- JobCategory: ------------------------- | id

首先,我解释我的问题:

我有两个表Job和JobCategory:

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

JobCategory: 
  -------------------------
  | id   | categoty_name  |
  -------------------------
这两个表由外键“category\u job\u id”关联

在这个应用程序中,我使用的是推进ORM。我希望用三个关键词进行搜索,工作名称和类别名称

第一个字段是关键字,是“输入”我可以写关键字的人,第二个字段是类别名称,是“选择”,类别列表。第三个字段是Job_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;

}

但是搜索结果是错误的

我想这样做会奏效的。如果没有,您可以在发出
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();
}

我不认为这是你问题的解决办法,但你的帖子中有一些错误。您的表可能是
job\u category
,而不是
JobCategory
——后者是您相应的模型类。我应该认为
categoty\u name
也应该是
category\u name
。顺便说一下,有一个
toString
方法(或类似方法)可以应用于
$query
,它将为您提供它生成的SQL。这对于调试来说是非常宝贵的。是的,经过一些更改后,它工作得非常好,非常感谢,但是如果你能帮助我们,我还有一个愚蠢的问题……这里:如果你没有时间,没问题!我看了一眼,但Symfony并不是一个很好的地方。我用的是基本推进。明天我会好好看看,看能不能帮上忙。