Symfony app - how to add calculated fields to Propel objects?

前端 未结 5 782
清酒与你
清酒与你 2021-02-10 03:41

What is the best way of working with calculated fields of Propel objects?

Say I have an object \"Customer\" that has a corresponding table \"customers\" and each column

5条回答
  •  臣服心动
    2021-02-10 04:09

    Here's what I did to solve this without any additional queries:

    Problem

    Needed to add a custom COUNT field to a typical result set used with the Symfony Pager. However, as we know, Propel doesn't support this out the box. So the easy solution is to just do something like this in the template:

    foreach ($pager->getResults() as $project):
    
     echo $project->getName() . ' and ' . $project->getNumMembers()
    
    endforeach;
    

    Where getNumMembers() runs a separate COUNT query for each $project object. Of course, we know this is grossly inefficient because you can do the COUNT on the fly by adding it as a column to the original SELECT query, saving a query for each result displayed.

    I had several different pages displaying this result set, all using different Criteria. So writing my own SQL query string with PDO directly would be way too much hassle as I'd have to get into the Criteria object and mess around trying to form a query string based on whatever was in it!

    So, what I did in the end avoids all that, letting Propel's native code work with the Criteria and create the SQL as usual.

    1 - First create the [get/set]NumMembers() equivalent accessor/mutator methods in the model object that gets returning by the doSelect(). Remember, the accessor doesn't do the COUNT query anymore, it just holds its value.

    2 - Go into the peer class and override the parent doSelect() method and copy all code from it exactly as it is

    3 - Remove this bit because getMixerPreSelectHook is a private method of the base peer (or copy it into your peer if you need it):

    // symfony_behaviors behavior
    foreach (sfMixer::getCallables(self::getMixerPreSelectHook(__FUNCTION__)) as $sf_hook)
    {
      call_user_func($sf_hook, 'BaseTsProjectPeer', $criteria, $con);
    }
    

    4 - Now add your custom COUNT field to the doSelect method in your peer class:

    // copied into ProjectPeer - overrides BaseProjectPeer::doSelectJoinUser()
    public static function doSelectJoinUser(Criteria $criteria, ...)
    {
       // copied from parent method, along with everything else
       ProjectPeer::addSelectColumns($criteria);
       $startcol = (ProjectPeer::NUM_COLUMNS - ProjectPeer::NUM_LAZY_LOAD_COLUMNS);
       UserPeer::addSelectColumns($criteria);
    
       // now add our custom COUNT column after all other columns have been added
       // so as to not screw up Propel's position matching system when hydrating
       // the Project and User objects.
       $criteria->addSelectColumn('COUNT(' . ProjectMemberPeer::ID . ')');
    
       // now add the GROUP BY clause to count members by project
       $criteria->addGroupByColumn(self::ID);
    
       // more parent code
    
       ...
    
       // until we get to this bit inside the hydrating loop:
    
       $obj1 = new $cls();
       $obj1->hydrate($row);
    
       // AND...hydrate our custom COUNT property (the last column)
       $obj1->setNumMembers($row[count($row) - 1]);
    
       // more code copied from parent
    
       ...
    
       return $results;         
    }
    

    That's it. Now you have the additional COUNT field added to your object without doing a separate query to get it as you spit out the results. The only drawback to this solution is that you've had to copy all the parent code because you need to add bits right in the middle of it. But in my situation, this seemed like a small compromise to save all those queries and not write my own SQL query string.

提交回复
热议问题