Log message #4209698

# At Username Text
# Oct 19th 2019, 20:52 mrfeedback and how can I add the users id when saving a record? do I really have to pass it from the controller?
# Oct 19th 2019, 20:35 mrfeedback is it a bad idea to use authoriztion within models?
# Oct 19th 2019, 15:12 ndm No problem.
# Oct 19th 2019, 15:09 kailas thanks so much for the help!
# Oct 19th 2019, 15:09 kailas @ndm ooh! figured it out! I forgot to change my associated array in my patch entity. once I changed it to ShipmentHandlingUnits.OrderLines everything works!
# Oct 19th 2019, 15:07 kailas @ndm https://gist.github.com/kailasnarendran/ef7b2694d63243a8c47104aae9adf192 is what i’m posting
# Oct 19th 2019, 15:06 kailas @ndm following the format of the add() action. creating new entity then patching. i changed to passing primary key as you suggested and that seems to work. see https://gist.github.com/kailasnarendran/df11c8ca5d97f542882c14ae82c66d50 . now it’s creating the shipment handling unit with the order_line flagged as dirty, but it doesn’t create the OrderLinesShipmentHandlingUnits entity in the _joinData field
# Oct 19th 2019, 15:02 kailas @nds
# Oct 19th 2019, 15:00 kailas @ndm sorry for stepping away. creating a new shipment
# Oct 19th 2019, 14:32 slackebot1 https://book.cakephp.org/3.0/en/orm/saving-data.html#patching-hasmany-and-belongstomany
# Oct 19th 2019, 14:32 ndm Generally if you want to associate existing records, you'd pass the primary key property of that existing record, not the foreign key in the join table, ie `shipment_handling_units[0].order_lines[$i].id` instead of `shipment_handling_units[0].order_lines[$i]._joinData.order_line_id` And if you'd wanted to update a record, you'd usually patch an entity obtained from the database that includes the associated records.
# Oct 19th 2019, 14:21 ndm Are you creating a new shipment, or updating an existing one?
# Oct 19th 2019, 14:19 kailas @ndm ah, yeah, the debugs of the pre-save and the patch arrays was key. part of the problem was the [$i]. should have been .$i. in my form input. now the problem is that it’s trying to create new order lines, rahter than associate with the existing ones
# Oct 19th 2019, 14:13 kailas k
# Oct 19th 2019, 14:13 ndm Well, that might be (part of) your problem. Debug the entity after patching to check what it looks like, and make sure that your `save()` call uses the correct `associated` option too.
# Oct 19th 2019, 14:12 kailas to be clear, i’m patching a newEntity that is empty
# Oct 19th 2019, 14:12 kailas ugh. didn’t work. not sure how to debug this further (since it does save the shipmehandling unit and there are no errors)
# Oct 19th 2019, 14:11 kailas @ndm ok, cool, will try it!
# Oct 19th 2019, 14:10 ndm Generally that looks OK I think. Not sure if you actually need to put the foreign key in the form if the entity that you're patching contains all the associated entities.
# Oct 19th 2019, 14:06 kailas @ndm the form inputs are inside a foreach that’s iterating through all the lines I can add
# Oct 19th 2019, 14:04 kailas @ndm so, to put it all in one place, https://gist.github.com/kailasnarendran/ef0bb5536cb9d13a97e5ac3ad4d82252 . does that look right?
# Oct 19th 2019, 14:01 ndm No, if there is no such association and it's just BTM, then this shouldn't be in the `associated` option (there are edge cases where you want to do something like that), and it should just be `ShipmentHandlingUnits.OrderLines`, in that case your original property path would be correct.
# Oct 19th 2019, 13:59 kailas so ShipmentHandlingUnits belongsTo OrderLinesShipmentHandlingUnits (need to add this), so then I can create those entities that will link ShipmentHandlingUnits to OrderLines (via the BTM relationship)
# Oct 19th 2019, 13:57 kailas Do I need to define that explicitly as well?
# Oct 19th 2019, 13:57 kailas yeah, i don’t have an explicit association between ShipmentHandlingUnits and OrderLinesShipmentHandlingUnits (it’s just implied with the BTM between ShipmentHandlingUnits and OrderLines
# Oct 19th 2019, 13:56 ndm No, I just cut of the rest because the problem is the missing association, assuming that the `associated` option is telling the truth.
# Oct 19th 2019, 13:55 kailas oh, i see, so I don’t actually set the value of _joinData, i need to set those fields in .orderlines_shipment_handling_units[] explicitly? so shipment_handling_units[0].orderlines_shipment_handling_units[0].order_line_id=1, shipment_handling_units[0].orderlines_shipment_handling_units[0].qty=3, etc…
# Oct 19th 2019, 13:53 ndm Your path is only two levels deep, but your association is three levels deep. It should be something like `shipment_handling_units[index].orderlines_shipment_handling_units[index].order_lines[index]...`
# Oct 19th 2019, 13:49 kailas @ndm is that the @this->Shipments->patchEntity ?
# Oct 19th 2019, 13:48 kailas @ndm hrm.. i’m sorry, i don’t understand what you mean by “your path is missing the property”
# Oct 19th 2019, 13:47 ndm No no... sorry, I just need to wake up first, it needs and index too. But your path is missing the property for `OrderLinesShipmentHandlingUnits`.
# Oct 19th 2019, 13:46 kailas @ndm shipment_handling_units[0].order_lines[]._joinData.order_line_id = … ?
# Oct 19th 2019, 13:45 kailas @ndm oh, i see, so i should get rid of the $i?
# Oct 19th 2019, 13:45 ndm *c
# Oct 19th 2019, 13:45 ndm Wait, all keys except x
# Oct 19th 2019, 13:45 kailas @ndm so my patch is : $this->Shipments->patchEntity($shipment, $this->request->getData(),[‘associated’=>[‘ShipmentHandlingUnits.OrderLinesShipmentHandlingUnits.OrderLines’]]); my individual lines are (iterating through the order lines) : <?=$this->Form->input(“shipment_handling_units[0].order_lines[$i]._joinData.order_line_id”,[‘type’=>‘hidden’,‘value’=>$orderLine->id]); ?>
# Oct 19th 2019, 13:43 ndm The path is still wrong, all keys need an index ;) Anyways, by default only first level associations are patched and saved, for deeper associations you need to explicitly allow them using the `associated` option for patching and saving, maybe that's your problem.
# Oct 19th 2019, 13:35 kailas I’m trying to create an entity A and B (and existing C) where : A has many B and B BelongsToMany C. in my patch entity array i include the a.b[0].c._joinData.stuff values. On my save, A and B are created, but the join table entities between B and C are not created (from the _joinData data). is that even possible?
# Oct 19th 2019, 12:54 admad When I am faced with such a situation I usually just keep mum :slightly_smiling_face:
# Oct 19th 2019, 12:39 alexdd55976 I would, but I have no experience with all that :,)
# Oct 19th 2019, 12:33 admad In case anyone wants to weight in on this your thoughts are welcome https://github.com/cakephp/cakephp/pull/13766