开发者

Simple doctrine query -> one specific row and column

I do have a table called "tax" with two column "id | tax_value". I can list the table with an foreach very well.

Now my question, how does the code look like when I just want to have one specfic row and column in the php c开发者_JAVA百科ode? I don't want it in the query!!!

for example:

column 2 (tax_value)

row 1.

The query gives me an array, right?


In TaxTable.class.php:

public function getOneById($id)
{
    $q = Doctrine_Query::create()->select("t.tax_value")
                                 ->from("tax t")
                                 ->where("t.id = ?", $id)
                                 ->fetchOne();
    return $q;
}

In action:

public function executeTaxView(sfWebRequest $request)
{
    $this->tax = TaxTable::getInstance()->getOneById($request->getParameter("id"));
    $this->forward404Unless($this->tax);
}

in taxViewSucess.php:

  Tax Value: <b><?php echo $tax->getTaxValue();?></b>


You can use Doctrine::HYDRATE_SINGLE_SCALAR when fetching a single row with a single column in your select statement:

$total = Doctrine_Query::create()->from('Products p')
        ->select('SUM(p.amount) as total')
        ->where('p.category_id = ?', $categoryId)
        ->fetchOne(array(), Doctrine::HYDRATE_SINGLE_SCALAR);
// $total is now a single value, eg. 12

Example uses a imaginary Product table with categories and selects the amount of products for a certain category.

How you use this query in symfony depends on your needs: in a table class, model class, action, ..


If you use Doctrine_Query, then you can use fetchOne to grab one single row. Furthermore it's been a bit since I've used Doctrine 1, but I'm almost certain with Doctrine_Query you can set the field selection list to one column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜