【问题标题】:Yii model->findAll(), how to add relational table columns?Yii model->findAll(),如何添加关系表列?
【发布时间】:2014-09-15 10:52:04
【问题描述】:

我有两个模型。它们是 DisnotificationUpdate 和 DisNotification。 DisnotificationUpdate 关系如下。

public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'Login', 'userid'),
            'notifi' => array(self::BELONGS_TO, 'Disnotification', 'notifi_id'),

        );
    }

属性如下。

public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'notifi_id' => 'Notifi',
            'view' => 'View',
            'userid' => 'Userid',
        );
    }

Disnotification 模型具有以下属性。

public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'notification' => 'Notification',
            'owner' => 'Owner',
            'workspace_id' => 'Workspace',
            'postID' => 'Post',
            'pDate' => 'P Date',
        );
    }

我想从 DisnotificationUpdate 中选择值,并在 DisNotification 中使用“pDate”按值排序。 我尝试如下。

 $dataProvider=DisnotificationUpdate::model()->findAll(array(
        'condition' => 'userid=:userid',
        'order' => 'notifi.pDate ASC',
        'limit'=>10,
        'params' => array(':userid' => $myid)
    ));

但它给出了一个错误,“'order Clause'中的未知列'notifi.pDate'”。我做错了什么?谢谢。

【问题讨论】:

    标签: php sql-order-by foreign-key-relationship yii


    【解决方案1】:

    你需要预先加载相关模型

    $dataProvider=DisnotificationUpdate::model()->findAll(array(
        'with' => array('notifi'),
        'condition' => 'userid=:userid',
        'order' => 'notifi.pDate ASC',
        'limit'=>10,
        'params' => array(':userid' => $myid)
    ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多