Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/6b29b3b5b7a7ae53d7dd to your computer and use it in GitHub Desktop.
Save doctrinebot/6b29b3b5b7a7ae53d7dd to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-865 - https://github.com/doctrine/doctrine2/issues/5387
<?php
class PictureRepository extends Doctrine\ORM\EntityRepository
{
//--------------- version 1 (NOT WORKING) ----------------
/**
* @param int $album
* @param array $pictures
* @return array
*/
public function findMulti($album, array $pictures) {
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->add('select', 'e')
->add('from', $this->getEntityName() .' e')
->add('where', $qb->expr()->andx(
$qb->expr()->eq('e.album', '?1'),
$qb->expr()->in('e.id', '?2')
));
$query = $qb->getQuery();
$query->setParameter(1, $album);
$query->setParameter(2, $pictures);//$pictures is an array, it's not working at all
return $query->getResult();
}
//--------------- version 2 (NOT WORKING) ----------------
/**
* @param int $album
* @param array $pictures
* @return array
*/
public function findMulti($album, array $pictures) {
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->add('select', 'e')
->add('from', $this->getEntityName() .' e')
->add('where', $qb->expr()->andx(
$qb->expr()->eq('e.album', '?1'),
$qb->expr()->in('e.id', '?2')
));
$query = $qb->getQuery();
$query->setParameter(1, $album);
$query->setParameter(2, implode(', ', $pictures));//Again not working, there is only one entity in the result, not all of them
return $query->getResult();
}
//--------------- version 3 (NOT WORKING) ----------------
/**
* @param int $album
* @param array $pictures
* @return array
*/
public function findMulti($album, array $pictures) {
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->add('select', 'e')
->add('from', $this->getEntityName() .' e')
->add('where', $qb->expr()->andx(
$qb->expr()->eq('e.album', '?1'),
//this time triing pass it directly (which is NOT ideal, it should be parameter),
//but again not working, becouse $pictures is an array of strings
//(these strings are numeric, but the type is string)
$qb->expr()->in('e.id', $pictures)
));
$query = $qb->getQuery();
$query->setParameter(1, $album);
return $query->getResult();
}
//--------------- version 4 (WORKING) ----------------
/**
* @param int $album
* @param array $pictures
* @return array
*/
public function findMulti($album, array $pictures) {
//With this foreach it finally works, but I really don't know
//what should I do if they were really strings.
//And also, $pictures should not be passed directly like this.
//It should be a parameter.
foreach ($pictures as $key => $value) {
$pictures[$key] = (int) $value;
}
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->add('select', 'e')
->add('from', $this->getEntityName() .' e')
->add('where', $qb->expr()->andx(
$qb->expr()->eq('e.album', '?1'),
$qb->expr()->in('e.id', $pictures)
));
$query = $qb->getQuery();
$query->setParameter(1, $album);
return $query->getResult();
}
}
<?php
/**
* @Entity
* @Table(name = "album")
*/
class AlbumEntity extends IdEntity
{
/**
* @OneToMany(targetEntity = "PictureEntity", mappedBy = "album")
* @var Doctrine\Common\Collections\Collection
*/
private $pictures;
/**
* @return Doctrine\Common\Collections\Collection
*/
public function getPictures() {
return $this->pictures;
}
}
<?php
/**
* @Entity(repositoryClass = "PictureRepository")
* @Table(name = "picture")
* @property AlbumEntity $gallery
*/
class PictureEntity extends IdEntity
{
/**
* @ManyToOne(targetEntity = "AlbumEntity", inversedBy = "pictures")
* @JoinColumn(name = "album")
* @var AlbumEntity
*/
private $album;
/**
* @return AlbumEntity
*/
public function getAlbum() {
return $this->album;
}
/**
* @param AlbumEntity $entity
* @return PictureEntity
*/
public function setAlbum(AlbumEntity $entity) {
$this->album = $entity;
return $this;
}
}
<?php
/**
* @MappedSuperclass
* @property-read int $id
*/
abstract class IdEntity
{
/**
* @Id
* @Column(type = "integer")
* @GeneratedValue
* @var int
*/
private $id;
/**
* @return int
*/
public function getId() {
return $this->id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment