Yii Framework Forum: how to add the missing record to the parent model easily - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

how to add the missing record to the parent model easily

#1 User is offline   xPeng 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 04-August 15

Posted 18 September 2017 - 09:10 PM

Attached File  country_city.PNG (3.41K)
Number of downloads: 3
The ERD:

The child tabel: CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country`) REFERENCES `country` (`country`) ON UPDATE CASCADE

parent table country:
country
USA

child table city
country   city
USA       New York


Using ActiveForm for the model of the child table:
...
<?= $form->field($model,'country')->textInput()?> <-- input here: France
<?= $form->field($model, 'city')->textInput()?> <-- input here: Paris
the validate failed.

Is there a easy way to add the France to the parent model at first automaticly with a option like this:
<?= $form->field($model,'country')->textInput(['addToParentModel' => true])?>

Thanks.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,641
  • Joined: 16-February 11
  • Location:Japan

Posted 19 September 2017 - 06:00 AM

View PostxPeng, on 18 September 2017 - 09:10 PM, said:

Are there a easy way to add the France to the parent model at first automaticly with a option like this:
<?= $form->field($model,'country')->textInput(['addToParentModel' => true])?>

AFAIK, simple "No" is the answer.

I would not try to do it, but it could be possible to create a method in City model that ensures the parent Country's existence before inserting/updating a record, probably in "beforeSave" method.

But I'd rather use a listbox or dropdown for the 'country' field in the form for the city.

One more thing. Why do you want to avoid a standard way that uses integers for the keys?
0

#3 User is offline   xPeng 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 04-August 15

Posted 19 September 2017 - 11:15 AM

Thanks for the anwser.

Quote

But I'd rather use a listbox or dropdown for the 'country' field in the form for the city.

I do make a dropdown form the country, but when a country model(or other model what ever) missed a record, I want the user contribute to extend the database with his enters; or in other situations, the users should make the whole new database by enter the new records that not exist.



Quote

One more thing. Why do you want to avoid a standard way that uses integers for the keys?

The ERD here without keys was for simplicity.
I do add the surrogate Id key, although it's only a option for me. I don't put the keys in practice often.

Thanks again.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users