Some help with update_record

Some help with update_record

Mark Smith發表於
Number of replies: 8

Please forgive me as I am new to Moodle development (not a new developer, but new to Moodle) and am trying to understand the update_record method a little better. My questions are:

(1) When udpate_record is called (for example, on the user table), is the timemodified column automatically updated as a byproduct of said update?

(2) The documentation generically states that the second parameter is a dataobject. In my case I am using get_record to get the results and then update_record to update back to the database.  I'm trying to locate the "user" class in the moodle 1.9.9 xref and can't seem to locate it (searching under classes).

http://xref.moodle.org

Can you please point me in the right direction? I am assuming if the timemodified is not automatically udpated I can update a property of that object before sending it back to update_record.

 

Thanks much.

評比平均分數: -
In reply to Mark Smith

Re: Some help with update_record

Hubert Chathi發表於

1. I do not believe that the timemodified column is automatically set; you'll have to update it yourself.

2. There is not "user" class.  get_record just returns a generic PHP object.  By the way, make sure to call addslashes_recursive on the object before sending it to update_record (for Moodle 1.9).  Otherwise, special characters in your data may cause errors.

In reply to Mark Smith

Re: Some help with update_record

Richard Crawford發表於

1. Nope. timemodified has to be set manually, as part of your update object.

2. Not sure what you mean with your second point. There's no such thing as a user class in Moodle (not in 1.9+ at least; I'm not sure about 2.0+), so when you call get_record() you just get a basic, generic object. To update the record, you would modify one of the parameters of the object, then use update_record(). For example:

$user = get_record("user","id",1);

$user->username = "new_username";

update_record("user",$user);

...which isn't the same as calling an object of the class User; in this case, we're using a generic object that just happens to be called $user.

If you wanted to make sure the timemodified field is set, you would have to add that line as part of your data object:

$user->timemodified = time();

Hope that makes sense, and that it helps.

In reply to Richard Crawford

Re: Some help with update_record

Tim Hunt發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片

Do not do the get_record then update_record in 1.9! (It fails because of addslashes.)

Do

$userupdate = new stdClass();
$userupdate->id = $user->id;
$userudpate->username = "new_username";
$userupdate->timemodified = time();
update_record('user', $userupdate);

In general, a good way to get the feel for how to use a particular Moodle API function is to search the whole code for places where it is used. Then select some of those that you think were probably written by developers who really know what they were doing, and see what they did.

In reply to Tim Hunt

Re: Some help with update_record

sam marshall發表於
Core developers的相片 Peer reviewers的相片 Plugin developers的相片

Tim's version is of course correct. For 1.9, don't forget to call addslashes on the string fields (->username here) if it comes from a variable or something.

I think this approach is also the correct one for 2.0 as it ensures the database does the minimum work by only updating specific fields. [This may or may not make any difference to performance, depending on the database in use, but it's certainly good practice.]

In other words, in my opinion you should never use the pattern:

1. $x = get_record('table', ...)
2. $x->something = $newvalue;
3. update_record('table', $x)

on any Moodle version. However, in Moodle 2, using this pattern can't actually introduce a security hole, so it's less bad. 微笑

--sam

In reply to Richard Crawford

Re: Some help with update_record

Mark Smith發表於

I guess I am asking where the properties of the user object and values of said properties can be found.

For example, I now need to update the auth column in the database. Do the properties match up 1:1 with the column names in the database and how do I know which values are inside the acceptable parameters?

Thanks, Mark

In reply to Mark Smith

Re: Some help with update_record

Tim Hunt發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片

Yes. object field names map directly on to column names.

That also works if you do something like

$DB->get_records_sql("SELECT col1 AS frog, col2 + col3 AS toad FROM ...");

You will get an array of objects with fields frog and toad (and the array will be indexed by the frog values).

In reply to Tim Hunt

Re: Some help with update_record

Mark Smith發表於

Tim,

In your code earlier:

$userupdate = new stdClass();
$userupdate->id = $user->id;
$userudpate->username = "new_username";
$userupdate->timemodified = time();
update_record('user', $userupdate);

I am assuming user->id might be a value returned from get_record or it could also be a distinct id passed into the method, correct? Your point is that you shouldn't pass the same object into update that was called from get (if I understand correctly). If I also understand update will only update fields existant in the new object.

 

Thanks.

In reply to Mark Smith

Re: Some help with update_record

Tim Hunt發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片

Update use the id column to identify which row to update. (All Moodle tables have an id INT AUTOINCREMENT column as primary key.) It will then use all the other fields on the object that match columns in the table to update that row.

id can come from anywhere. It often comes from the URL. E.g. $id = required_param('user', PARAM_INT);