Skip to content

Instantly share code, notes, and snippets.

@evercode1
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save evercode1/f72d6f4ba79f4e000097 to your computer and use it in GitHub Desktop.

Select an option

Save evercode1/f72d6f4ba79f4e000097 to your computer and use it in GitHub Desktop.
RecordHelpers Chap 6
<?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'];
}
}
}
@Carsak
Copy link
Copy Markdown

Carsak commented Jun 13, 2015

$userid = Yii::$app->user->identity->id;

Maybe:
$userid = \Yii::$app->user->identity->id;

Forgot backslash before Yii

@pcleau
Copy link
Copy Markdown

pcleau commented Aug 7, 2015

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