【问题标题】:Propel and symfony : Join Table doesn't workPropel 和 symfony:加入表不起作用
【发布时间】:2013-04-20 16:38:40
【问题描述】:

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

我有两张表 Job 和 JobCategory :

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

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

这两个表是通过外键“category_job_id”关联的。

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

第一个字段是keywords是“输入”谁我可以写关键字,第二个字段是Category_name是一个“选择”,类别列表。第三个字段是Job_name,是一个“select”,Job名称列表,如果不为空关键字字段将被忽略。

我做了这样的搜索功能,但它对我不起作用:

  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;

}

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

【问题讨论】:

  • 我认为这不是您问题的解决方案,但是您的帖子中有一些错误。您的表可能是job_category,而不是JobCategory - 后者是您对应的模型类。我应该认为categoty_name 也应该是category_name
  • 看看 Propel 文档顺便说一句:有一个 toString 方法(或类似方法)可以应用于 $query,它将为您提供它生成的 SQL。这对于调试非常有用。

标签: symfony1 symfony-1.4 propel


【解决方案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();
}

【讨论】:

  • 是的,经过一些更改,它工作得很好,非常感谢,但如果你能帮助我们,我还有另一个愚蠢的问题......
  • 如果你没有时间,没问题!Tnks.
  • 我看了看,但 Symfony 并不是真正的领域。我使用基本的 Propel。明天我会好好看看,看看能不能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
相关资源
最近更新 更多