Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamp Behavior #235

Closed
resurtm opened this issue May 11, 2013 · 12 comments
Closed

Timestamp Behavior #235

resurtm opened this issue May 11, 2013 · 12 comments
Assignees
Labels
status:under development Someone is working on a pull request. type:feature
Milestone

Comments

@resurtm
Copy link
Contributor

resurtm commented May 11, 2013

@qiangxue, @samdark, what do you think about the idea of developing good old well-known timestamp behavior in Yii 2.0 too? :-)

@samdark
Copy link
Member

samdark commented May 11, 2013

Don't think it's a good idea since the amount of code required to implement it nearly equals the amount of code to attach behavior.

@resurtm resurtm closed this as completed May 11, 2013
@armpogart
Copy link

The difference between behavior and implementing it in core is obvious. Many beginner coders are expecting that to be core functionality of the modern framework and I can say from my programming experience, that when I was still learning and was very new to yii I didn't know anything about behaviors and didn't bother to look at them, but I have used core functionality to the fullest from the first days.

Another argument is that many modern ORMs and frameworks implement this in core.

@resurtm
Copy link
Contributor Author

resurtm commented May 13, 2013

Old well-known timestamp behaviour ported to 2.0: https://gist.github.com/resurtm/5567517 (might be helpful for someone).

@bwoester
Copy link
Contributor

And another request about this: http://www.yiiframework.com/forum/index.php/topic/43225-activerecord-timestamps/

Just opened another suggestion for a behavior that might be helpful for some. #249 Revision count behavior.
Maybe we can bundle a few behaviors to showcase the feature.

@samdark
Copy link
Member

samdark commented May 13, 2013

Yeah, showcasing is important. We may reconsider it.

@creocoder
Copy link
Contributor

Vote to add that behavior to core.

@samdark samdark reopened this May 13, 2013
@qiangxue
Copy link
Member

Class name suggestion: yii\behaviors\AutoTimestamp

@cajoy
Copy link
Contributor

cajoy commented May 14, 2013

Vote to add to the core as well...

@qiangxue
Copy link
Member

Done.

@slavcodev
Copy link
Contributor

Suggest newest behavior, to extends count of timestamp attributes

class AutoTimestamp extends Behavior
{
    public $attributes = array(
        'create_time' => ActiveRecord::EVENT_BEFORE_INSERT,
        'update_time' => ActiveRecord::EVENT_BEFORE_UPDATE,
    );

    public $timestamp;

    public function events()
    {
        $events = array();

        foreach ($this->attributes as $attribute => $event) {
            $events[$event] = function($event) use ($attribute) {
                $event->sender->updateTimestamp($attribute);
            };
        }

        return $events;
    }

    public function updateTimestamp($attribute)
    {
        $this->owner->$attribute = $this->evaluateTimestamp($attribute);
    }

    protected function evaluateTimestamp($attribute)
    {
        if ($this->timestamp instanceof Expression) {
            return $this->timestamp;
        } elseif ($this->timestamp !== null) {
            return call_user_func($this->timestamp);
        } else {
            return time();
        }
    }
}

@samdark
Copy link
Member

samdark commented May 17, 2013

@slavcodev good idea. @yiisoft/core-developers I think having more than two timestamp fields is possible.

@qiangxue
Copy link
Member

Thank you for your suggestion. Done via e69afe3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:under development Someone is working on a pull request. type:feature
Projects
None yet
Development

No branches or pull requests

8 participants