Here is how to define your model's attributes:
class Author extends SimpleAR\Model
{
protected static $_columns = array(
'first_name',
'last_name',
'years_since_birth',
);
}
But we can see that "years_since_birth" is not really appropriate. "age" would be a better wording:
protected static $_columns = array(
'first_name',
'last_name',
// <attribute name> => <column name>
'age' => 'years_since_birth',
);
Note: A better solution might be to change the field name in database. But sometimes, you are not able to change it.
SimpleAR\Model class provides some methods to get and set attributes.
You can retrieve every object's attribute with their values with the
attributes()
method:
$author = Author::create(['first_name' => 'John', 'age' => 43]);
print_r($author->attributes());
// Result:
// array(
// 'id' => 1,
// 'first_name' => 'John',
// 'last_name' => null,
// 'age' => 43,
// );
You can use get()
and set()
methods to respectively get and set several
attributes at once.
Very handy!
Use columns()
static method to retrieve an associative array between attribute
names and column names.
Example with Author
model:
print_r(Author::columns());
// Result:
// array(
// 'id' => 'id',
// 'first_name' => 'first_name',
// 'last_name' => 'last_name',
// 'age' => 'years_since_birth',
// );
You can define custom getters and setters method for specific attribute that will be called when trying to access to the attribute.
public function set_password($raw)
{
$this->_attr('password', md5($raw));
}
// ...
// Then, you set `password` attribute this way:
$author->password = 'yeah!';
Note: Use `_attr()` method inside getters and setters to prevent infinite loop.
You can write a getter or setter for attribute that does not “exist”:
public function get_name()
{
return $this->first_name . ' ' . $this->last_name;
}
And then use it in a view as in following example:
<p>
Hello, <?php $user->name; ?>!
</p>
You can define a model-wide sort ordering when retrieving several instances from
database. Take a Country
model that modelizes a country. In your application,
you always want to retrieve countries sorted in alphabetical order.
class Country extends SimpleAR\Model
{
protected static $_orderBy = array(
'name', // Equivalent to: 'name' => 'ASC'
);
}
// Result will automatically be sorted.
Country::all();
Use `$_orderBy` class attribute to define a global ordering.
You can define model-wide conditions. It means that these conditions will be applied to every query made on this model.
// Let's take Article model for an example. On your blog, you never want offline
// articles.
Article::setGlobalConditions(['status' => 'online']);
This way, the condition "status is online" will be applied to each query made over Article model.
You can use `MyModel::getGlobalConditions()` to get current global conditions of `MyModel`.
SimpleAR provides a collection of callback methods that you can overwrite to take control of your model instance.
_onBeforeLoad()
;_onAfterLoad()
;_onBeforeInsert()
;_onAfterInsert()
;_onBeforeUpdate()
;_onAfterUpdate()
;_onBeforeDelete()
;_onAfterDelete()
.