Yii Framework Forum: Yii 2.0 Date Input On Form Submition Help - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii 2.0 Date Input On Form Submition Help

#1 User is offline   bambinou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 06-August 13

Posted 11 May 2014 - 11:38 AM

Hi All,

I would like to know if someone could help me with this, I have been struggling for days and cannot find an answer.
I am new to OOP and still struggle a bit with the logic.

I have installed Yii2 on my local machine, all I want is insert a date in the correct format in the database as:
DD/MM/YYYY

I cannot believe how hard it is just ti work this one out as a beginner.......

I tried this:
http://www.yiiframew...behaviors.html:

And added this code inmy Posts Model:


use yii\behaviors\TimestampBehavior;

class User extends ActiveRecord
{
    // ...

    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
            ],
        ];
    }
}




I have the corresponding database fields as created_at and updated_at set as INT.


Unfortunately, when I try to save my form I get this error:
PHP Fatal Error – yii\base\ErrorException

Class 'app\models\ActiveRecord' not found
1. in C:\wamp\www\yii2\models\posts.php at line 61
    }

public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
            ],
        ];
    }



}




Any idea why please?I have followed the Yii website tutorial correctly but still cannot get the date input to work.


Thank you,


Regards,

Ben
0

#2 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 629
  • Joined: 29-August 12

Posted 11 May 2014 - 12:01 PM

View Postbambinou, on 11 May 2014 - 11:38 AM, said:

Any idea why please?I have followed the Yii website tutorial correctly but still cannot get the date input to work.

Set your own value to return in the TimeStampBehavior like below:
public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
                'value' => function($event) {
                    $format = "d/m/Y"; // any format you wish
                    return date($format); 
                }
            ],
        ];
    }
}

0

#3 User is offline   bambinou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 06-August 13

Posted 11 May 2014 - 12:21 PM

Thanks Kartik,

I added the return and still get this
PHP Fatal Error – yii\base\ErrorException

Class 'app\models\ActiveRecord' not found



Thank you,

Ben
0

#4 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 629
  • Joined: 29-August 12

Posted 11 May 2014 - 01:57 PM

View Postbambinou, on 11 May 2014 - 12:21 PM, said:

Thanks Kartik,

I added the return and still get this
PHP Fatal Error – yii\base\ErrorException
Class 'app\models\ActiveRecord' not found



This is a problem in your model class definition (not related to dates at all)... on the top where you are defining your model:

You will have to change this line which you have in your case something like this
class YourModel extends ActiveRecord

to
class YourModel extends \yii\db\ActiveRecord


Ensure your namespaces are correct.
0

#5 User is offline   bambinou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 06-August 13

Posted 11 May 2014 - 02:12 PM

Thanks Kartik,

Yes this is the first thing I checked,hereis my fullcode, this is a new Yii basic app installation:

<?php
namespace app\models;
use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "posts".
 *
 * @property integer $id
 * @property string $title
 * @property string $data
 * @property integer $created_at
 * @property integer $updated_at
 */

class posts extends \yii\db\ActiveRecord
{
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'posts';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'data'], 'required'],
            [['data'], 'string'],
            [['created_at', 'updated_at'], 'integer'],
            [['title'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'data' => 'Data',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }
    
    
    
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
                'value' => function($event) {
                    $format = "d/m/Y"; // any format you wish
                    return date($format); 
                }
            ],
        ];
    }
}

0

#6 User is offline   Kartik V 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 629
  • Joined: 29-August 12

Posted 11 May 2014 - 02:22 PM

The following lines are not namespaced:
'attributes' => [
     ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
     ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
],

You can use this following line at the top of your class
use yii\db\ActiveRecord;

This should resolve your issue.
0

#7 User is offline   bambinou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 06-August 13

Posted 11 May 2014 - 04:18 PM

View PostKartik V, on 11 May 2014 - 02:22 PM, said:

The following lines are not namespaced:
'attributes' => [
     ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
     ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
],

You can use this following line at the top of your class
use yii\db\ActiveRecord;

This should resolve your issue.




Thanks again Kartik,


Yes it worked but I had to had another line(as shown below) and letme explain where I really struggled, I kept having the format in the database as YY/MM/DD and thought...what am I doing wrong???
A programmer has just told me that we cannot store the date as DD/MM/YY in mysql, it has to be the other butinstead we have to reformat the view side of of the date on the output...I had no idea...


public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
                'value' => new Expression('NOW()'),
            ],
        ];
    }

}



So I was looking at this page:
http://www.yiiframew...meFormat-detail


But it does not give any great info on what can be added to:

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'datetimeFormat' => ['d-m-Y H:i:s'],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'title',
            'data:ntext',
            'created_at',
            'updated_at',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>


to make it look as DD/MM/YYYY

I can see this line that says:
$datetimeFormat public property
string $datetimeFormat = 'Y-m-d H:i:s'

But how to interpret this please? I am trying to understand the concept... I tried to add it as this:

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'dateFormat' => ['d-m-Y'],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'title',
            'data:ntext',
            'created_at',
            'updated_at',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>




But this causes an error:
#Unknown set property.



Thanks,

Ben
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users