2

I am created a listbox and related functions like in my model Appointment.php

Validation Rule & related function in Appointment.php

public function rules()
    {
        return [            
            ['weekdays', 'validateWeekDays'],

        ];
    }

public static function getWeekdaysList()
        {
            return [
                'monday' => 'Monday',
                'tuesday' => 'Tuesday',
                'wednesday' => 'Wednesday',
                'thursday' => 'Thursday',
                'friday' => 'Friday',
                'saturday' => 'Saturday',
                'sunday' => 'Sunday',
            ];
        }
    public function validateWeekdays ($attribute, $params)
        {
            $label = '«' . $this->getAttributeLabel($attribute) . '»';

            // Checking if it's array first
            if (is_array(!$this->$attribute)) {    
                $this->addError($attribute, "$label must be array.");

                return;
            }

            $allowedWeekdays = array_keys(static::getWeekdayslist());

            // Checking if every selection is in list of allowed values
            foreach ($this->$attribute as $weekday) 
            {
                if (!in_array($weekday, $allowedWeekdays)) {
                    $this->addError($attribute, "$label - $weekday is not in allowed list");
                }
            }

Then in my _form.php

<?= $form->field($model, 'weekdays')->listBox(app\models\Appointment::getWeekdaysList(), [
    'multiple' => true,
    'size' => 7,
]) ?> 

Now while saving the form I am getting the error like: Database Exception – yii\db\Exception

PDOStatement::bindValue() expects parameter 3 to be long, string given
Failed to prepare SQL: INSERT INTO `appointment` (`appointment_id`,
`appointment_date`, 
 `patient_detail_id`, `patient_name`, 
`priority`, `doctor_name`, `doctor_fee`, 
`discount`, `weekdays`, `total_amount`, 
`is_paid`, `hospital_commission`) VALUES 
(:qp0, :qp1, :qp2, :qp3, :qp4, :qp5, 
:qp6, :qp7, :qp8, :qp9, :qp10, :qp11)

What I need to do to correct this error. Thanks.

5
  • You can also use Json::encode() to save the array as a json string, which is nice for complicated arrays. Commented Jan 19, 2015 at 19:51
  • Hi deacs, can you please elaborate on that, how can I do that. Commented Jan 19, 2015 at 19:56
  • Hi deacs I am facing a small issue as on update the saved values are not highlighted in the listbox as Saved. Any solution? Commented Jan 19, 2015 at 20:11
  • 1 sec, I'll post a solution for you as an answer Commented Jan 19, 2015 at 20:21
  • 1
    You don't need validateWeekdays, you can simply use in validator... Commented Jan 19, 2015 at 20:46

2 Answers 2

2

Try serializing your array before saving it to the database field (which should be a VARCHAR):

public function beforeSave($insert)
{
    if (parent::beforeSave($insert)) 
    {
        $this->weekdays = serialize($this->weekdays);
        return true;
    }
}

And then unserializing when fetching it again:

public function afterFind()
{
    parent::afterFind();
    $this->weekdays = unserialize($this->weekdays);
}

And make sure you have an array again when you render your form. You could do the same with yii\helpers\Json::encode() and yii\helpers\Json::decode().

Sign up to request clarification or add additional context in comments.

6 Comments

Hi deacs, when creating a new record with your code - I am getting the error like - htmlspecialchars() expects parameter 1 to be string, array given
it is line 96 - public static function encode($content, $doubleEncode = true) { return htmlspecialchars($content, ENT_QUOTES | ENT_SUBSTITUTE, Yii::$app ? Yii::$app->charset : 'UTF-8', $doubleEncode); }
which lines of code that you or I wrote caused the error?
Ok I will check line by line & revert.
Ok on insert part there is no error, but I am getting the value like - a:2:{i:0;s:6:"friday";i:1;s:8:"saturday";}. so it is the unserialize part which is causing the error. As when I am adding the afterfind() I am getting the error.
|
1

Ok all I needed was one function and it works perfectly.

public function beforeSave($insert)
        {       
        $weekday = implode(",", $this->weekdays);
        $this->weekdays = $weekday;
        return parent::beforeSave($insert);
        }

In the controller update action I have added the line

$model->weekdays = explode(',', $model->weekdays);

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.