Comments (26)
9th May 2010, Mark Story said:
Moving to enhancement, as this is something that would require either additional calls to exists() or other drastic changes to how Model::save() works.
from cakephp.
25th Feb 2011, dpavel said:
I called $this->exists() in the conditional as a workaround. For those who, like me, really wanted to use beforeValidate or beforeSave to verify record existence and switch to an update.
from cakephp.
1st Mar 2011, ronin-79226 (at lighthouseapp) said:
the part with exists() can be moved just after the validates() call without breaking model.write test, so you would have at least one callback where the id still is present.
Ofcourse you could also override save() in your model
from cakephp.
2nd Mar 2011, ronin-79226 (at lighthouseapp) said:
ticket #603 relates to this as well, not realy duplicate but if either one will be fixed, the other ticket is worth to be taken in consideration as well
from cakephp.
7th Apr 2011, Joan Coll Cerdán said:
dpavel, that only works with CakePHP 1.2, where $this->exists() writes the result to the Model property '__exists', which is read in the 'save' method, but in CakePHP 1.3 that value isn't stored anywhere and is stored locally in the method 'save'.
In my opinion it should not be necessary to add any additional calls to $this->exists(), and should be up to the developer to ensure the existence of the model if he is tinkering with the model ID.
from cakephp.
7th Apr 2011, ADmad said:
@Joan
You can override Model::exists() in your AppModel and implement whatever you need.
from cakephp.
7th Apr 2011, Joan Coll Cerdán said:
@ADmad
Not really, because in Model::save() the Model::exists() is called and stored in the local variable $exists. The problem relies in the fact that there isn't anymore any Model::$__exists property.
from cakephp.
7th Apr 2011, ADmad said:
@Joan
Right, i forgot that it stores the value in a local variable $exists before triggering the callbacks. But the Model::$__exists had its own problems which is why it was removed.
from cakephp.
8th Apr 2011, Joan Coll Cerdán said:
@ADmad
Yes, I'm aware of that. What do you think if, instead, in Model::save we store in a local variable the original $this->id before calling the callbacks, and then check if it's different before setting it to false? For example:
Model::save, line 1246:
$exists = $this->exists();
$original_id = $this->id;
Model::save, line 1317:
if (!$exists && $count > 0 && $this->id == $original_id) {
$this->id = false;
}
from cakephp.
1st Feb 2012, Mark Story said:
Duplicated in # 2021, which also contains a patch to address the issue.
from cakephp.
The correct path to the other ticket is https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2021 .
Should we close?
from cakephp.
@dereuromark I'm not sure if you are aware that currently only authorized people can open the archived tickets:
This is not a public project
from cakephp.
That's not good. Those should be public and readonly. At least that was the plan.
Only the open tickets have been transferred to Github. Thus the vital information in closed ticket needs to be still accessible by everyone for reference.
from cakephp.
This probably also applies to the CookBook by the way. I can't fully confirm it, because I do have leveraged access to it already. But if that's the case, it means there is currently no access to its issues since they haven't been transfered to GH yet ;-/
In any case I get a login screen when I follow:
https://cakephp.lighthouseapp.com/projects/43067-cookbook/overview
from cakephp.
Is this going to be fixed in Cake 3.0?
from cakephp.
@derekperkins probably not, I'm still having troubles understanding the use case. Are you needing to update the primary key or just update a different record from the one originally passed to save?
from cakephp.
The main use case is to make sure that we're keeping unique records. We're checking for existing records in beforeSave, and if we find a duplicate, we want to update the record instead of creating a new one.
from cakephp.
@dereckperkins oh ok, that is possible already in 3.0
from cakephp.
Awesome. Is that including multi-column primary key duplicates?
from cakephp.
Multi-column primary key support is not totally complete. I'll draft up some ideas about how it might work and post them here and on the cakephp-core mailing list. Associated joins are one of the unclear/tricky parts to them.
from cakephp.
@derekperkins not for associations yet, but it is possible for the primary model.
from cakephp.
@derekperkins Why aren't you doing it in afterValidate()
?
http://book.cakephp.org/2.0/en/models/callback-methods.html#aftervalidate
tl;dr + I didn't try it. Can't you change the id there, too?
from cakephp.
@ravage84 doing it there is a workaround, although it would work fine for 2.x
from cakephp.
This was implemented in version 3
from cakephp.
Hi All,
I am also struggling with same issue, I am trying to set the primary key {id} in beforeSave function , so that , it will fire the 'Update query', however unfortunatly , it is going for insert, and so the below error comes:
Warning (512): SQL Error: 1062: Duplicate entry '1' for key 'PRIMARY' [CORE/cake/libs/model/datasources/dbo_source.php, line 685]
Query: INSERT INTO users
(first_name
, last_name
, email
, id
, modified
, created
) VALUES ('test', 'test', '[email protected]', 1, '2014-12-06 07:46:09', '2014-12-06 07:46:09')
I would highly appreciate, if some one can help me here. I am using cakephp 1.3.20:
My beforesave function is
function beforeSave() {
if (!$this->id && !isset($this->data[$this->alias][$this->primaryKey])) {
if (isset($this->data['User']['first_name']) && isset($this->data['User']['last_name'])){
$this->recursive = -1;
$id = $this->field('id', array('first_name' => $this->data['User']['first_name'],'last_name'=> $this->data['User']['last_name']));
if ($id) {
$this->id = $this->data[$this->alias][$this->primaryKey] = $id;
$this->__exists = true;
$this->set('id', $id);
}
}
}
return true;
}
from cakephp.
You cannot have this sort of functionality with the CakePHP 2.x ORM, only CakePHP 3.
from cakephp.
Related Issues (20)
- I18nExtractCommand ignores --plugin option if --paths is passed
- Run the test, get three errors. HOT 2
- Issue with MSSQL Driver's save() Method on Tables with Triggers Using OUTPUT INSERTED.* HOT 5
- Inflector not correctly observed when baking association tables HOT 2
- jsonSerialize that runs into an Exception does not keep the Exception message, shows generic error instead HOT 8
- RFC: Change default value of the `valueSeparator` argument for `Table::findList()` HOT 1
- BaseErrorHandler::$logger conflicts with PSR-3 $logger HOT 3
- Official way to setup global events which are testable out of the box HOT 1
- \Cake\Core\env() does not return the same value as written in the .env file. HOT 6
- removeBehavior does not remove functions from methodMap
- Add a way to access parent or global data while validating nested resource or associated model
- Retrieving a scalar value that has been bind into the container HOT 1
- $builder->setExtensions not working for me after cakephp 5.0 upgrade HOT 1
- Incompability with PSREXT and CakePHP HOT 11
- Inconsistency for JSON type field between mysql and sqlite HOT 18
- BUG: Fixtures with a connection name other than default will keep searching for the default connection host HOT 4
- Cannot convert Date to DateTime using toDateTimeImmutable HOT 1
- Inflector singularizes `meta` as `metum` HOT 2
- `Inflector::classify` phpdoc is ambiguous HOT 3
- Can't bake Entity with specific name HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cakephp.