unknownbits
unknownbits

Reputation: 2885

yii2 model str_replace() for all attributes

hello 's gets saved in the db as hello \'s

$eventDetail = Event::find()
                ->joinWith('eventQuestion', true)
                ->joinWith('eventQuestion.questionOption', true)
                ->where(['=','event.id',$id])
                ->one();

All data containing \' should be replace with '

What should I change in the model(s) to handle that?

Upvotes: 0

Views: 1087

Answers (2)

Anamika Shrivastava
Anamika Shrivastava

Reputation: 713

In Yii2, if we add any value with single quote, by default it add without backslash as you want to show in your view page.

Are you converting the single quote with backslash before saving or its implicitly changing?

If its changing implicitly, you can check magic_quote_gpc setting in you PHP.ini. It should be off. If your php version is less than 5.4 then you should change the magic_quote setting. if your PHP version is > 5.4 then there should be some other error.

Upvotes: 0

Michael Nguyen
Michael Nguyen

Reputation: 1752

I would do

public function retrieveById() {
$eventDetail = Event::find()
                ->joinWith('eventQuestion', true)
                ->joinWith('eventQuestion.questionOption', true)
                ->where(['event.id' =>$id])
                ->one();

// this or do a for loop if you want to loop through $eventDetail
$eventDetail->description = str_replace("\'","'", $eventDetail->description);
}

Upvotes: 1

Related Questions