I have a custom command class under a DB abstraction bundle I am using to generate the model objects needed for PDO::FETCH_CLASS. Works pretty similar to Doctrine models. The problem I am facing is I need to get the service for the PDO instance, so that I can reuse it on the generated classes. Is there a way to inject something from the Symfony scope to get at the service parameter?
The command I run php bin/console pdo:generate:model <NameOfBundle>, puts a Base/Peer model (similar to Propel) in the the bundle's root inside a directory called Model.
Below is an example of one of the generated DB Peer model:
namespace Ode\AppBundle\Model;
use Ode\AppBundle\Model\HflogsBase;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Hflogs extends HflogsBase implements \JsonSerializable, ContainerAwareInterface {
private $container;
public function __construct() {
parent::__construct();
}
public function setContainer(ContainerInterface $container = null) {
$this->container = $container;
}
public function frequency() {
return number_format($this->frequency, 2, '.', '');
}
/**
* @todo Run another query on the database to acquire a relational element
*/
public function getPdoInstance() {
$pdo = $this->container->get('ode_pdo.db');
}
}
Please, pardon my ignorance of Symfony's structure, but I am at a complete loss on how to get my own classes to inherit said structure's properties.
UPDATE 1
Per request here is the Base model class:
namespace Ode\AppBundle\Model;
class HflogsBase {
const TABLE_NAME = 'hflogs';
const MODEL_NAME = 'Ode\AppBundle\Model\Hflogs';
public $id;
public $frequency;
public $mode;
public $description;
public $time_on;
public $time_off;
public $lat;
public $lng;
public $user_id;
public $submitted;
const COLUMNS = 'a.id,a.frequency,a.mode,a.description,a.time_on,a.time_off,a.lat,a.lng,a.user_id,a.submitted';
public function __construct() {}
}
UPDATE 2
Instead of blowing up the comments below another user asked how I instantiate the peer model class. I basically use it in the controller as PDO's class type for \PDO:FETCH_CLASS. In PDO what this does is instantiate the object, and populates all the row values from a query, so that I have strongly typed results from the DB (it cuts down on time needed debugging loosely formed standard classes or associative arrays results).
So for example, here is an instance of what I do in a controller:
class DefaultController extends Controller
{
/**
* @Route("/test", name="testpage")
*/
public function testAction() {
$logs = $this->get('ode_pdo.db')->query("
SELECT " . HflogsBase::COLUMNS . "
FROM " . HflogsBase::TABLE_NAME . " AS a
WHERE a.id NOT IN (
SELECT id
FROM " . HflogMetaBase::TABLE_NAME . "
WHERE meta_key != 'is_inactive'
AND meta_value = 1
)
")->fetchAll(\PDO::FETCH_CLASS, HflogsBase::MODEL_NAME);
return new Response('');
}
}
The MODEL_NAME constant from HflogsBase refers to the peer class name:
const MODEL_NAME = 'Ode\AppBundle\Model\Hflogs';