12. Persisting object
// src/Volcano/VideostatusBundle/Controller/ClipController.php
use SymfonyComponentHttpFoundationResponse;
use VolcanoVideostatusBundleEntityClip;
public function createAction()
{
$clip = new Clip();
$clip->setUrl('http://www.youtube.com/watch?v=PYtXuBN1Hvc');
$clip->setTimeStart(52);
$clip->setTimeFinish(54);
$em = $this->getDoctrine()->getManager();
$em->persist($clip);
$em->flush();
return new Response('Created clip id '.$clip->getId());
}
17. DQL
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT c FROM VolcanoVideostatusBundle:Clip c WHERE c.
timeFinish - c.timeStart > :delta ORDER BY p.id ASC'
)->setParameter('delta', 10);
$clips = $query->getResult();
22. Many To Many Association
/**
* @ORMEntity
*/
class Clip
{
/**
* @ORMManyToMany(targetEntity="Tag", inversedBy="clips")
* @ORMJoinTable(name="clips_tags")
**/
private $tags;
public function __construct() {
$this->groups = new ArrayCollection();
}
}
23. Many To Many Association
/**
* @ORMEntity
*/
class Tag
{
/**
* @ORMManyToMany(targetEntity="Clip", mappedBy="tags")
**/
private $clips;
public function __construct() {
$this->clips = new ArrayCollection();
}
}
24. Many To Many Association
CREATE TABLE Clip (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE clips_tags (
clip_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY(clip_id, tag_id)
) ENGINE = InnoDB;
CREATE TABLE Tag (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE clips_tags ADD FOREIGN KEY (clip_id) REFERENCES Clip(id);
ALTER TABLE clips_tags ADD FOREIGN KEY (tag_id) REFERENCES Tag(id);
25. Persisting object
public function createAction()
{
$clip = new Clip();
$clip->setUrl('http://www.youtube.com/watch?v=PYtXuBN1Hvc');
$tag1 = new Tag();
$tag1->setName('Fun');
$tag2 = new Tag();
$tag2->setName('Котята');
$clip->addTag($tag1);
$clip->addTag($tag2);
$em = $this->getDoctrine()->getManager();
$em->persist($tag1);
$em->persist($tag2);
$em->persist($clip);
$em->flush();
return new Response('Created clip id '.$clip->getId());
}
26. Fetching Related Objects
public function showClipTagsAction($id)
{
$clip = $this->getDoctrine()
->getRepository('VolcanoVideostatusBundle:Clip')->find($id);
$tags = $clip->getTags();
return array('tags' => $tags);
}
<div class="tags">
<div class="tags-list">
{% for tag in tags %}
<a class="label" href="#">{{tag.name}}</a>
{% endfor %}
</div>
</div>
27. Fetching Related Objects
<?php
// src/Volcano/VideostatusBundle/Entity/ClipRepository.php
public function findOneByIdJoinedToTags($id)
{
$query = $this->getEntityManager()
->createQuery('
SELECT c, t FROM VideostatusBundle:Clip c
JOIN c.tags t
WHERE c.id = :id'
)->setParameter('id', $id);
try {
return $query->getSingleResult();
} catch (DoctrineORMNoResultException $e) {
return null;
}
}
28. Lifecycle Callbacks
/**
* @ORMEntity()
* @ORMHasLifecycleCallbacks()
*/
class Clip
{
//....
/**
* @ORMPrePersist
*/
public function setCreatedValue()
{
$this->created = new DateTime();
}
}