Good afternoon! How can I save related records, if I need to store more than two related records?
In documentation:
<?php
// Create a robot
$artist = new Artists();
$artist->name = 'Shinichi Osawa';
$artist->country = 'Japan';
// Create an album
$album = new Albums();
$album->name = 'The One';
$album->artist = $artist; //Assign the artist
$album->year = 2008;
//Save both records
$album->save();
I need something like this:
$model = new Model();
$model->FirstRelatedRecord->SecondRelatedRecord = $some_content;
$model->save();
I have 3 Entity:
class Tickets extends Model {
public $id;
public $email;
public $uniquekey;
public $active;
public $new;
public function initialize(){
$this->hasMany("id", "Modules\Modules\Models\TicketsMembers", "ticket_id", array(
'alias' => 'TicketsMembers',
'foreignKey' => array(
'action' => \Phalcon\Mvc\Model\Relation::ACTION_CASCADE
)));
}
}
class TicketsMembers extends Model {
public $id;
public $ticket_id;
public $name;
public function initialize(){
$this->hasMany("id", "Modules\Modules\Models\TicketsMessages", "member_id", array(
'alias' => 'TicketsMessages',
'foreignKey' => array(
'action' => \Phalcon\Mvc\Model\Relation::ACTION_CASCADE
)));
}
}
class TicketsMessages extends Model {
public $id;
public $member_id;
public $message;
public $date;
public $answer;
public function initialize(){
}
}
I'm trying to do as following:
$model = new Tickets();
$model->email = $this->request->getPost('email', 'email');
$model->uniquekey = rand(100,109).date('dmy');
$model->active = 'Y';
$model->new = 'Y';
$member = new TicketsMembers();
$member->ticket_id = $model;
$member->name = $this->request->getPost('name', array('string', 'striptags'));
$model->TicketsMembers = $member;
$content = new TicketsMessages();
$content->member_id = $model->TicketsMembers;
$content->message = $this->request->getPost('message', array('string', 'striptags'));
$content->date = time();
$content->answer = 'N';
$model->TicketsMembers->TicketsMessages = $content;
if (!$model->save()) {
//My code
}
As a result, records are stored in table "tickets" and table "tickets_members". However, table "tickets_messages" is empty.
So,
$model->TicketsMembers = $member;
works perfect, but
$model->TicketsMembers->TicketsMessages = $content;
does not work.
What should I do in this situation? Is it possible in such a way save a record? Thanks for your help!