views:

25

answers:

0

Using KO 3.07... and ORM

I have a survey model, and a choices model. The survey itself holds the question, while the choices are all the given options that people have for that survey question... (As of now, 2 choices per survey.)

In my survey model, I need to validate that the actual question is within a given range of characters, and the category exists.

I also need to validate that each choice is within a given range of characters, in order for the survey (and choices) to be saved in the DB.

Now for the question. I want to keep all logic for the choices in the choice model, and all logic for the survey in the survery model... So I want the choice model to validate that the loaded choice is good enough, and I want the survey model to validate that the loaded question is good enough and the category exists.

Now, If I were to do this, how would I go about it.

I was thinking something like this...

Survey Model

class Model_Survey extends ORM
{
    protected $_has_one = array(
        'category' => array(),
    );

    protected $_belongs_to = array(
        'user' => array(),
    );

    protected $_has_many = array(
        'opinions'    => array(),
        'choices'     => array(),
        'survey_tags' => array(),
        'favorites'   => array(),
    );

    protected $_rules = array(
        'question' => array(
            'not_empty'  => NULL,
            'min_length' => array(15),
            'max_length' => array(100),
        ),
    );

    protected $_filters = array(
        TRUE => array(
            'trim' => NULL,
        ),
    );

    protected $_callbacks = array(
        'category_id' => array(
            'Model_Category' => 'category_exists',
        ),
    );

    public function create($array)
    {
        $this->values($array);

        $choices = array();

        // Create a choice object for each choice supplied
        foreach ($array['choices'] as $choice)
        {
            $choices[] = ORM::factory('choice')->values(array('text' => $choice));
        }

        $result = $this->check();

        if ($result)
        {
            foreach ($choices as $choice)
            {
                if ( ! $this->check())
                {
                    $result = FALSE;
                }
            }
        }
        // Get ALL errors
        $this->errors = array_merge($this->_validate->errors(), $choices[0]->validate()->errors());

        return $result;
    }
}

Choice Model

class Model_Choice extends ORM
{
    protected $_belongs_to = array(
        'survey' => array(),
    );

    protected $_rules = array(
        'text' => array(
            'not_empty'  => NULL,
            'min_length' => array(15),
            'max_length' => array(50),
        ),
    );

    protected $_filters = array(
        TRUE => array(
            'trim' => NULL,
        ),
    );
}

I havent tested it, but I have a couple questions about it before I try.

  1. Should I use the choices object to validate the choice, or just put all logic inside the survey object. (I'm guesing the former).
  2. How Would I get all the errors that occured, if any occur. I guess merging them would work just fine, but IDK.

Maybe...

Anyways, how should I start this process...

Thanks