Php 获取未知属性

Php 获取未知属性,php,yii2,yii2-advanced-app,yii2-model,yii2-validation,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'], ]

大家好

请帮我清除代码中的错误

我在
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,
        ];
    }

}

当我试图创建一个用户或更新一个现有的用户时,我得到一个错误

我得到以下错误:

正在获取未知属性:app\models\ClientClient::client\u id

公共函数save()中导致的错误

我怎样才能修好它

public function getClientPhone(){ return $this->hasOne(ClientPhone::class, ['client_id' => 'id']); } public function getClientClient(){ return $this->hasOne(ClientClient::class, ['id' => 'client_id']); } 公共函数getClientPhone(){ 返回$this->hasOne(ClientPhone::class,['client_id'=>'id']); } 公共函数getClientClient(){ 返回$this->hasOne(ClientClient::class,['id'=>'client_id']); } 这就是关系。我猜您的ClientClient模型具有列id,请尝试以下操作:

public function getClientClient(){ return $this->hasOne(ClientClient::class, ['client_id' => 'id']); } 公共函数getClientClient(){ 返回$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'], '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']);
}
}

您需要共享
ClientPhone
model的代码-错误告诉您没有在那里声明
client\u id
字段。您在
client\u-client
表中有
client\u-id
列吗?@rob006没有。我在“client\u-phone”中有“client\u-id”,那么为什么对
client\u-id
ClientClient
model中的字段?@rob006成功了!非常感谢。我是dumb@Sergej然后,您应该发布您的模型代码,而不仅仅是摘录和期望我们猜测;) 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'], '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']);
}
}