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

前端 未结 5 763
清酒与你
清酒与你 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-10 03:59

    I am doing this in a project now by overriding hydrate() and Peer::addSelectColumns() for accessing postgis fields:

    // in peer
    public static function locationAsEWKTColumnIndex()
    {
        return GeographyPeer::NUM_COLUMNS - GeographyPeer::NUM_LAZY_LOAD_COLUMNS;
    }
    
    public static function polygonAsEWKTColumnIndex()
    {
        return GeographyPeer::NUM_COLUMNS - GeographyPeer::NUM_LAZY_LOAD_COLUMNS + 1;
    }
    
    public static function addSelectColumns(Criteria $criteria)
    {
        parent::addSelectColumns($criteria);
        $criteria->addAsColumn("locationAsEWKT", "AsEWKT(" . GeographyPeer::LOCATION . ")");
        $criteria->addAsColumn("polygonAsEWKT", "AsEWKT(" . GeographyPeer::POLYGON . ")");
    }
    // in object
    public function hydrate($row, $startcol = 0, $rehydrate = false)
    {
        $r = parent::hydrate($row, $startcol, $rehydrate);
        if ($row[GeographyPeer::locationAsEWKTColumnIndex()])   // load GIS info from DB IFF the location field is populated. NOTE: These fields are either both NULL or both NOT NULL, so this IF is OK
        {
            $this->location_ = GeoPoint::PointFromEWKT($row[GeographyPeer::locationAsEWKTColumnIndex()]); // load gis data from extra select columns See GeographyPeer::addSelectColumns().
            $this->polygon_ = GeoMultiPolygon::MultiPolygonFromEWKT($row[GeographyPeer::polygonAsEWKTColumnIndex()]); // load gis data from extra select columns See GeographyPeer::addSelectColumns().
        }   
        return $r;
    }   
    

    There's something goofy with AddAsColumn() but I can't remember at the moment, but this does work. You can read more about the AddAsColumn() issues.

提交回复
热议问题