获取未知属性

时间:2018-07-03 08:58:03

标签: php yii2 yii2-advanced-app yii2-model yii2-validation

大家好,

请帮助我摆脱代码中的错误

我在ClientForm中有下一个代码

class ClientForm extends model
{

    private $_client;
    private $_phone;

    public function rules()
    {
        return [
            [['ClientClient'], 'required'],
            [['ClientPhone'], 'safe'],
        ];
    }

    public function afterValidate()
    {
        $error = false;
        if (!$this->clientClient->validate()) {
            $error = true;
        }
        if (!$this->ClientPhone->validate()) {
            $error = true;
        }
        if ($error) {
            $this->addError(null); // add an empty error to prevent saving
        }
        parent::afterValidate();
    }

    public function save()
    {
        if (!$this->validate()) {
            return false;
        }
        $transaction = Yii::$app->db->beginTransaction();
        if (!$this->clientClient->save()) {
            $transaction->rollBack();
            return false;
        }
        $this->ClientPhone->client_id = $this->clientClient->id;
        if (!$this->clientphone->save(false)) {
            $transaction->rollBack();
            return false;
        }
        $transaction->commit();
        return true;
    }

    public function getClientclient()
    {
        return $this->_client;
    }

    public function setClientclient($client)
    {
        if ($client instanceof Clientclient) {
            $this->_client = $client;
        } else if (is_array($client)) {
            $this->_client->setAttributes($client);
        }
    }

    public function getClientphone()
    {
        if ($this->_phone === null) {
            if ($this->Clientclient->isNewRecord) {
                $this->_phone = new ClientPhone();
                $this->_phone->loadDefaultValues();
            } else {
                $this->_phone = $this->ClientClient->clientPhone;
            }
        }
        return $this->_phone;
    }

    public function setClientPhone($ClientPhone)
    {
        if (is_array($ClientPhone)) {
            $this->Clientphone->setAttributes($ClientPhone);
        } elseif ($ClientPhone instanceof ClientPhone) {
            $this->_phone = $ClientPhone;
        }
    }

    public function errorSummary($form)
    {
        $errorLists = [];
        foreach ($this->getAllModels() as $id => $model) {
            $errorList = $form->errorSummary($model, [
                'header' => '<p>Please fix the following errors for <b>' . $id . '</b></p>',
            ]);
            $errorList = str_replace('<li></li>', '', $errorList); // remove the empty error
            $errorLists[] = $errorList;
        }
        return implode('', $errorLists);
    }

    private function getAllModels()
    {
        return [
            'ClientClient' => $this->clientClient,
            'ClientPhone' => $this->clientphone,
        ];
    }

}

ClientClientClientPhone(模型)

class ClientPhone extends \yii\db\ActiveRecord
{
public static function tableName(){
    return 'client_phone';
}
public function rules(){
    return [
        [['client_id'], 'integer'],
        [['phone_digital'], 'required'],
        [['phone_digital'], 'string', 'max' => 255],
        [['client_id'], 'exist', 'skipOnError' => true, 'targetClass' => Clientclient::class, 'targetAttribute' => ['client_id' => 'id']],
    ];
}
public function attributeLabels(){
    return [
        'id' => 'ID',
        'client_id' => 'Client ID',
        'phone_digital' => 'Phone Digital',
    ];
}
public function getclientclient(){
    return $this->hasOne(clientclient::class, ['id' => 'client_id']);
}
public function getClientClient(){
    return $this->hasOne(ClientClient::class, ['client_id' => 'id']);
}
}

class ClientClient extends \yii\db\ActiveRecord
{

public static function tableName(){
    return 'client_client';
}
public function rules(){
    return [
        [['age', 'client_id', 'phone_digital'], 'integer'],
        [['first_name', 'patronymic', 'last_name'], 'string', 'max' => 255],
    ];
}
public function attributeLabels(){
    return [
        'id' => 'ID',
        'first_name' => 'First Name',
        'patronymic' => 'Patronymic',
        'last_name' => 'Last Name',
        'age' => 'Age',
        'client_id' => 'Client Id',
        'phone_digital' => 'Phone Digital',


    ];
}
public function getClientPhone(){
    return $this->hasOne(ClientPhone::class, ['client_id' => 'id']);
}
}

当我尝试创建用户或更新现有用户时,出现错误消息

然后出现以下错误:

  

获取未知属性:app \ models \ ClientClient :: client_id

public function save()中引起的错误

我该如何解决?

2 个答案:

答案 0 :(得分:0)


    public function getClientPhone(){
        return $this->hasOne(ClientPhone::class, ['client_id' => 'id']);
    }

    public function getClientClient(){
        return $this->hasOne(ClientClient::class, ['id' => 'client_id']);
    }
    

这些是关系。我猜您的ClientClient模型具有列ID,因此请尝试以下操作:


    public function getClientClient(){
        return $this->hasOne(ClientClient::class, ['client_id' => 'id']);
    }

应该没事的。

答案 1 :(得分:0)

正确的解决方案

class ClientClient extends \yii\db\ActiveRecord

public static function tableName()
{
    return 'client_client';
}
public function rules(){
    return [
        [['age'], 'integer'],
        [['first_name', 'patronymic', 'last_name'], 'string', 'max' => 255],
    ];
}
public function attributeLabels(){
    return [
        'id' => 'ID',
        'first_name' => 'First Name',
        'patronymic' => 'Patronymic',
        'last_name' => 'Last Name',
        'age' => 'Age',
    ];
}

public function getClientPhone(){
    return $this->hasOne(ClientPhone::class, ['client_id' => 'id']);
}
}