在laravel中,您可以在模型中声明一些不会获取的属性,如何在纯php/sql中实现这一点?我的控制器方法如下所示:
public function getUserByUsername($username)
{
$db = new Db();
$query = $db->conn->prepare("SELECT id, userName, firstName, lastName, phoneNumber, userType, compliant, emailVerified, phoneVerified FROM apiAccount WHERE userName = :userName");
$query->bindParam(':userName', $username);
if($query->execute())
{
$db = null;
$query->setFetchMode(PDO::FETCH_CLASS, 'AccountModel');
$result = $query->fetchAll();
if (isset($result[0])) {
$db = null;
return $result[0];
} else {
$db = null;
throw new APIException('Query could not be executed, user not found', 600, null, 'User does not exist');
}
}
else
{
$db = null;
throw new APIException('Query could not be executed', 600, null, 'Dodgy SQL');
}
}
我的模特:
class AccountModel extends BaseModel
{
public $id;
public $userName;
public $firstName;
public $lastName;
public $phoneNumber;
public $userType;
public $compliant;
public $emailVerified;
public $phoneVerified;
}
基本上,我不想每次添加或修改数据库中的列时都不断地向查询中添加内容。我只是真的想跳过密码被自动提取时,我把用户信息到我的模型。请给我指出正确的方向。
1条答案
按热度按时间5vf7fwbs1#
一种方法是向模型中添加一个构造函数,该构造函数接受一个数组并只设置其存在的属性。
然后获取一个关联数组并从中构造模型的示例。