mozaniak
(Ozaniak Michal)
July 12, 2013, 6:05am
1
Hello,
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.
jayant
(Codesutras)
July 12, 2013, 7:01am
2
Hello,
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 class.it would be easier to understand it.
mozaniak
(Ozaniak Michal)
July 12, 2013, 8:31am
3
This is Person model:
<?php
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 .
jayant
(Codesutras)
July 12, 2013, 8:38am
4
This is Person model:
<?php
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: '
);
}
}
?>
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.
mozaniak
(Ozaniak Michal)
July 12, 2013, 9:34am
5
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')); ?>
</div>
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.