来自第三表的YII 2关系数据

时间:2017-08-09 15:55:57

标签: php yii yii2

我有一个三级关系的数据库。 cheque->对帐户>顾客。现在我尝试使用以下方法同时从所有三个表中检索数据。

$query = Cheque::find();
$query->joinWith(['account.customer']);
$query->orderBy('sr desc');
$dataProvider = new ActiveDataProvider([
     'query' => $query,
]);

检查型号:

class Cheque extends \common\components\db\ActiveRecord {


    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'cheque';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['sr'], 'integer'],
            [['ID', 'account_ID'], 'required'],
            [['ID', 'account_ID', 'created_by', 'branch_ID', 'application_ID'], 'string'],
            [['serial_start', 'serial_end', 'status'], 'number'],
            [['created_on'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'ID' => 'ID',
            'account_ID' => 'Account  ID',
            'serial_start' => 'Serial Start',
            'serial_end' => 'Serial End',
            'created_on' => 'Created On',
            'created_by' => 'Created By',
            'branch_ID' => 'Branch  ID',
            'application_ID' => 'Application  ID',
            'status' => 'Status',
            'sr' => 'ID'
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAccount() {
        return $this->hasOne(Account::className(), ['ID' => 'account_ID']);
    }


    public static function getActiveChequeBook($account_ID) {
        return Cheque::findAll(['account_ID' => $account_ID, 'status' => array_search('Active', \common\models\Lookup::$cheque_status)]);
    }
    }

但执行此操作后,我收到以下错误: pre>异常'yii \ base \ InvalidCallException',带有消息'设置只读属性:common \ models \ Account :: customer'

1 个答案:

答案 0 :(得分:1)

您的common \ models \ Account模型中的属性客户没有setter(仅存在getCustomer方法)。检查你的模型,并在课堂上添加适当的属性。