2016-05-13 56 views
0

我在重构我的应用程序的某些部分,并将fetch(PDO::FETCH_ASSOC)替换为fetchObject()的专用类。访问fetchObject()中的PDO连接 - 实例

有没有办法在生成的类中访问pdo实例?看到这个例子:

class User 
{ 
    private $id; 
    private $username; 
    private $firstname; 
    private $lastname; 

    public function setFirstname($newValue) 
    { 
     if (empty($newValue) || !is_string($newValue)) throw new Exception('Wrong'); 
     $this->firstname = $newValue; 
    } 

    public function save() 
    { 
     // IMPORTANT PART: 
     // I don't want to use the global object container here 
     $dbh = Application::getInstance()->getPDO(); 

     $sth = $dbh->prepare('UPDATE main_user SET firstname = :firstname WHERE id = :id'); 
     $sth->execute([ 
      'id'  => $this->id, 
      'firstname' => $this->firstname, 
     ]); 
    } 
} 

$dbh = $application->getPDO(); 
$sth = $dbh->prepare('SELECT * FROM main_user WHERE id = ?'); 
$sth->execute([ 10 ]); 
$user = $sth->fetchObject('User'); 
$user->setFirstname('Test'); 
$user->save(); 

我的应用程序的部分使用多个数据库,因此多个pdo对象。为了获得可重用的代码,我想阻止使用我的全局容器类 - 当然还有global

+1

创建一个类成员(例如'User :: $ conn')并将PDO连接作为参数传递给构造函数以进行设置? – CD001

回答

4

您可以通过PDO实例,因此您不必再次调用Application :: getInstance() - > getPDO()

public function save(PDO $dbh) 
{ 
    $sth = $dbh->prepare('UPDATE main_user SET firstname = :firstname WHERE id = :id'); 
    $sth->execute([ 
     'id'  => $this->id, 
     'firstname' => $this->firstname, 
    ]); 
} 
// and pass the (already available) $dbh to the save-Method at the buttom 
$user->save($dbh); 

CD001在评论中提到的,你也可以将它传递给构造函数:

class User 
{ 
    // ... 
    private $conn; 

    public function __construct(PDO $conn) 
    { 
     $this->conn = $conn; 
    } 
} 
// ... later at the bottom when fetching the Object: 
$user = $sth->fetchObject('User', [ $dbh ]); 
+0

想过用'save()'传递它,但是如果在构建之后那样,可能没有显着的连接。我会和构造参数一起去看,看起来非常好。谢谢! – clemens321

1

你应该有一个用户库,将用户保存到数据库:

use \Path\To\User\Model\User; 

class UserRepository extends AbstractRepository 
{ 
    public function save(User $user) 
    { 
     $sth = $this->db->prepare('UPDATE main_user SET firstname = :firstname WHERE id = :id'); 
     $sth->execute([ 
      'id'  => $user->id, // make sure these properties are public on the model 
      'firstname' => $user->firstname, 
     ]); 
    } 
} 

class AbstractRepository 
{ 
    protected $db; 

    // inject it using Dependency Injection 
    public function __construct(DbInterface $db) 
    { 
     $this->db = $db; 
    } 
} 

然后,在您的模型中,您可以使用用户存储库将其保存到db:

namespace \Path\To\User\Model; 

class User 
{ 
    public $id; 
    public $username; 
    public $firstname; 
    public $lastname; 

    protected $userRepository; 

    // inject it with DI 
    public function __construct(UserRepository $userRepository) 
    { 
     $this->userRepository = $userRepository; 
    } 

    public function setFirstname($firstname) 
    { 
     if (empty($firstname) || !is_string($firstname)) { 
      throw new Exception('Wrong firstname!'); 
     } 

     $this->firstname = $firstname; 

     return $this; 
    } 

    public function save() 
    { 
     return $this->userRepository->save($this); 
    } 
} 

现在,你需要这些类注册到一个依赖注入框架如PHP-DI,并正确地运用..

$user = $container->get('User'); 
$user->setFirstname('MyUser'); 
$user->create(); 

如果你重构你的应用程序,那么你应该做的是正确的。

请注意,上面的示例仅供参考,它们需要许多改进才能用于生产。