CakePHP Save newly created ID in Parent ID field of self assoc model -


I have a parent / child self association table, where id = parent_id, that ID is the core of itself, though the ad action / View

I'm having trouble saving data in my table with Add A / View - when I add a new record, I select a parent_id from the drop down box and enter a name.

  & lt ;? Php echo $ this- & gt; Form- & gt; Input ('parent_id', array ('empty' = & gt; 'no guardian')); Copy $ this- & gt; Form- & gt; Input ('name'); ? & Gt;   

If the user does not select "no parent," then that means that I would like parent_id = id, where the ID is unique ID created in DB when saved automatically.

array ('item' = & gt; array)

This is what $ -> request-> ('Parent_id' = & gt; '', 'name' = & gt; 'testname')))

I have tried to set parent_id = id in the first save The id is not present yet, there is nothing to assign to paren_id in it, I have also tried to call the "parent" model, save it first and save everyone in the controller but they also do not work Are there.

Administrator

  Add Public Function () {if ($ is-> Request-> ('Post')) {$ this-> ; Item- & gt; Create (); // first tried to call the parent model in the union itself, // if ($ this-> item-> ParentItem-> Save ($ -> -> -> data;)) (If ($ - and- gt; Item-> Savings All ($ this-> Request-> Data)) {$ this-> Session-> Set Flash (__ ('Items $ '-> Redire (Array (' Actions '=>' Index ')); and {$ this-> Session-> Set Flash (__ (' Item could not be saved, please try again. '));}}   

Item.php / model relationship

  public $ associTo = array ('ParentItem' = & gt; array ('classN Ame '= & gt; item', 'foreign key' = & gt; 'parent_id', 'conditions' => "',' fields '=>"', 'order' = & gt; '); Many in the public $ = array (' child item '=> gt; array (' className '= & gt;' item ',' foreigner '= & gt;' parent_id ',' dependent '= & gt; Incorrect, 'Terms' = & gt; '', 'fields' =>, 'order' =>, '',));   

How can I take that which is just made / saved and saves it in another area, in this case parent_id?

Update: I'm working on something else and I have used getInsertId () to get the last entered ID and I'm trying to save it to parent_id, but there is something like this. Whoever stops it, I have removed all model verification to ensure that it was not so. But is there something like cake (or my association setup) that does not allow parent_id = id (i.e. one line it's own parent?

This is the latest code in my ad action. .. this line for db, but w / oa parent_id. Then try to edit using ad action and set parent_id = id, but do not even allow editing to save. / P>

  add public function () (if ($ this-> ; Request-> ('Post')) {$ this-> Item- & gt; Create (); if ($ this-> item-> Save ($ this- & gt; Request-> data)) {$ last_id = $ this-> item-> getInsertId (); $ this-> item- & gt; savedfield ('parent_id', $ last_id); $ the -> Session-> Set Flash (__ ('item has been saved')); $ -> - (array ('action' => gt; 'index'));} Other {$ this- & Gt; Session-> gt;; Set Flash (__ ('The item could not be saved, please try again.'));}}   

I call this $ - Have tried too - gt; Item-> ParentItem - & gt; Save (), save all, 'deep' => True, but still nothing allows me to update the parent column. A line Ge

Thank you in advance

In your model of association, you Foregin_key can be set from Parent_ID , and will auto-fill

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -