i am try to buid a ManyToMany relationship i have 3 Models movie,genres & moviegenres i want to get all genres for a movie for example $movie = Movie::find(); $movie->genres;
i can put the genres in the same table but movies have more then 1 genre thus i would like the be able the filter if need be in the in the future i am using phalcon php as the framework
the genres Model
<?php namespace AppModels; class Genres extends PhalconMvcModel { /** * * @var integer */ public $id; /** * * @var string */ public $name; /** * Initialize method for model. */ public function initialize() { $this->setSchema("phalcon"); $this->setSource("genres"); $this->hasManyToMany( "id", AppModelsMovieGenres::class, "gen_id", "movie_id", AppModelsMovie::class, "id", [ 'alias' => 'Movies', 'reusable' => true, ]); } public static function find($parameters = null) : PhalconMvcModelResultsetInterface { $parameters = self::checkCacheParameters($parameters); return parent::find($parameters); } public static function findFirst($parameters = null) { // $parameters = self::checkCacheParameters($parameters); return parent::findFirst($parameters); } protected static function checkCacheParameters($parameters = null) { if (null !== $parameters) { if (true !== is_array($parameters)) { $parameters = [$parameters]; } if (true !== isset($parameters['cache'])) { $parameters['cache'] = [ 'key' => self::generateCacheKey($parameters), 'lifetime' => 300, ]; } } return $parameters; } protected static function generateCacheKey(array $parameters) { $uniqueKey = []; foreach ($parameters as $key => $value) { if (true === is_scalar($value)) { $uniqueKey[] = $key . ':' . $value; } elseif (true === is_array($value)) { $uniqueKey[] = sprintf( '%s:[%s]', $key, self::generateCacheKey($value) ); } } return join(',', $uniqueKey); } public function reset() { // TODO: Implement reset() method. } }
the movie model
<?php namespace AppModels; class Movie extends PhalconMvcModel { /** * * @var string */ protected $slug; /** * * @var string */ protected $title; /** * * @var integer */ protected $id; /** * * @var integer */ protected $imdb; /** * * @var integer */ protected $tmdb; /** * * @var integer */ protected $year; /** * * @var string */ protected $plot; /** * * @var string */ protected $released; /** * * @var string */ protected $genres; /** * * @var string */ protected $directors; /** * * @var string */ protected $poster; /** * * @var integer */ protected $runtime; /** * * @var string */ protected $actors; /** * * @var double */ protected $rating; /** * * @var string */ protected $link; /** * * @var string */ protected $backdrop; /** * Method to set the value of field slug * * @param string $slug * @return $this */ public function setSlug($slug) { $this->slug = $slug; return $this; } /** * Method to set the value of field title * * @param string $title * @return $this */ public function setTitle($title) { $this->title = $title; return $this; } /** * Method to set the value of field id * * @param integer $id * @return $this */ public function setId($id) { $this->id = $id; return $this; } /** * Method to set the value of field imdb * * @param integer $imdb * @return $this */ public function setImdb($imdb) { $this->imdb = $imdb; return $this; } /** * Method to set the value of field tmdb * * @param integer $tmdb * @return $this */ public function setTmdb($tmdb) { $this->tmdb = $tmdb; return $this; } /** * Method to set the value of field year * * @param integer $year * @return $this */ public function setYear($year) { $this->year = $year; return $this; } /** * Method to set the value of field plot * * @param string $plot * @return $this */ public function setPlot($plot) { $this->plot = $plot; return $this; } /** * Method to set the value of field released * * @param string $released * @return $this */ public function setReleased($released) { $this->released = $released; return $this; } /** * Method to set the value of field genres * * @param string $genres * @return $this */ public function setGenres($genres) { $this->genres = $genres; return $this; } /** * Method to set the value of field directors * * @param string $directors * @return $this */ public function setDirectors($directors) { $this->directors = $directors; return $this; } /** * Method to set the value of field poster * * @param string $poster * @return $this */ public function setPoster($poster) { $this->poster = $poster; return $this; } /** * Method to set the value of field runtime * * @param integer $runtime * @return $this */ public function setRuntime($runtime) { $this->runtime = $runtime; return $this; } /** * Method to set the value of field actors * * @param string $actors * @return $this */ public function setActors($actors) { $this->actors = $actors; return $this; } /** * Method to set the value of field rating * * @param double $rating * @return $this */ public function setRating($rating) { $this->rating = $rating; return $this; } /** * Method to set the value of field link * * @param string $link * @return $this */ public function setLink($link) { $this->link = $link; return $this; } /** * Method to set the value of field backdrop * * @param string $backdrop * @return $this */ public function setBackdrop($backdrop) { $this->backdrop = $backdrop; return $this; } /** * Returns the value of field slug * * @return string */ public function getSlug() { return $this->slug; } /** * Returns the value of field title * * @return string */ public function getTitle() { return $this->title; } /** * Returns the value of field id * * @return integer */ public function getId() { return $this->id; } /** * Returns the value of field imdb * * @return integer */ public function getImdb() { return $this->imdb; } /** * Returns the value of field tmdb * * @return integer */ public function getTmdb() { return $this->tmdb; } /** * Returns the value of field year * * @return integer */ public function getYear() { return $this->year; } /** * Returns the value of field plot * * @return string */ public function getPlot() { return $this->plot; } /** * Returns the value of field released * * @return string */ public function getReleased() { return $this->released; } /** * Returns the value of field genres * * @return string */ public function getGenres() { return $this->genres; } /** * Returns the value of field directors * * @return string */ public function getDirectors() { return $this->directors; } /** * Returns the value of field poster * * @return string */ public function getPoster() { return $this->poster; } /** * Returns the value of field runtime * * @return integer */ public function getRuntime() { return $this->runtime; } /** * Returns the value of field actors * * @return string */ public function getActors() { return $this->actors; } /** * Returns the value of field rating * * @return double */ public function getRating() { return $this->rating; } /** * Returns the value of field link * * @return string */ public function getLink() { return $this->link; } /** * Returns the value of field backdrop * * @return string */ public function getBackdrop() { return $this->backdrop; } /** * Initialize method for model. */ public function initialize() { $this->setSchema("phalcon"); $this->setSource("movie"); } public function afterFetch() { if ($this->rating > 0) { $this->rating *= 10; } if ($this->runtime > 0) { $mktime = mktime(0, $this->runtime); $hour = 0 + date('H', $mktime); $mins = 0 + date('i', $mktime); $temp = $hour . ' hour'; if ($hour != 1) { $temp .= 's'; } $temp .= ' '; $temp .= $mins . ' min'; if ($mins != 1) { $temp .= 's'; } $this->runtime = $temp; } else { $this->runtime = ''; } $find = strpos($this->plot, '.'); if ($find !== false) { $this->overview = substr($this->plot, 0, $find); $this->overview .= '...'; } else { $this->overview = $this->plot; } $this->hasManyToMany( "genres", AppModelsMovieGenres::class, "gen_id", "movie_id", AppModelsMovie::class, "id", [ 'alias' => 'rio', 'reusable' => true, ]); $this->actors = unserialize($this->actors); } public function getrio($parameters = null) { return $this->getRelated('rio', $parameters); } public static function find($parameters = null) : PhalconMvcModelResultsetInterface { $parameters = self::checkCacheParameters($parameters); return parent::find($parameters); } public static function findFirst($parameters = null) { // $parameters = self::checkCacheParameters($parameters); return parent::findFirst($parameters); } protected static function checkCacheParameters($parameters = null) { if (null !== $parameters) { if (true !== is_array($parameters)) { $parameters = [$parameters]; } if (true !== isset($parameters['cache'])) { $parameters['cache'] = [ 'key' => self::generateCacheKey($parameters), 'lifetime' => 300, ]; } } return $parameters; } protected static function generateCacheKey(array $parameters) { $uniqueKey = []; foreach ($parameters as $key => $value) { if (true === is_scalar($value)) { $uniqueKey[] = $key . ':' . $value; } elseif (true === is_array($value)) { $uniqueKey[] = sprintf( '%s:[%s]', $key, self::generateCacheKey($value) ); } } return join(',', $uniqueKey); } }
and the moviegenres model
<?php namespace AppModels; class MovieGenres extends PhalconMvcModel { /** * * @var integer */ public $id; /** * * @var integer */ public $gen_id; /** * * @var integer */ public $movie_id; /** * Initialize method for model. */ public function initialize() { $this->setSchema("phalcon"); $this->setSource("movie_genres"); $this->belongsTo('gen_id', 'AppModelsGenres', 'id', ['alias' => 'mgen']); $this->belongsTo('movie_id', 'AppModelsMovie', 'id', ['alias' => 'Movie']); } public static function find($parameters = null) : PhalconMvcModelResultsetInterface { $parameters = self::checkCacheParameters($parameters); return parent::find($parameters); } public static function findFirst($parameters = null) { // $parameters = self::checkCacheParameters($parameters); return parent::findFirst($parameters); } protected static function checkCacheParameters($parameters = null) { if (null !== $parameters) { if (true !== is_array($parameters)) { $parameters = [$parameters]; } if (true !== isset($parameters['cache'])) { $parameters['cache'] = [ 'key' => self::generateCacheKey($parameters), 'lifetime' => 300, ]; } } return $parameters; } protected static function generateCacheKey(array $parameters) { $uniqueKey = []; foreach ($parameters as $key => $value) { if (true === is_scalar($value)) { $uniqueKey[] = $key . ':' . $value; } elseif (true === is_array($value)) { $uniqueKey[] = sprintf( '%s:[%s]', $key, self::generateCacheKey($value) ); } } return join(',', $uniqueKey); } public function reset() { // TODO: Implement reset() method. } }
Advertisement
Answer
Your Movies
class is where you want to put your many-to-many relationship, using MovieGenres
as the intermediary.
$this->hasManyToMany( 'id' // Local model column 'AppModelMovieGenres', // intermediary model 'movie_id', // intermediary column that maps to the local model 'gen_id', // intermediary column that maps to the final model 'AppModelGenres', // final model 'id', // final model column that the intermediary model maps to ['alias'=>'genres'] );