多对多关系获取数据

ManyToMany relationship get the data

我正在尝试建立 ManyToMany 关系,我有 3 个模型电影、流派和电影流派 我想获取电影的所有流派,例如 $movie = Movie::find(); $movie->genres; 我可以将这些类型放在相同的 table 中,但是电影有超过 1 种类型,因此如果将来需要的话,我希望能够使用过滤器,我正在使用 phalcon php 作为框架

类型模型

<?php
namespace App\Models;
class Genres extends \Phalcon\Mvc\Model
{

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

    /**
     *
     * @var string
     */
    public $name;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("phalcon");
        $this->setSource("genres");
        $this->hasManyToMany(
            "id",
            \App\Models\MovieGenres::class,
            "gen_id",
            "movie_id",
            \App\Models\Movie::class,
            "id",
            [   'alias' => 'Movies',
                'reusable' => true,
            ]);
    }

    public static function find($parameters = null) : \Phalcon\Mvc\Model\ResultsetInterface
    {
        $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.
    }
}

电影模特

<?php
namespace App\Models;
class Movie extends \Phalcon\Mvc\Model
{

    /**
     *
     * @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",
            \App\Models\MovieGenres::class,
            "gen_id",
            "movie_id",
            \App\Models\Movie::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) : \Phalcon\Mvc\Model\ResultsetInterface
    {
        $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);
    }





}

和电影类型模型

<?php

namespace App\Models;

class MovieGenres extends \Phalcon\Mvc\Model
{

    /**
     *
     * @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', 'App\Models\Genres', 'id', ['alias' => 'mgen']);
        $this->belongsTo('movie_id', 'App\Models\Movie', 'id', ['alias' => 'Movie']);
    }

    public static function find($parameters = null) : \Phalcon\Mvc\Model\ResultsetInterface
    {
        $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.
    }
}

你的 Movies class 是你想要放置 many-to-many 关系的地方,使用 MovieGenres 作为中介。

$this->hasManyToMany(
    'id'                     // Local model column
    'App\Model\MovieGenres', // intermediary model
    'movie_id',              // intermediary column that maps to the local model
    'gen_id',                // intermediary column that maps to the final model
    'App\Model\Genres',      // final model
    'id',                    // final model column that the intermediary model maps to
    ['alias'=>'genres']
);