# |
Feb 3rd 2018, 18:00 |
kailas |
I’ve always been a bit challenged when it comes to saving data to a join table. I’ve got a pretty simple sample now that is not working as I would expect. see https://gist.github.com/anonymous/08e620154ea7bd30fe59291179b80eab X3Invoices->belongToMany(‘TaxItems’). In my X3Invoice entity, i have a method to recalculate taxes. I’m looping through tax items, and calculating the tax (_joinData->amount) for each. in my debug sta |
# |
Feb 3rd 2018, 18:00 |
sayed |
…. |
# |
Feb 3rd 2018, 18:00 |
sayed |
it did validate on the next attempt of inserting data |
# |
Feb 3rd 2018, 18:00 |
sayed |
but by inserting a new row of data |
# |
Feb 3rd 2018, 18:00 |
sayed |
I had cleared the cache a couple of times but that wasnt it |
# |
Feb 3rd 2018, 18:00 |
sayed |
hmm thanks but i think i kinda fixed it (by doing nothing) ,:) |
# |
Feb 3rd 2018, 17:57 |
hmic |
you should rather show your table i think |
# |
Feb 3rd 2018, 17:56 |
hmic |
sayed: i can't see anything on that piece of code |
# |
Feb 3rd 2018, 17:52 |
popperz0r |
anyone knows how to add model associations to Cakedc\Users? |
# |
Feb 3rd 2018, 17:33 |
sayed |
https://gist.github.com/anonymous/29c3f8413286d46524ea7b372c82d2c3 |
# |
Feb 3rd 2018, 17:32 |
sayed |
Any ideas are welcome :slightly_smiling_face: ! |
# |
Feb 3rd 2018, 17:32 |
slackebot |
return $this->error($e, !empty($this->entity) ? $this->entity->getErrors() : []); } }``` |
# |
Feb 3rd 2018, 17:32 |
sayed |
``` public function add() { try { $this->entity = $this->items->newEntity(); $this->entity = $this->items->patchEntity($this->entity, $this->getData()); if (!$this->items->save($this->entity)) { throw new Exception('The ' . $this->name . ' could not be saved. Please, try again.'); } $this->set('item', $this->entity); } catch (Exception $e) { |
# |
Feb 3rd 2018, 17:32 |
sayed |
I am patching the entity just based on the requested data, nothing special going on there. |
# |
Feb 3rd 2018, 17:27 |
sayed |
https://gist.github.com/SayedShahidi/70ea73a2f02c088ddbfc64673ae380e1 |
# |
Feb 3rd 2018, 17:25 |
sayed |
https://gist.github.com/SayedShahidi/5be6b8ce4acc29750e7685d7ec7b08bb |
# |
Feb 3rd 2018, 17:24 |
hmic |
!tell sayed about gist |
# |
Feb 3rd 2018, 17:24 |
sayed |
``` -- auto-generated definition CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, guid VARCHAR(255) NOT NULL, emailadres VARCHAR(255) NOT NULL, CONSTRAINT guid UNIQUE (guid), CONSTRAINT emailadres UNIQUE (emailadres) ) ENGINE = InnoDB; ``` |
# |
Feb 3rd 2018, 17:21 |
sayed |
Sure, just a sec |
# |
Feb 3rd 2018, 17:20 |
sayed |
?? |
# |
Feb 3rd 2018, 17:19 |
hmic |
provide the data by different means |
# |
Feb 3rd 2018, 17:19 |
hmic |
i'm not on slack |
# |
Feb 3rd 2018, 17:18 |
hmic |
!fail |
# |
Feb 3rd 2018, 17:18 |
sayed |
kinda strange ,:) |
# |
Feb 3rd 2018, 17:18 |
sayed |
no |
# |
Feb 3rd 2018, 17:18 |
sayed |
File uploaded https://cakesf.slack.com/files/U8SE5F2NN/F949JJQ0N/screen_shot_2018-02-03_at_18.17.37.png / https://slack-files.com/T053DPNCM-F949JJQ0N-52f86cc5e9 |
# |
Feb 3rd 2018, 17:17 |
hmic |
is the guid setup as the primarykey for that table too? |
# |
Feb 3rd 2018, 17:17 |
sayed |
field |
# |
Feb 3rd 2018, 17:16 |
sayed |
and oww yeah unique also works on the emailadres |
# |
Feb 3rd 2018, 17:16 |
sayed |
``` public function buildRules(RulesChecker $rules) { $rules->add($rules->isUnique(['guid'])); $rules->add($rules->isUnique(['emailadres'])); return $rules; } ``` |
# |
Feb 3rd 2018, 17:16 |
sayed |
yes |
# |
Feb 3rd 2018, 17:16 |
hmic |
do you have rules setup too, for the validateunique to work? |
# |
Feb 3rd 2018, 17:16 |
sayed |
Any ideas ? |
# |
Feb 3rd 2018, 17:16 |
sayed |
but not for unique, it just goes through and mysql itself gives out an error, since the column is marked as unique |
# |
Feb 3rd 2018, 17:15 |
sayed |
and the thing that i really dont get is, if i for example leave the `guid` field empty, it does validates it as required |
# |
Feb 3rd 2018, 17:14 |
sayed |
but it passes through |
# |
Feb 3rd 2018, 17:14 |
sayed |
In my users table i am clearly checking the item with the validator |
# |
Feb 3rd 2018, 17:14 |
sayed |
``` $validator ->scalar('guid') ->maxLength('guid', 255) ->requirePresence('guid', 'create') ->notEmpty('guid') ->add('guid', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); ``` |
# |
Feb 3rd 2018, 17:13 |
sayed |
``` { "message": "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry \u0027290b0b55-01fc-4c7e-a8b8-1234234\u0027 for key \u0027guid\u0027", "url": "\/api\/users\/add", "code": 500, "file": "\/Users\/sayedshahidi\/sites\/rendement\/vendor\/cakephp\/cakephp\/src\/Database\/Statement\/MysqlStatement.php", "line": 39 } ``` |
# |
Feb 3rd 2018, 17:13 |
sayed |
``` |
# |
Feb 3rd 2018, 17:12 |
sayed |
fields * |