0

Is it any possibility to create query with doctrine (symfony 1.4) in which I defined table name from database (not model class from schema.yml) in from clause?

For example:

in schema.yml I have model class

StaticPage:
connection: doctrine
tableName: static_page
columns:
...

my query is as following:

$item = Doctrine_Query::create();
$item->query("SELECT * FROM StaticPage WHERE id = ".$id);
$change = $item->fetchOne();
$change->setPublished(true);
$change->save();

In this query instead StatigPage I need static_page (tableName)...

Thanks...

  • 1
    If you have a Doctrine model for it, why don't you use it? `$item = StaticPageTable::getInstance()->find($id);` `$item->setPublished(true)->save();` – Fracsi Jun 20 '13 at 07:21
  • I defined this query in sfDoctrineModule generator and it needs to be generalized, because I call it many times in my app for different models. For some reasone when I put modelClass in from clause, for some models this query returns data from more then one table, and that is the problem. I figured out, if I put tableName in from clause, then my query works fine.. –  Jun 20 '13 at 07:27

2 Answers2

1

Other way of Fracsi's solution:

$item = Doctrine::getTable("StaticPage")->findOneById($id);
    if($item instanceof StaticPage) {
        $item->setPublished(true);
        $item->save();
    }

Always use your model/ORM. There are few cases in which Doctrine can't build the correct query syntax and you have to write sql on your own.

Sven Schneider
  • 295
  • 1
  • 9
  • Sorry, but this is not solution for my problem. Thanks for your effort –  Jun 20 '13 at 07:44
  • No problem, but I really don't get your problem then. The way Fracsi and me explained is the way one should work with Doctrine. – Sven Schneider Jun 20 '13 at 12:30
1

Generalized version: (pass the model name through parameter (e.g. $sf_request)

$item = Doctrine::getTable($model_name)->findOneById($id);
$item->setPublished(true);
$item->save();
Fracsi
  • 2,274
  • 15
  • 24