简体   繁体   中英

Symfony doctrine repository return instance of entity

I have an entity which store the 3D objects what I printed.

private $id;
/**
 * @ORM\Column(type="array", nullable=true)
 */
private $images;
/**
 * @ORM\Column(type="datetime")
 */
private $date_created;
/**
 * @ORM\Column(type="datetime")
 */
private $date_modified;
/**
 * @ORM\ManyToOne(targetEntity="App\UserBundle\Entity\User")
 */
private $user;
/**
 * @ORM\ManyToOne(targetEntity="App\ThreedBundle\Entity\Threedobject", cascade={"all"})
 */
private $threedobject;
/**
 * @ORM\Column(type="text", nullable=true)
 */
private $description;

There is a SQL query which looks like this:

select threedobject_id from threed_print where user_id = {RANDOM_NUMBER} group by threedobject_id;

I have to get the $threedobject all instance which (I mean the App\\ThreedBundle\\Entity\\Threedobject instances) which represent the following sql query through Doctrine. I tried the following querybuilder, but it have returned the array representation of the values, but most of the cases I have to use the methods of the elements, so I want to get the instances.

$query = $this->em->createQueryBuilder();
$result = $query
  ->select('tp')
  ->addSelect('to')
  ->from('ThreedBundle:ThreedPrint', 'tp')
  ->join('tp.threedobject', 'to')
  ->join('tp.user', 'u')
  ->where('u.id = :userID')
  ->groupby('to.id')
  ->setParameter('userID', $userID)
  ->getQuery();
return $result->getResult();

I read about the repository find method, but in this is not what I want, or I'm not totally understand how it is working.

UPDATE:

Basically what I need:

$query = $this->em->createQueryBuilder();
$result = $query
  ->select('to')
  ->from('ThreedBundle:ThreedPrint', 'tp')
  ->join('tp.threedobject', 'to')
  ->join('tp.user', 'u')
  ->where('u.id = :userID')
  ->groupby('to.id')
  ->setParameter('userID', $userID)
  ->getQuery();
return $result->getResult();

But I got the following error for that:

'SELECT to FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.

If you want to use the repository, you should create a folder in your project under src (which normally you call it Repository) and create a new class with a name (for example: ThreedobjectRepository).

then you put the following in that class:

namespace NameProject\NameBundle\Repository;

use Doctrine\ORM\EntityRepository;


class ThreedobjectRepository extends EntityRepository
{

function findById3D($a)
{
$query = $this->getEntityManager()
    ->createQuery("Select th.threedobject_id AS id
                               FROM NameprojectNameBundle:threed_print th
                               Where th.user_id=:user_id
                               GROUP BY threedobject_id")
                 set parameter (user_id, $a);

return $query->getResult();


}

}

don't forget to put on your threed_print.php this line:

/**
 * @ORM\Entity(repositoryClass="Nameproject\NameBundle\Repository\ThreedobjectRepository")
* @ORM\Table
*/

then you can go to the controller you are working on and instead of using findall() or findoneby(), you just use the function that you already create findById3D($a) and use easily the instance you wanted to use.

I hope I helped you.

You should implement the inverse relationship oneToMany from Threedobject to Threedprint, adding the $threedprints field in Threedobject.

Then you could write this

$threedobjects=$this->em->createQueryBuilder()
->select('to')
->from('ThreedBundle:Threedobject')
->join('to.threedprints', 'tp')
->join('tp.user', 'u')
->where('u.id = :userID')
->setParameter('userID', $userID)
->getQuery()->getResult();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM