Skip to content

Commit 5346c15

Browse files
committed
Coses issue #39 Add set_expr to Paris
1 parent 92c6e4a commit 5346c15

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.markdown

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Changelog
2929

3030
* Exclude tests and git files from git exports (used by composer)
3131
* Update included Idiorm version for tests
32+
* Implement `set_expr` - closes issue #39
3233

3334
#### 1.2.0 - released 2012-11-14
3435

@@ -355,6 +356,13 @@ To check whether a property has been changed since the object was created (or la
355356

356357
$name_has_changed = $person->is_dirty('name'); // Returns true or false
357358

359+
You can also use database expressions when setting values on your model:
360+
361+
$user = Model::factory('User')->find_one($id);
362+
$user->name = 'Paris';
363+
$user->set_expr('last_logged_in', 'NOW()');
364+
$user->save();
365+
358366
Of course, because these objects are instances of your base model classes, you can also call methods that you have defined on them:
359367

360368
class User extends Model {

paris.php

+11
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,22 @@ public function get($property) {
353353

354354
/**
355355
* Setter method, allows $model->set('property', 'value') access to data.
356+
* @param string|array $key
357+
* @param string|null $value
356358
*/
357359
public function set($property, $value = null) {
358360
$this->orm->set($property, $value);
359361
}
360362

363+
/**
364+
* Setter method, allows $model->set_expr('property', 'value') access to data.
365+
* @param string|array $key
366+
* @param string|null $value
367+
*/
368+
public function set_expr($property, $value = null) {
369+
$this->orm->set_expr($property, $value);
370+
}
371+
361372
/**
362373
* Check whether the given field has changed since the object was created or saved
363374
*/

test/test_queries.php

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ class Widget extends Model {
9393
$expected = "DELETE FROM `widget` WHERE `id` = '1'";
9494
Tester::check_equal("Delete data", $expected);
9595

96+
$widget = Model::factory('Widget')->create();
97+
$widget->name = "Fred";
98+
$widget->age = 10;
99+
$widget->set_expr('added', 'NOW()');
100+
$widget->save();
101+
$expected = "INSERT INTO `widget` (`name`, `age`, `added`) VALUES ('Fred', '10', NOW())";
102+
Tester::check_equal("Insert data containing an expression", $expected);
103+
96104
class Profile extends Model {
97105
public function user() {
98106
return $this->belongs_to('User');

0 commit comments

Comments
 (0)