Im trying, for the last day or so, to figure out how to use Models with PDO. But without any success. I have made all my code available here: https://github.com/ArneAnka/slim3-skeleton So go ahead and check out what I’m talking about. Currently I’m getting Using $this when not in object context in User.php Here you can review my User.php file.
And I’m injection User.php with the PDO instance from dependencies.php
// -----------------------------------------------------------------------------
// Model factories
// -----------------------------------------------------------------------------
$container['User'] = function ($container) {
return new App\Models\User($container->get('db'));
};
So what I’m doing wrong here? Cant wrap my head around it.
(If I’m not abel to solve this, my next question will be how to add FluentPDO to a slim project )
You cannot use $this in a static method. You either need to change that method to not be static or change the $db property to be static and reference it as a static property.
Yes i noticed my mistake about the static function, and I have corrected that. But still, no luck.
I have made som changes to both HomeController and User model. And when calling $this->user->all() from HomeController, it should return This return should be from a query.... (from the repo).
But I’m receiving Call to a member function all() on unknown
I got things up and running. But I’m not really satisfied with the way things work. Seems like a lot of work to attached the model to the container, then inject the model into the controller. Almost as doing the same thing twice.
There are other ways, but this is the recommended approach. This was you know exactly what your controller needs and exactly where it came from and how it was setup. You can look at PHP-DI which will do some magical stuff for you so you don’t need to do everything explicitly.
I’m not sure what you want to achieve here, since Eloquent wraps PDO. Do you want to execute raw SQL queries? If so, I may have a solution for you (it’s probably far from optimal but it allowed me to migrate an application with lots of raw SQL queries quickly and painlessly).
I implemented the following PDO wrapper class which worked really well for me. I’m not saying that this is an optimal approach, but it allowed me to easily convert a codebase containing lots of raw SQL.
class DBUtil
{
public static function getConnection ()
{
return \Illuminate\Database\Capsule\Manager::connection();
}
public static function select ($sql, $bindings=[], $fetchMode=\PDO::FETCH_ASSOC)
{
if (!$sql) {
throw new \Exception ('Invalid sql statement received');
}
$dbConn = self::getConnection();
$dbConn->setFetchMode ($fetchMode);
$rows = $dbConn->select ($sql, $bindings);
return $rows;
}
public static function selectOne ($sql, $bindings=[], $fetchMode=\PDO::FETCH_ASSOC)
{
if (!$sql) {
throw new \Exception ('Invalid sql statement received');
}
$dbConn = self::getConnection();
$dbConn->setFetchMode ($fetchMode);
$row = $dbConn->selectOne ($sql, $bindings);
return $row;
}
public static function delete ($sql, $bindings)
{
if (!$sql) {
throw new \Exception ('Invalid sql statement received');
}
$dbConn = self::getConnection();
return $dbConn->delete ($sql, $bindings);
}
public static function insert ($sql, $bindings)
{
if (!$sql) {
throw new \Exception ('Invalid sql statement received');
}
$dbConn = self::getConnection();
$dbConn->insert ($sql, $bindings);
return $dbConn->getPdo()->lastInsertId();
}
public static function update ($sql, $bindings)
{
if (!$sql) {
throw new \Exception ('Invalid sql statement received');
}
$dbConn = self::getConnection();
return $dbConn->update ($sql, $bindings);
}
}