Code Monkey home page Code Monkey logo

Comments (26)

 avatar commented on July 16, 2024

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.

 avatar commented on July 16, 2024

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.

 avatar commented on July 16, 2024

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.

 avatar commented on July 16, 2024

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.

 avatar commented on July 16, 2024

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.

 avatar commented on July 16, 2024

7th Apr 2011, ADmad said:


@Joan You can override Model::exists() in your AppModel and implement whatever you need.

from cakephp.

 avatar commented on July 16, 2024

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.

 avatar commented on July 16, 2024

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.

 avatar commented on July 16, 2024

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.

 avatar commented on July 16, 2024

1st Feb 2012, Mark Story said:


Duplicated in # 2021, which also contains a patch to address the issue.

from cakephp.

dereuromark avatar dereuromark commented on July 16, 2024

The correct path to the other ticket is https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2021 .
Should we close?

from cakephp.

ravage84 avatar ravage84 commented on July 16, 2024

@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.

dereuromark avatar dereuromark commented on July 16, 2024

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.

ravage84 avatar ravage84 commented on July 16, 2024

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.

derekperkins avatar derekperkins commented on July 16, 2024

Is this going to be fixed in Cake 3.0?

from cakephp.

lorenzo avatar lorenzo commented on July 16, 2024

@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.

derekperkins avatar derekperkins commented on July 16, 2024

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.

lorenzo avatar lorenzo commented on July 16, 2024

@dereckperkins oh ok, that is possible already in 3.0

from cakephp.

derekperkins avatar derekperkins commented on July 16, 2024

Awesome. Is that including multi-column primary key duplicates?

from cakephp.

markstory avatar markstory commented on July 16, 2024

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.

lorenzo avatar lorenzo commented on July 16, 2024

@derekperkins not for associations yet, but it is possible for the primary model.

from cakephp.

ravage84 avatar ravage84 commented on July 16, 2024

@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.

lorenzo avatar lorenzo commented on July 16, 2024

@ravage84 doing it there is a workaround, although it would work fine for 2.x

from cakephp.

lorenzo avatar lorenzo commented on July 16, 2024

This was implemented in version 3

from cakephp.

ib-anurag-mishra avatar ib-anurag-mishra commented on July 16, 2024

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.

josegonzalez avatar josegonzalez commented on July 16, 2024

You cannot have this sort of functionality with the CakePHP 2.x ORM, only CakePHP 3.

from cakephp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.