2

I create some relations Model_One hasMany Model_Two.

I create some form with Model_One fields and a three fields likes Model_Two.0.name, Model_Two.1.name, Model_Two.2.name.

In beforeSave or beforeValidate (no matter) of Model_2 I would like remove relations when name it's empty. How to do this?

After saving my results should look like this:
Saved fields from Model_One
Saved fields from Model_Two with keys, for example 0 and 2 (because Model_Two.1.name was empty).

tshepang
  • 12,111
  • 21
  • 91
  • 136
kicaj
  • 2,881
  • 5
  • 42
  • 68
  • Removing relations? To me it looks more like you want to exclude data-sets from being validated/saved? – ndm Oct 12 '13 at 11:46

1 Answers1

2

Use Model->unbindModel() to unbind models dynamically

Refer the below links for more details

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly

http://api.cakephp.org/2.4/class-Model.html#_unbindModel

nithin
  • 449
  • 2
  • 5
  • Thank you. It worked for me eg. `$this->User->unbindModel( array('belongsTo' => array('Category', 'BodyHeight')) );` – Kamlesh Jun 29 '21 at 13:48