Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix for editable id columns #610

Closed
wants to merge 5 commits into from

3 participants

@tefra

Currently the id column can't change value, because the function _entityConditions of class lithium\data\model\Query returns a condition based on the new value instead of the old.

tefra added some commits
@tefra tefra Fix for editable id columns
Added function 'stored' for easy access to the currently stored data
a27252b
@tefra tefra Fix for editable id columns
Provide the stored data in the function _entityConditions to get editable id columns
3016730
@travisbot

This pull request fails (merged 3016730 into c3419a4).

@tefra tefra Fix the fix for the editable id column
With the previous fix i managed to break the create, it should be fine now
9a15650
@travisbot

This pull request fails (merged 9a15650 into c3419a4).

@jails
Collaborator

Thanks for your PR, but imho the actual behavior seems correct. In what kind of use case your PR is needed ?

@tefra

You are thinking about tables with auto-increment integer for primary key, in that case yes it's bad practise, but even so to have the following code

$foo = Foo::find(15);
$foo->id = 14;
$foo->save();

produce this

UPDATE foo SET id = 14 WHERE id = 14;

is wrong...

Now about my case i have a table with a varchar pk that i need it to be editable, instead i would have to add an AI int for no reason and use something like this to find them

$foo = Foo::findByAlias('main');
@jails
Collaborator

Makes sense. However this PR seems using 4 spaces instead of tab for indenting your code and break some tests (and would need it's own test case). But it's preferable to wait for @nateabele comments before modifying something ;-).

@travisbot

This pull request fails (merged 7151dd6 into c3419a4).

@travisbot

This pull request fails (merged 110e531 into c3419a4).

@tefra tefra closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 12, 2012
  1. @tefra

    Fix for editable id columns

    tefra authored
    Added function 'stored' for easy access to the currently stored data
  2. @tefra

    Fix for editable id columns

    tefra authored
    Provide the stored data in the function _entityConditions to get editable id columns
  3. @tefra

    Fix the fix for the editable id column

    tefra authored
    With the previous fix i managed to break the create, it should be fine now
Commits on Aug 17, 2012
  1. @tefra

    Fixing issue #613

    tefra authored
  2. @tefra
This page is out of date. Refresh to see the latest.
View
16 data/Entity.php
@@ -400,6 +400,22 @@ public function export() {
);
}
+ /**
+ * Easy access to the currently stored values
+ *
+ * @param string $name Optionally included field name.
+ * @return mixed Entire stored data array if $name is empty, otherwise the value from the named field.
+ */
+ public function stored($name = null){
+ if($this->exists()) {
+ if($name) {
+ return isset($this->_data[$name]) ? $this->_data[$name] : null;
+ }
+ return $this->_data;
+ }
+ return $this->data($name);
+ }
+
/**
* Converts the data in the record set to a different format, i.e. an array.
*
View
2  data/model/Query.php
@@ -520,7 +520,7 @@ protected function _entityConditions() {
if (!$this->_entity || !($model = $this->_config['model'])) {
return;
}
- $key = $model::key($this->_entity->data());
+ $key = $model::key($this->_entity->stored());
if (!$key && $this->_type != "create") {
throw new ConfigException('No matching primary key found.');
View
1  template/view/Renderer.php
@@ -202,6 +202,7 @@ protected function _init() {
},
'options' => '_attributes',
'title' => 'escape',
+ 'value' => 'escape',
'scripts' => function($scripts) use (&$context) {
return "\n\t" . join("\n\t", $context['scripts']) . "\n";
},
View
4 tests/cases/template/helper/FormTest.php
@@ -350,10 +350,10 @@ public function testSubmitGeneration() {
}
public function testTextareaGeneration() {
- $result = $this->form->textarea('foo', array('value' => 'some content'));
+ $result = $this->form->textarea('foo', array('value' => 'some content >'));
$this->assertTags($result, array(
'textarea' => array('name' => 'foo', 'id' => 'Foo'),
- 'some content',
+ 'some content >',
'/textarea'
));
}
Something went wrong with that request. Please try again.