Good day everyone
Please help me get rid of the error in my code
I have next code in 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,
];
}
}
ClientClient
and ClientPhone
(model)
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']);
}
}
and
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']);
}
}
When I try to create a user or update an existing one, I get an error
And I get the following error:
Getting unknown property: app\models\ClientClient::client_id
An error caused in public function save()
How can I fix it?
Correct solution
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']);
}
}