Friday, December 7, 2012

Model-query not working with virtualFields

Hi,

i have a Modelaction where i want to list all zipcodes within a radius nearby another zipcode

    public function listZipcodesNearBy($zipcode = '', $radius = false, $km = 1.609344) {
        if ($data = $this->findByZipcode($zipcode)) {
            $lat = $data['Zipcode']['cityLat'];
            $lng = $data['Zipcode']['cityLng'];
           
            $this->virtualFields['distance'] = '
                ACOS (
                    SIN('.$lat.' * PI() / 180) *
                    SIN(cityLat * PI() / 180) +
                    COS('.$lat.' * PI() / 180) *
                    COS(cityLat * PI() / 180) *
                    COS(('.$lng.' - cityLng) * PI() / 180)
                ) * 180 / PI() * 60 * 1.1515 * '.$km;
           
            $conditions = array();
            if (isset($radius)) {
                $conditions = array("distance <=", (int)$radius);
            }
            return $this->find('list', compact('conditions'));

        }
        return false;
    }

When i don't use virtualfields and comment out $conditions with distance <= $radius i get a list of zipcodes. But with virtualfields there's no output. no errors too..

Do you see the error? Is there a way to print sql statements build in models without action?

Please help.

Greetings from Germany
M.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: