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

Update result set after selecting them fails

when i try to reset and update ad some new records it fails, i do someting like that:


$robot = Robots::findFirst("user_id=100");
count($robot->parts);

$new_parts = array();
$part = new Parts();
$part->name = 'part1';
$new_parts[] = $part;
$part = new Parts();
$part->name = 'part2';
$new_parts[] = $part;

$robot->parts = $new_parts;
$robot->save();

it fails to save and if i remove


count($robot->parts);

it works fine and adds 2 new parts

But why you add this to resultset ? What returns getMessages() after save when fail ? You should findFirst if you have only one $robot, if you have multiple then loop through them(you can select only ids of robots and create parts with robot id).

edited Nov '15

Sorry, single entity was passed to metod, so findFirst was in my case.

The main point is that if i select records and then try to reset it or add new elements it's not saved. getMessages returns nothing, as it was no errors it seams that parts are simply ignored when robot is saved. (updated sorce code now to represent correct case)

Show us your relation then.

edited Nov '15

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();
edited Nov '15
$this->belongsTo('event_id', 'Events\Models\Events', 'event_id', array(
            'alias' => 'event'
        ));

Thrid parameter should be field in related model - 'id'.

$this->hasMany('event_id', 'Events\Models\EventPhotos', 'event_id', array(
            'alias' => 'photos'
        ));

Here the same, but first parameter is field in current model for which is relation builded in EventPhotos so i guess id too. Fix your relations and it should work fine.

Read documentation there is everything about relations:

https://docs.phalcon.io/pl/latest/reference/models.html#relationships-between-models

edited Nov '15

Lol it's just a name "event_id" or "id" should be same (in my case in bot tables this field is called same), main thing it must be index or primary key. so i dont think it would have any impact, althou i noticed if you do something like that:


$event = Events::findFirstByEventId($id);
$event->photos->delete();
$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();

it fails to save to

edited Nov '15

NO, it shouldnt be SAME, but if they are same in tables then okay. well maybe try to set $photo->event_id = $id, if that will save, then its for 100% bad relations.