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

How to access related models in beforeUpdate() method?

How to access related models in beforeUpdate() method?

For example: Model Song has many Artists .

I want to check if a song has at least an artist, if true mark empty_info is "N", if false mark empty_info is "Y"

public function beforeUpdate()
{
    parent::beforeUpdate();

    if(count($this->artists) == 0) {
        $this->empty_info = "Y";
    } else {
        $this->empty_info = "N";
    }
}

I tried by cannot access artists in the function above.

Here is my Song class:

<?php

namespace Bcdcnt\Models;

use Phalcon\Mvc\Model\Relation;

class Song extends SongBase {

    /**
     *
     * @var integer
     */
    public $type;

    public function initialize() {
        parent::initialize();

        $this->hasManyToMany(
                "id", "Bcdcnt\Models\SongArtist", "song_id", "artist_id", "Bcdcnt\Models\Artist", "id", array('alias' => 'artists')
        );

        $this->hasMany("id", "Bcdcnt\Models\SongArtist", "song_id", array(
            'alias' => 'songArtists',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
            )
        ));

        $this->hasManyToMany(
                "id", "Bcdcnt\Models\SongComposer", "song_id", "composer_id", "Bcdcnt\Models\Composer", "id", array('alias' => 'composers')
        );

        $this->hasMany("id", "Bcdcnt\Models\SongComposer", "song_id", array(
            'alias' => 'songComposers',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
            )
        ));

        $this->hasManyToMany(
                "id", "Bcdcnt\Models\SongPoet", "song_id", "poet_id", "Bcdcnt\Models\Poet", "id", array('alias' => 'poets')
        );

        $this->hasMany("id", "Bcdcnt\Models\SongPoet", "song_id", array(
            'alias' => 'songPoets',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
            )
        ));

        $this->hasManyToMany(
                "id", "Bcdcnt\Models\SongDocument", "song_id", "document_id", "Bcdcnt\Models\Document", "id", array('alias' => 'documents')
        );

        $this->hasMany("id", "Bcdcnt\Models\SongDocument", "song_id", array(
            'alias' => 'songDocuments',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
            )
        ));

        $this->hasManyToMany(
                "id", "Bcdcnt\Models\SongPlaylist", "song_id", "playlist_id", "Bcdcnt\Models\Playlist", "id", array('alias' => 'playlists')
        );

        $this->hasMany("id", "Bcdcnt\Models\SongPlaylist", "song_id", array(
            'alias' => 'songPlaylists',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
            )
        ));

        $this->hasManyToMany(
                "id", "Bcdcnt\Models\SongScat", "song_id", "scat_id", "Bcdcnt\Models\Scat", "id", array('alias' => 'scats')
        );

        $this->hasMany("id", "Bcdcnt\Models\SongScat", "song_id", array(
            'alias' => 'songScats',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
            )
        ));

        $this->hasMany("id", "Bcdcnt\Models\Comment", "song_id", array(
            'alias' => 'comments',
            'foreignKey' => array(
                'action' => Relation::ACTION_CASCADE
            )
        ));
    }

    public function beforeUpdate() {
        parent::beforeUpdate();

        if ($this->getRelated('Artists')->count() == 0 || $this->getRelated('Composers')->count() == 0) {
            $this->empty_info = "Y";
        } else {
            $this->empty_info = "N";
        }
    }

    public function get_link() {
        return "/bai-hat/" . $this->slug . "-" . $this->id . ".html";
    }

    public function get_artist() {
        if (count($this->songArtists)) {
            $args = array();
            $songArtists = $this->getSongArtists(array("order" => "[order]"));
            foreach ($songArtists as $songArtist) {
                $args[] = $songArtist->artist->title;
            }

            return implode(" - ", $args);
        } else {
            return "Đang cập nhật";
        }
    }

}


51.1k

How you defined the relations ?

Artist class:

<?php namespace NS\Model;

class Artist extends AbstractModel {

    public $id;

    public function initialize() {
        $this->belongsTo('song_id', '\NS\Model\Song', 'id');
    }

}

Song class:

<?php namespace NS\Model;

class Song extends AbstractModel {

    public $id;

    public function initialize() {
        $this->hasMany('id', '\NS\Model\Artist', 'song_id', [
            'alias' => 'Artists'
        ]);
    }

}

Get them:

$song = new Song;
$artists = $song->artists;
// or
$artists = $song->getRelated('Artists');


51.1k

Try with $this->getRelated('Artists')->count()

public function beforeUpdate()
{
    parent::beforeUpdate();

    if($this->getRelated('Artists')->count() == 0) {
        $this->empty_info = "Y";
    } else {
        $this->empty_info = "N";
    }
}

I have updated my full class of Song.

I can access related models outside the class, but inside the class can not.

For example, it is ok:

$song = new Song;
$artists = $song->artists;
// or
$artists = $song->getRelated('Artists');

But in beforeUpdate() function, it can not:

public function beforeUpdate()
{
parent::beforeUpdate();

if($this->getRelated('Artists')->count() == 0) {
    $this->empty_info = "Y";
} else {
    $this->empty_info = "N";
}
}