ok here is code parts for better understanding. i am building Form manager that handles all models and submodels
class Events extends Model
{
public function initialize()
{
$this->hasOne('event_id', 'Events\Models\EventDetails', 'event_id', array(
'alias' => 'details'
));
$this->hasMany('event_id', 'Events\Models\EventToArtist', 'event_id', array(
'alias' => 'artistsList'
));
$this->hasMany('event_id', 'Events\Models\EventPhotos', 'event_id', array(
'alias' => 'photos'
));
$this->hasManyToMany(
'event_id', 'Events\Models\EventToArtist', 'event_id',
'artist_id', 'Events\Models\Artists', 'artist_id', array(
'alias' => 'artists'
));
}
public function validation()
{
$this->validate(new UniquenessValidator(array(
'field' => 'url',
'message' => DI::getDefault()->get('t')->_('event already exists'),
)));
if ($this->validationHasFailed() == true) {
return false;
}
}
public function setParentEventId($parent_event_id)
{
if (!$parent_event_id) {
$this->parent_event_id = null;
} else {
$this->parent_event_id = $parent_event_id;
}
}
public function beforeValidationOnCreate()
{
$this->created_time = date('Y-m-d H:i:s');
$this->user_id = DI::getDefault()->get('session')->get('user')->user_id;
$this->views = 0;
}
}
class EventPhotos extends Model
{
public function initialize()
{
$this->belongsTo('event_id', 'Events\Models\Events', 'event_id', array(
'alias' => 'event'
));
}
}
and if you try to do something like that:
$event = Events::findFirstByEventId($id);
$photos = count($event->photos); //if this line is removed everithing works fine
$list = array();
$photo = new EventPhotos();
$photo->file = '123.jpg';
$list[] = $photo;
$photo = new EventPhotos();
$photo->file = '1234.jpg';
$list[] = $photo;
$event->photos = $list;
$event->save();