Last active
August 29, 2015 14:12
-
-
Save evercode1/f72d6f4ba79f4e000097 to your computer and use it in GitHub Desktop.
RecordHelpers Chap 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace common\models; | |
| use yii; | |
| class RecordHelpers | |
| { | |
| public static function userHas($model_name) | |
| { | |
| $connection = \Yii::$app->db; | |
| $userid = Yii::$app->user->identity->id; | |
| $sql = "SELECT id FROM $model_name WHERE user_id=:userid"; | |
| $command = $connection->createCommand($sql); | |
| $command->bindValue(":userid", $userid); | |
| $result = $command->queryOne(); | |
| if ($result == null) { | |
| return false; | |
| } else { | |
| return $result['id']; | |
| } | |
| } | |
| } |
Hi Bill,
thank you for your amazing book.
I am a beginner, so probably I am wrong, but, for security purpose ("quoting"), is it possible to replace:
$sql = "SELECT id FROM $model_name WHERE user_id=:userid";
by
$sql = "SELECT id FROM {{%" . $model_name . "}} WHERE user_id=:userid";
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$userid = Yii::$app->user->identity->id;
Maybe:
$userid = \Yii::$app->user->identity->id;
Forgot backslash before Yii