以关系命令PROPEL

时间:2012-08-01 13:44:00

标签: php symfony1 propel

我有:

# config/schema.yml
propel:
  jobeet_category:
    id:           ~
    name:         { type: varchar(255), required: true, index: unique }

  jobeet_job:
    id:           ~
    category_id:  { type: integer, foreignTable: jobeet_category, foreignReference: id, required: true }
    type:         { type: varchar(255) }
    company:      { type: varchar(255), required: true }

例如:

JobeetCategory:

id | name
1  | first
2  | second

JobeetJob:

id | category_id | name
1  | 1           | bbb
2  | 1           | ccc
3  | 1           | aaa
4  | 2           | zzz
5  | 2           | xxx

我做了:

$c = new Criteria();
$categories = JobeetCategoryPeer::doSelect($c);

我可以:

foreach($categories as $category){

  echo $category->getName();
  foreach ($category->getJobeetJobs() as $job){
          echo '-' . $job->getName();
  }
}
这告诉我:

 first
 - bbb
 - ccc
 - aaa
 second
 - zzz
 - xxx

但我希望将其排序ASC by NAME

 first
 - aaa
 - bbb
 - ccc
 second
 - xxx
 - zzz

如何使用PROPEL QUERY制作它?

3 个答案:

答案 0 :(得分:1)

如果您提到symfony的版本并推动您使用它会更容易。但是这里可能有所帮助,希望它有所帮助。

$categories = JobeetCategoryQuery::create()
                            ->addJoin(JobeetJobPeer::CATEGORY_ID, JobeetCategoryPeer::ID)
                            ->addAscendingOrderByColumn(JobeetJobPeer::NAME)
                            ->find();

只是因为查询不存在,您可以使用标准。

  $criteria = new Criteria();
  $criteria->addJoin(JobeetJobPeer::CATEGORY_ID, JobeetCategoryPeer::ID)
                        ->addAscendingOrderByColumn(JobeetJobPeer::NAME);
  $categories = JobeetCategoryPeer::doSelect($criteria);

答案 1 :(得分:1)

我知道这是一个老问题,但想要提供一个更新的方法来做到这一点。您可以将查询对象传递给getter:

foreach($categories as $category){
  echo $category->getName();
  $q = JobeetJobQuery::create()->orderByName();
  foreach ($category->getJobeetJobs($q) as $job){
          echo '-' . $job->getName();
  }
}

答案 2 :(得分:0)

$c = new Criteria();
$c->addJoin(JobeetJobPeer::CATEGORY_ID, JobeetCategoryPeer::ID, Criteria::INNER_JOIN);
$c->addAscendingOrderByColumn(JobeetJobPeer::NAME);

$categories = JobeetCategoryPeer::doSelect($c);

jobeet_job没有列name。您是否使用$job->getName()的自定义getter?