控制器,repo和twig打印阵列(Symfony3)

时间:2017-09-11 18:44:07

标签: symfony twig symfony-3.3

我需要打印单个用户的域名

ENTITY(这是实体代码)

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Domini
 *
 * @ORM\Table(name="domini")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DominiRepository")
 */
class Domini
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     *
     * @ORM\Column(name="id_user", type="integer")
     */
    private $idUser;

    /**
     * @var string
     *
     * @ORM\Column(name="dominio", type="string", length=100, unique=true)
     */
    private $dominio;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set idUser
     *
     * @param integer $idUser
     *
     * @return Domini
     */
    public function setIdUser($idUser)
    {
        $this->idUser = $idUser;

        return $this;
    }

    /**
     * Get idUser
     *
     * @return int
     */
    public function getIdUser()
    {
        return $this->idUser;
    }

    /**
     * Set dominio
     *
     * @param string $dominio
     *
     * @return Domini
     */
    public function setDominio($dominio)
    {
        $this->dominio = $dominio;

        return $this;
    }

    /**
     * Get dominio
     *
     * @return string
     */
    public function getDominio()
    {
        return $this->dominio;
    }
}

REPOSITORY(这是Repo代码)

<?php

namespace AppBundle\Repository;
use AppBundle\Entity\Domini;
use Doctrine\ORM\EntityRepository;

class DominiRepository extends \Doctrine\ORM\EntityRepository
{
    /**
     * @return dominio[]
     */
    public function findAllPublishedOrderedBySize()
    {

        $query = 'SELECT dominio FROM AppBundle:Domini dominio';
        $mydomain = $this->getEntityManager()->createQuery($query);
        return $mydomain->execute();

    }
}

CONTROLLER

<?php
// src/AppBundle/Controller/DominiController.php
namespace AppBundle\Controller;

use AppBundle\Entity\Domini;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\EntityManagerInterface;

class DominiController extends Controller
{
/**
     * @Route("/admin/domini")
     */
    public function dominiAction()
    {
        $title = "ADMIN";
        $nome_utente = $this->getUser();

            $user = $this->getUser()->getId();
        $em = $this->getDoctrine()->getManager();

        $dominilist = $em->getRepository('AppBundle:Domini')
        ->findAllPublishedOrderedBySize();

        return $this->render('admin/domini.html.twig', [
            'title' => $title,
            'nome_utente' => $nome_utente,
            'user' => $user,
            'dominilist' => $dominilist,
        ]);

    }

}

TWIG

<h1>{{ title }}</h1>
<h2>Benvenuto: {{ nome_utente }}</h2>
<h3>USER ID:{{ user }}</h3>


<ul>
    {% for item in dominilist %}
        <li>{{ item.dominio }}</li>
    {% endfor %}
</ul>

MYSQL表

表名:domini

字段:id,id_user,dominio

现在一切正常,但是“支配者”#39; =&GT; $ dominilist不起作用。 Dominio是在&#34; dominio&#34;下保存的域名。为什么呢?

如果我试图破坏这个领域,它就无法运作。

1 个答案:

答案 0 :(得分:1)

如果要选择存储在该表中的所有数据,可以直接在控制器中使用快速方便的方法

# Controller
$dominilist = $em->getRepository('AppBundle:Domini')
    ->findAll();

或者您也可以在控制器中找到domini id

# Controller
$user = $this->getUser()->getId();
$dominilist = $em->getRepository('AppBundle:Domini')->findBy(['id'=>$user]);

只有当您想要更复杂的查询时,才能在存储库中创建q方法。因为“我需要打印单个用户的域名”

# Repository
public function findAllPublishedOrderedBySize($id)
{
    $qb = $this->createQueryBuilder('d');
    $qb
        ->select('d.dominio')
        ->where('d.id = :id')
        ->setParameter('id', $id)
    ;

    return $qb->getQuery()->getResult();
}

# Controller
$user = $this->getUser()->getId();
$domini = $em->getRepository('AppBundle:Domini')
    ->findAllPublishedOrderedBySize($user);

无论如何,这些类型的查询很小,您可以在控制器中创建它们,正如我在上面展示的那样。