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";
}
}
}