3093 Posts in 875 Topics by 654 members
| Go to End | Next > | |
| Author | Topic: | 1700 Views |
-
DataObject won't update

11 December 2010 at 9:27pm Last edited: 11 December 2010 10:04pm
I'm trying to update the property of a DataObject and it's just not having it. The DataObject is a sub class of another class extending DataObject if that matters. The objects property updates fine but doesn't save to the database. Am I missing something?
$results = DataObject::get("User", "SHA1(`Username`) = '{$activationKey}'", "", "", "1");
if($results->TotalItems() == 1)
{
$results = $results->toArray();$results[0]->Activated = true;
$results[0]->write();
}[EDIT] The DataObject will update its own properties but not the parent objects, guess that makes sense but how can I update the parent objects properties?
-
Re: DataObject won't update

11 December 2010 at 11:53pm Last edited: 11 December 2010 11:58pm
Hello,
I have a similar problem in Silverstripe 2.4.3, i am using a custom independent Controller for a widget and from the post data i retrieve the values to update a newly created DataObject but that does not work. If i use the widgets Controller it can only update the DataObject by the $form->saveInto(CustomDataObject); and the write.
This is my code segment:
$data = $this->requestParams;
if (isset($data) && is_array($data) && (count($data) > 0) ) {
$subscriber = new CustomDataObject();$subscriber->FullName = "Test"; //hardcoded for test purposes only. It must be the $data['FullName']
$subscriber->Email = "test@example.com"; //hardcoded for test purposes only. It must be the $data['Email']$subscriber->write();
}It does write the object to the database but without updating the fields the FullName and Email. I also tried it with update($data) but that does not work either.
Thanks
-
Re: DataObject won't update

12 December 2010 at 12:22am
Well I solved mine, I could have sworn I tried this but here's the new code...
$results = DataObject::get("User", "SHA1(`Username`) = '{$activationKey}'", "", "", "1");
if($results->TotalItems() == 1)
{
$results = $results->toArray();$results[0]->Activated = 'true'; //add single quotes around boolean true to make it a string
$results[0]->write();
} -
Re: DataObject won't update

12 December 2010 at 10:11am
Question as a aside
$results = DataObject::get("User", "SHA1(`Username`) = '{$activationKey}'", "", "", "1");
Any reason why you're not using get_one() which will get 1 object and you won't get it wrapped in a set (usually don't need a set for 1 object)
$results = DataObject::get_one("User", "SHA1(`Username`) = '{$activationKey}'");
if($results) {
$results->Activated = 'true';
$results->write();
} -
Re: DataObject won't update

12 December 2010 at 3:09pm
"Any reason why you're not using get_one()", I'd really like to say yes but no, I didn't see get_one in the documentation, thank you very much for pointing it out! much appreciated
-
Re: DataObject won't update

13 December 2010 at 9:57pm Last edited: 13 December 2010 11:04pm
Hi guys,
Thanks Willr for the interest in the thread.
Nice to see that your problem got solved, unfortunately mine did not. Anyway, I am curious what was the source of your problem, did you set the Activated property as string in you User data object (db property as 'Varchar' or 'Text') ?
Regarding my problem, does anyone has any idea what could it be?
ThanksP.S.: Good luck netnerd85 with your project.
-
Re: DataObject won't update

13 December 2010 at 10:32pm
Hi again,
Ok, I did got my problem solved.
The issue was with the fact that I've added getters and setters to my CustomDataObject (actually the problem was the setters).
This was my mistake, but I did not found anywhere any warning about this so why is it not OK?
Thanks
-
Re: DataObject won't update

14 December 2010 at 6:17pm Last edited: 14 December 2010 6:17pm
did you set the Activated property as string in you User data object (db property as 'Varchar' or 'Text') ?
If Activated is either true or false, Boolean would be a better type to use than Varchar.
| 1700 Views | ||
| Go to Top | Next > |


