按相关数据排序不起作用

Order by related data doesn't work

我有关系模型:

public function relations()
{
    return array(
        'fidistr' => array(self::BELONGS_TO, 'Distributors', 'fidistr_id', 'order'=>'fsname'),
        'fitown' => array(self::BELONGS_TO, 'Town', 'fitown_id'),
        'distributorsPointMails' => array(self::HAS_MANY, 'DistributorsPointMail', 'fidistr_point_id'),
        'distributorsPointPhones' => array(self::HAS_MANY, 'DistributorsPointPhones', 'fidistr_point_id'),
    );
}

我想按 "fsname" 从相关 table 订购数据。我试过这个:

$models = DistributorsPoint::model()->findAll('fitown_id=:id', array('id' => $_POST['city_id']));

但它仍然是 returns 未排序的数据。请帮忙

关系上的order仅在延迟加载关系时使用。 From the Yii guide on relations:

Note: when using eager loading such relation options as 'order', 'group', 'having', 'limit' and 'offset' will be ignored. You should setup such parameters at the main model criteria level if you wish them to be applied.

您可以传递条件数组而不是字符串作为 findAll(). This will be used to initialize a CDbCriteria object. To order by a relation you need to eager load the relation using with() 的第一个参数:

$models = DistributorsPoint::model()
    ->with('fidistr')
    ->findAll(array(
        'condition' => 'fitown_id=:id',
        'order'     => 'fidistr.fsname',
        'params'    => array(':id' => $_POST['city_id'])
   ));