We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Storing related records

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!



26.3k
Accepted
answer

In line


$member->ticket_id = $model;

You assing an object to a variable that is expected to be an integer.

You do the same in this line:


$content->member_id = $model->TicketsMembers;

You assign here a RESULTSET to a field that is expected to be integer.

I think it is not allowed to to this, but I might be wrong.

What I would do is:

1 In TicketsMembers I would define missing belongsTo relation to Tickets.

2 In TicketsMessages I would define missing belongsTo relation to TicketsMembers.

Then:


$model = new Tickets();

$model->email = '[email protected]';
$model->uniquekey = 3423;
$model->active = 'Y';
$model->new = 'Y';

$member = new TicketsMembers();
$member->name = 'Awesome Name';

$content = new TicketsMessages();
$content->message = 'Awesome message';
$content->date = time();
$content->answer = 'N';

//assigns parent to children
$model->TicketsMembers = $member;
$member->TicketsMessages = $content;

if (!$model->save()) {
//My code
}

I have chaned $this->request->getPost to strings because there might be a risk that getPost (for some reason) returns null. What I mean is, that you need to find point of failure - there are more than one now. First try to check if relations work properly, then add getPost methods.

edited Aug '14

Problem solved.

  1. About belongsTo: these relation are defined (I just forgot to copy these lines =/ );

  2. In line:
$member->ticket_id = $model;

I'm really assing an object to a variable that is expected to be an integer, but if works perfectly. Perhaps there is a better way to get Id of parrent-table;

  1. So, the fault was in the wrong syntax in this line:
$model->TicketsMembers->TicketsMessages = $content;

This line isn't working.

Your variant

//assigns parent to children
$model->TicketsMembers = $member;
$member->TicketsMessages = $content;

works great!

Thanks for your help!

PS: How do you paste code on a black background? I haven't a tag [code] or something in the editor.