I have created this Entity :
<?php
namespace MyProject\MyBundle\Entity;
class User {
private $id;
function __construct($id) {
$this->id = $id;
}
public function setName($name) {
$sql = "UPDATE users SET name=:name WHERE id=:id";
$stmt = $this->connection->prepare($sql);
$stmt->execute(array('name' => '$name', 'id' => $this->id));
}
)
How can I connect to the database from it ?
I call this Entity from this controller :
<?php
namespace MyProject\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use MyProject\MyBundle\Entity;
class MyController extends Controller {
public function indexAction() {
$user = new User(1);
$user->setName("Tom");
}
}
There is cookbook article: http://symfony.com/doc/current/cookbook/doctrine/dbal.html which shows how to get and use the dbal connection without going through the entity manager.
But like @Rpg600 I am a bit puzzled at why you are doing such a thing.
You should probably make this a service and inject the connection.
Maybe:
Controller: