变得未知 属性

Getting unknown property

大家好

请帮助我消除代码中的错误

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

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

我收到以下错误:

Getting unknown property: app\models\ClientClient::client_id

public function save()

中导致的错误

我该如何解决?


    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']);
    }

应该没问题

正确的解决方案

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']);
}
}