2012-03-13 64 views
2

我有一个从项目到资产使用ProjectAsset类作为这个2之间的中间类多对多的关系。当我在项目上使用getProjectAsset()方法它返回所有我的ProjectAssets分配给该项目。我怎么才能orderby在schema.yml教条symfony 1.4

如何更改schema.yml以默认排序结果?

Project: 
    relations: 
    Assets: 
     class: Asset 
     refClass: ProjectAsset 
     foreignAlias: Projects 
     local: project_id 
     foreign: asset_id   

ProjectAsset: 
    tableName: projects_assets 
    columns: 
    title: { type: string, length: 255 } 
    project_id: { type: integer, primary: true } 
    asset_id: { type: integer, primary: true } 
    relations: 
    Project: 
     class: Project 
     foreignAlias: ProjectAssetRelations 
     local: project_id 
     foreign: id 
     onDelete: CASCADE 
    Asset: 
     class: Asset 
     foreignAlias: ProjectAssetRelations 
     local: asset_id 
     foreign: id 
     onDelete: CASCADE 

回答

13

这是我第一次读后想到的http://www.dobervich.com/2011/03/05/symfony2-blog-application-tutorial-part-ii-the-data-model/虽然还没有测试过。

oneToMany: 
    posts: 
     targetEntity: Post 
     orderBy: 
      createdAt: DESC 
+0

感谢您的回答。这是一种简单的方法,可以在不使用查询构建器/条件等情况下实现排序!它甚至适用于嵌套的实体列!干杯! – 2014-08-06 17:46:26

+0

这根本不适用于我。我正在使用Symfony 2.5 – 2014-11-10 15:09:14

+0

OP询问Sf 1.4; 2.x的作品完全不同。 – GergelyPolonkai 2014-11-10 15:11:04

2

它不必在schema.yml文件中完成,但在代码中检索数据时。

例如,

$passets = Doctrine_Query::create() 
      ->select('p.*') 
      ->from('ProjectAsset p') 
      ->orderBy("p.Title") 
      ->execute(); 

更新:也许你可以:check this question