Add condition if field exists

I am trying to do translations. And what I want to do is to have e.g. models like this:

Country
CountryTranslation
Product
ProductTranslation

In Translation models I got field "locale". Is there a way that if model contains field "locale", then add extra condition "AND locale='en'", so it would work dynamically, instead of for every "find" writing condition with "locale"?

I know that there in Doctrine 2 there is something what can be used to do this and it is called filter.



85.3k

You can try something like this:

<?php

class MyModel extends Phalcon\Mvc\Model
{
    public static function find($parameters=null)
    {
        if (isset($parameters[0]) {
            $parameters[0] = $parameters[0] . ' AND locale = "en"';
        } else {
            if (isset($parameters["conditions"]) {
                $parameters["conditions"] = $parameters["conditions"] . ' AND locale = "en"';
            }
        }
        return parent::find($parameters);
    }
}