除了选定的列之外,如何使用PDO从DB获取所有列到模型?

时间:2018-05-10 16:20:30

标签: php mysql sql

在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;
}

基本上,每次添加或修改数据库中的列时,我都不希望不断地向查询添加内容。我真的想在我将用户信息提取到模型中时自动跳过自动提取的密码。请指出我正确的方向。

0 个答案:

没有答案