Rada jak na vazbu serialy-serie-epizody-streamy
- erikbalog
- Člen | 27
Ahoj,
mohl by mi někdo, prosím, poradit – na stránce s filmy a seriály mám u filmu název, popis a další podrobnosti k danému filmu a k němu streamy (embedovaná videa) v doctrine.
<?php
namespace App\Model\Entities;
use Nette;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="movies")
*/
//class Movie extends Nette\Object
class Movie extends \Kdyby\Doctrine\Entities\BaseEntity
{
use \Kdyby\Doctrine\Entities\Attributes\Identifier;
/**
* @ORM\Column(type="integer", unique=true)
*/
protected $id_csfd;
/**
* @ORM\Column(type="string")
*/
protected $title;
/**
* @ORM\Column(type="string")
*/
protected $country;
/**
* @ORM\Column(type="smallint")
*/
protected $year;
/**
* @ORM\Column(type="smallint")
*/
protected $length;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\ManyToMany(targetEntity="Genre", inversedBy="movies", cascade={"persist"})
* @ORM\JoinTable(name="movies_genres")
* @ORM\OrderBy({"name" = "ASC"})
*/
protected $genres;
/**
* @ORM\ManyToMany(targetEntity="Actor", inversedBy="movies", cascade={"persist"})
* @ORM\JoinTable(name="movies_actors")
* @ORM\OrderBy({"name" = "ASC"})
*/
protected $actors;
/**
* @ORM\ManyToMany(targetEntity="Stream", inversedBy="movies", cascade={"persist"})
* @ORM\JoinTable(name="movies_streams")
* @ORM\OrderBy({"title" = "ASC"})
*/
protected $streams;
public function __construct()
{
$this->genres = new \Doctrine\Common\Collections\ArrayCollection();
$this->actors = new \Doctrine\Common\Collections\ArrayCollection();
$this->streams = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addGenre(Genre $newGenre)
{
$add = TRUE;
foreach($this->genres as $genre) {
if ($genre->name == $newGenre->name) {
$add = FALSE;
break;
}
}
if($add) {
$newGenre->addMovie($this);
$this->genres->add($newGenre);
}
}
public function addActor(Actor $newActor)
{
$add = TRUE;
foreach($this->actors as $actor) {
if ($actor->name == $newActor->name) {
$add = FALSE;
break;
}
}
if($add) {
$newActor->addMovie($this);
$this->actors->add($newActor);
}
}
public function addStream(Stream $stream)
{
$this->streams->add($stream);
}
}
?>
A teď řeším, jak udělat to samé se seriály. Je potřeba, aby se vytvořil seriál stejně jako film (jen bez nějakých informací) a k tomu se následně daly přidat série a pod nimi jednotlivé epizody, na které budou vázány streamy.
Nejde mi o hotový kód, ale potřebuji poradit, jak udělat vazby mezi seriál → série → epizody → streamy s Doctrine entitama.
Napadl mě způsob že bych pro kazdu vazbu udělal entitu ale zdá se mi to složité vytvářet 2× entitu stream i pro filmy i pro serialy
Díky.
- ZahorskyJan
- Člen | 59
@erikbalog ManyToMany a případný self-reference by to mohl vyřešit.
A takhle z hlavy na telefonu si myslím, že by se dalo zjednodušit/zrychlit to přidávání (protože proč nevyužít, že se to přidává do ArrayCollection):
<?php
public function addActor(Actor $newActor)
{
if( !$this->actors->contains($newActor) ) {
$newActor->addMovie($this);
$this->actors->add($newActor);
}
}
?>