Syntax for setting mysql date in a model?

Stupid question. I have a Model, and I am trying to set a date field in the controller. What is the syntax for that? I am trying:

$myobj = MyModel::findFirstById($id);
$myobj->setMyDate(date('Y-m-d'));
$myobj->update();

and I am getting the error message "The record doesn't have a valid data snapshot"



30.9k
edited Oct '14

Hey man

I'm not sure, but recommend you should set date in model with two method default of Phalcon

    public function beforeValidationOnCreate()
    {
        $this->created = time();

    }
      /**
     * Sets the timestamp before update the confirmation
     */
    public function beforeValidationOnUpdate()
    {
        $this->created = time();
    }

http://docs.phalconphp.com/en/latest/reference/models.html#events-and-events-manager



14.2k

I am not setting a created date though. Users create a record, work on it, and then submit it when they're done making changes. So it doesn't make sense for me to set this date every time I update. It's a single discrete event that I am trying to capture, not a last-updated-date.



30.9k

Ok

In my controller

/**
     * @return \Phalcon\Http\ResponseInterface
     */
    public function saveAction()
    {
        //  Is not $_POST
        if (!$this->request->isPost()) {
            $this->view->disable();

            return $this->response->redirect($this->router->getControllerName());
        }

        $id = $this->request->getPost('id', 'int', null);
        $date = date('Y-m-d H:i:s');

        if (!empty($id)) {
            $object = User::findFirstById($id);
        } else {
            $object = new User;
            $registerHash = md5(uniqid(rand(), true));
            $randomPasswd = substr(md5(microtime()), 0, 7);

            $object->setIdRole(2); //Role::ROLE_ADMINISTRATOR
            $object->setPasswd($this->security->hash($randomPasswd));
            $object->setRegisterhash($registerHash);
            $object->setDateAdd($date);
            $object->setStatus(User::STATUS_PENDING);
        }
        $object->setGender($this->request->getPost('gender'));
        $object->setDateUpd($date);

        $form = new UserForm($object);
        $form->bind($_POST, $object);

        //  Form isn't valid
        if (!$form->isValid($this->request->getPost())) {
            foreach ($form->getMessages() as $message) {
                $this->flashSession->error($message->getMessage());
            }

            // Redirect to edit form if we have an ID in page, otherwise redirect to add a new item page
            return $this->response->redirect(
                $this->router->getControllerName() . (!is_null($id) ? '/edit/' . $id : '/new')
            );
        } else {
            if (!$object->save()) {
                foreach ($object->getMessages() as $message) {
                    $this->flashSession->error($message->getMessage());
                }

                return $this->dispatcher->forward(
                    ['controller' => $this->router->getControllerName(), 'action' => 'new']
                );
            } else {
                $this->flashSession->success(t('Data was successfully saved'));

                return $this->response->redirect($this->router->getControllerName());
            }
        }
    }

Are you sure format date in Mysql $date = date('Y-m-d H:i:s');



14.2k

Hrm I think actually my problem is something else. I am closing this one



16.8k

And did you just tried $myobj->date = "YYYY-MM-DD"? I never had problems when setting the date in new MySQL records.

Dependes on your field, is it Date or DateTime?

Date = Y-m-d

DateTime = Y-m-d H:i:s



1.4k

i think you can use timestamp behavior

public function initialize()
    {
        $this->addBehavior(new Timestampable(array(
            'beforeValidationOnCreate' => array(
                'field' => 'created_at',
                'format' => 'Y-m-d H:i:s'
            )
        )));
    }