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");
}
}
2 个解决方案
#1
3
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.
有一本食谱文章:http://symfony.com/doc/current/cookbook/doctrine/dbal.html,它展示了如何在不通过实体经理的情况下获取和使用dbal连接。
But like @Rpg600 I am a bit puzzled at why you are doing such a thing.
但就像@ Rpg600一样,我有点疑惑你为什么这么做。
You should probably make this a service and inject the connection.
您应该将此服务作为服务并注入连接。
Maybe:
class UserCustomQuery($conn)
{
public function __construct($conn) { $this->conn = $conn; }
public function setName($userId,$name)
{
....
Controller:
$userCustomQuery = $this->get('user.custom.query');
$userCustomQuery->setName(1,'Tom');
#2
2
You need to use the doctrine Entity Manager http://symfony.com/doc/current/book/doctrine.html#persisting-objects-to-the-database
您需要使用doctrine Entity Manager http://symfony.com/doc/current/book/doctrine.html#persisting-objects-to-the-database
EDIT: Don't do you query in the model, but inside the entity repository like this:
编辑:不要在模型中查询,但在实体存储库中查询如下:
public function setName(name, id)
{
$sql = "UPDATE users SET name=:name WHERE id=:id";
$stmt = $this->getEntityManager()
->getConnection()
->prepare($sql);
$stmt->execute(array('name' => $name, 'id' => $id));
}
#1
3
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.
有一本食谱文章:http://symfony.com/doc/current/cookbook/doctrine/dbal.html,它展示了如何在不通过实体经理的情况下获取和使用dbal连接。
But like @Rpg600 I am a bit puzzled at why you are doing such a thing.
但就像@ Rpg600一样,我有点疑惑你为什么这么做。
You should probably make this a service and inject the connection.
您应该将此服务作为服务并注入连接。
Maybe:
class UserCustomQuery($conn)
{
public function __construct($conn) { $this->conn = $conn; }
public function setName($userId,$name)
{
....
Controller:
$userCustomQuery = $this->get('user.custom.query');
$userCustomQuery->setName(1,'Tom');
#2
2
You need to use the doctrine Entity Manager http://symfony.com/doc/current/book/doctrine.html#persisting-objects-to-the-database
您需要使用doctrine Entity Manager http://symfony.com/doc/current/book/doctrine.html#persisting-objects-to-the-database
EDIT: Don't do you query in the model, but inside the entity repository like this:
编辑:不要在模型中查询,但在实体存储库中查询如下:
public function setName(name, id)
{
$sql = "UPDATE users SET name=:name WHERE id=:id";
$stmt = $this->getEntityManager()
->getConnection()
->prepare($sql);
$stmt->execute(array('name' => $name, 'id' => $id));
}