Set Relations Without Database


my classes extends from CFormModel which uses component for communication with DB. The component uses web API to communiate with DB so i have no direct way to manipulate with DB. My problem is, that I can’t specify relations between models, because of that. For example, I have model Person and Adress. One Person has many adresses. My actual solution is that I define ‘adresses’ prperty for Person which contain array of Adress objects.

But when i want to validate Person, i can’t simply call $person->validate(). There can also be too many object because of that.

Is there any other way, how to do this ? Thanks.

Can you show your current code for Person model would be easier to understand it. :)

This is Person model:


class Person extends CFormModel {

    public $name;

    public $nameDefault = 'Your name here...';

    public $surname;

    public $surnameDefault = 'Your surname here...';

    public $adresses = array(); // Array of adresses. Adress models will be here.


    public function rules() {

        return array(

                     array('name, surname', 'required'),

                     array('name, surname', 'length', 'max'=>32, 'allowEmpty'=>false),

                     array('name, surname', 'match', 'pattern'=>'/^\p{L}+$/ui', 'allowEmpty'=>false),

                     array('name', 'compare', 'compareValue' => $this->nameDefault, 'operator' => '!=' ),

                     array('surname', 'compare', 'compareValue' => $this->surnameDefault, 'operator' => '!=' ),






    public function attributeLabels() {

      return array(

                    'name' => 'Your name: ',

                    'surname' => 'Your surname: '





For clarification:

In real application, this model will extend from another class, not CFormModel.

That model will communicate with DB using API and component. That is why I am not using relations. This is only for me, testing and education :) .

So, in the model you want to validate $adresses .is it correct ??

 array('name,surname,addresses', 'required'),

Also try to not initialize this $address with array and just handle this $addresses internally as array in your code.

 public $adresses ; //Let it define something like this.

Yes, I think I can do it using my own validation rule but…

Next problem is, how to write errors in views for each adress and automatically assign "error" class to fields.

My wiew contain now something like this (only small part of code):

<div class="row">

                <?php echo $form->labelEx($modelAdress,'state'); ?>

                <?php echo $form->textField($modelAdress,'state[0][0]',array('size'=>32,'maxlength'=>32,'class'=>'stateField')); ?>


But $modelAdress is not real model of that input. It is only clear instance. I generate these inputs using JS. I clone this input and I dont know, how many of them user will add. User can add varios number of persons with various number of adresses. The first index [0] define person JS id, second index is Adress ID generated by JS. Then, in controller, I parse POST into objects Persons with various types of adresses.