Log message #4262379

# At Username Text
# May 5th 2021, 08:28 slackebot <rightscoreanalysis>
# May 5th 2021, 08:27 rightscoreanalysis I think this does the same as you're doing in principle:
# May 5th 2021, 08:26 rightscoreanalysis I mean you can't just throw the data at Cake and it will figure whether to save or update based on what exists in the db?
# May 5th 2021, 08:24 kevin.pfeifer I dont know what you mean by manual switch
# May 5th 2021, 08:21 rightscoreanalysis so you are doing a manual switch
# May 5th 2021, 08:17 slackebot anything with that ID `$entity = $this->MyTable->newEmptyEntity();` The rest is the default patchEntity and save procedure of CakePHP
# May 5th 2021, 08:17 slackebot { $entity = $result[0]; } elseif( sizeof( $result ) == 0 ) { $entity = $this->MyTable->newEmptyEntity(); } $entity = $this->MyTable->patchEntity( $entity, $data ); if( $this->MyTable->save( $entity ) ) { // Yay, it saved } else { // Something went wrong }``` As you can see, I first check my Database if I have any entries with `'some_id' => $some_id` If there is one, use that entity `$entity = $result[0];` If I don’t have
# May 5th 2021, 08:17 kevin.pfeifer you need some sort of ID which identifies each entry in your database. Here is a part of how I sync things with an external API ```$result = $this->MyTable->find() ->where( [ 'some_id' => $some_id ] ) ->toArray(); $data = [ 'data_field1' => $data_field_1, 'data_field2' => $data_field_2, 'data_field3' => $data_field_3, ]; // Update or create depending if the entry is already in DB if( sizeof( $result ) == 1 )
# May 5th 2021, 08:10 rightscoreanalysis may I make a save I am getting Integrity constraint violation: 1062 Duplicate entry '1' for key 'type_id'
# May 5th 2021, 08:04 rightscoreanalysis my data contains an indexed field but not the primary
# May 5th 2021, 08:04 rightscoreanalysis I am guessing I don't have to do a find and update if records exists
# May 5th 2021, 08:03 rightscoreanalysis if I am saving data to my model how can ensure records are updated and not new created
# May 5th 2021, 08:02 rightscoreanalysis I created another project and fixed it - not sure what the issue was
# May 5th 2021, 05:40 mehov @rightscoreanalysis what do the logs say?
# May 4th 2021, 22:18 rightscoreanalysis even pages
# May 4th 2021, 22:18 rightscoreanalysis what's up with a brand new cake4 install - controllers cannot be found
# May 4th 2021, 20:57 admad Associations can have conditions; normalization can at times go beyond practicality :)
# May 4th 2021, 20:50 tyler.adam.lazenby I really like that last suggestion... but then I have to figure out how to make sure it works with the type keys. And the reason for the no type enum is because I am a stickler for Normalization level 3
# May 4th 2021, 20:36 admad Also instead of Accounts hasMany Addresses you can have Accounts hasOne MailingAddresses and hasOne BillingAddresses (both being aliases of Addresess of specific type).
# May 4th 2021, 20:33 admad A separate table address types seems unnecessary, why not just use a "type" enum column?
# May 4th 2021, 19:00 greg138 Sometimes, the alternatives are a little code smell or building a whole new infrastructure to deal with an additional layer of abstraction.
# May 4th 2021, 18:58 tyler.adam.lazenby see i was almost ok with that... but then I got that "code smell" of the what if situation... but I guess code for simplicity and document it well rather than complexity and think you only have to document "use this simple method to do everything from a to z"
# May 4th 2021, 18:51 greg138 This would get you to having a reliable order of records, at least, which then lets you do a loop over the present address records in the template and output fields for them with indexes that will match when you go to patch the entity.
# May 4th 2021, 18:49 greg138 Can you put a sort order column in your address types, and include that in your containment and sort your address records by that?
# May 4th 2021, 18:49 greg138 I'm assuming that you care about the order of the controls on the edit page, that you don't want it to sometimes have mailing address before shipping and sometimes after?
# May 4th 2021, 18:47 greg138 And then there's various ways to go, but maybe a little utility function that reorganizes the array indices for the addresses on an account to match this?
# May 4th 2021, 18:44 greg138 In that case, I think the order of the controls in the template shouldn't need to match the order of the associated records in the entity that you're patching? And they don't need to be sequential either. You could always use .0 in your template for mailing address and .1 for billing and .2 for shipping, for example. If there's no data at all submitted for .1, that's fine for the marshaller.
# May 4th 2021, 18:36 tyler.adam.lazenby For the action, yes I can know the template which types of address are *supposed to be present on the record. And I am using `replace` strategy ... I think*
# May 4th 2021, 18:33 greg138 That doesn't answer either of my questions. :)
# May 4th 2021, 18:07 tyler.adam.lazenby shipping is a POSSIBLE use case
# May 4th 2021, 18:07 tyler.adam.lazenby oh and shipping as well, but that one isn't being used here
# May 4th 2021, 17:55 tyler.adam.lazenby mailing and billing
# May 4th 2021, 17:55 tyler.adam.lazenby So far, only two types of addresses are available, and they are set with a migration seed. I don't plan on making that mutable either
# May 4th 2021, 17:46 greg138 Can you reliably know which types of addresses are *supposed* to be present on the record? Assuming you're using the `replace` save strategy, not `append`?
# May 4th 2021, 17:26 tyler.adam.lazenby because not all accounts are going to have billing addresses since some of them are going to be reseller accounts
# May 4th 2021, 17:25 tyler.adam.lazenby I can't trust the order in which the address are loaded
# May 4th 2021, 17:08 greg138 Or is it because you can't trust the order that the addresses are loaded?
# May 4th 2021, 17:07 greg138 Isn't it then just the label that you need to change, not the column name?
# May 4th 2021, 16:28 tyler.adam.lazenby but the problem is that the first address in the table MIGHT not always be the mailing address.
# May 4th 2021, 16:27 tyler.adam.lazenby I already know how to use this type of syntax ```<?= $this->Form->control('addresses.0.line_one', [ 'class' => 'form-control', 'required', 'label' => __('Address Line One'), ]) ?>```
# May 4th 2021, 16:13 paolo.bragagni Awesome is awesome, but oauth2 plugin is for V.3