Wrap text
Report abuse
|
|
belongs_to("user");
$t->string("title", "permalink");
$t->text("content");
$t->datetime("published_at");
$t->timestamps();
});
}
static function down()
{
static::drop_table("articles");
}
}
/**
*
* $article = new Article();
* // => #
*
* $article->title = "test title";
* $article->content = "test content";
* // => #
*
* $article->save();
* #
*
*
*/
class Article extends ActiveRecord::Base
{
// should give me:
// Article::recent->find("all") => returns the 10 most recent articles
// Article::published->find("all") => returns all articles which are published
// Article::recent->published->find("all") => returns the 10 most recent, published articles
static $named_scope = array(
array("recent", "limit" => 10, "order" => "published_at DESC"),
array("published", "conditions" => function() {
return array("published_at <= ?", time());
})
);
// $article->user;
// would return the User row with this article's user_id as an ActiveRecord Object
static $belongs_to = array("user");
// would make sure that title and content fields are not Empty
static $validates_presence_of = array('title', 'content');
// would run "update_published_at" before saving any Article Object
static $before_save = array("update_published_at");
function update_published_at()
{
if ($this->draft)
{
$this->write_attribute("published_at", null);
}
}
function is_draft()
{
return !$this->is_new_record() && $this->published_at;
}
function set_draft($value)
{
if ($value == "1")
{
$this->draft = true;
}
}
}
|