0
votes

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...

2
If you have a Doctrine model for it, why don't you use it? $item = StaticPageTable::getInstance()->find($id); $item->setPublished(true)->save();Fracsi
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..user1687107

2 Answers

1
votes

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

$item = Doctrine::getTable($model_name)->findOneById($id);
$item->setPublished(true);
$item->save();
1
votes

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.