如何从另一个控制器Symfony2中检索数据

时间:2014-01-13 10:39:12

标签: symfony doctrine-orm

我是Symfony2的新手,我有一个问题,我自己无法完全解决。我正在编写应用程序并使用表客户端和项目。每个项目都有一个客户,每个客户都可以有多个项目

客户端和projectstables的自主显示确实已经有效,但我还必须在客户的前端显示项目。我已经尝试了多种方法,但到目前为止我没有取得任何成功。

您能否告诉我我必须在ClientController-,config.yml和routing.yml-File中添加什么才能在客户的 show.html.twig文件中显示项目表?

我提前感谢您的回答

网址:http://dev.pingag.ch/Symfony/web/app_dev.php/clients/

ClientController.php

    namespace Acme\KeywordBundle\Controller;

    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
    use Acme\KeywordBundle\Entity\Client;
    use Acme\KeywordBundle\Entity\Project;
    use Acme\KeywordBundle\Form\ClientType;

/**
 * Client controller.
 *
 * @Route("/clients")
 */
class ClientController extends Controller
{


    /**
     * Lists all Client entities.
     *
     * @Route("/", name="clients")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('AcmeKeywordBundle:Client')->findAll();

        return array(
            'entities' => $entities,
        );

    } 


    ...
    ...
    ...
}

config.yml

    imports:
- { resource: parameters.yml }
- { resource: security.yml }
...
...
...

的routing.yml

    acme_keyword:
resource: "@AcmeKeywordBundle/Controller"
type:     annotation

client.php

<?php

namespace Acme\KeywordBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Client
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class client
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=true)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

     // Anfang Kopiert von Anleitung

    /**
     * @ORM\OneToMany(targetEntity="Project", mappedBy="client")
     */
    protected $projects;

    /**
     * @ORM\OneToMany(targetEntity="File", mappedBy="client")
     */
    protected $files;

      /**
     * @ORM\OneToMany(targetEntity="Contact", mappedBy="client")
     */
    protected $contacts;

    public function __construct()
    {
        $this->projects = new ArrayCollection();
        $this->files = new ArrayCollection();
        $this->contacts = new ArrayCollection();
    }


    // Ende Kopiert von Anleitung

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     * @Assert\NotBlank()
     */
    private $name;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="createdAt", type="datetime", nullable=true)
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updatedAt", type="datetime", nullable=true)
     */
    private $updatedAt;

    /**
     * @var string
     *
     * @ORM\Column(name="logoFileName", type="string", length=255, nullable=true)
     */
    private $logoFileName;

    /**
     * @var string
     *
     * @ORM\Column(name="logoContentType", type="string", length=255, nullable=true)
     */
    private $logoContentType;

    /**
     * @var integer
     *
     * @ORM\Column(name="logoFileSize", type="integer", nullable=true)
     */
    private $logoFileSize;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="logoUpdatedAt", type="datetime", nullable=true)
     */
    private $logoUpdatedAt;


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

    /**
     * Set name
     *
     * @param string $name
     * @return Client
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set createtAt
     *
     * @param \DateTime $createtAt
     * @return Client
     */
    public function setCreatetAt($createtAt)
    {
        $this->createtAt = $createtAt;

        return $this;
    }

    /**
     * Get createtAt
     *
     * @return \DateTime 
     */
    public function getCreatetAt()
    {
        return $this->createtAt;
    }

    /**
     * Set updatetAt
     *
     * @param \DateTime $updatetAt
     * @return Client
     */
    public function setUpdatetAt($updatetAt)
    {
        $this->updatetAt = $updatetAt;

        return $this;
    }

    /**
     * Get updatetAt
     *
     * @return \DateTime 
     */
    public function getUpdatetAt()
    {
        return $this->updatetAt;
    }

    /**
     * Set logoFileName
     *
     * @param string $logoFileName
     * @return Client
     */
    public function setLogoFileName($logoFileName)
    {
        $this->logoFileName = $logoFileName;

        return $this;
    }

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

    /**
     * Set logoContentType
     *
     * @param string $logoContentType
     * @return Client
     */
    public function setLogoContentType($logoContentType)
    {
        $this->logoContentType = $logoContentType;

        return $this;
    }

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

    /**
     * Set logoFileSize
     *
     * @param integer $logoFileSize
     * @return Client
     */
    public function setLogoFileSize($logoFileSize)
    {
        $this->logoFileSize = $logoFileSize;

        return $this;
    }

    /**
     * Get logoFileSize
     *
     * @return integer 
     */
    public function getLogoFileSize()
    {
        return $this->logoFileSize;
    }

    /**
     * Set logoUpdatedAt
     *
     * @param \DateTime $logoUpdatedAt
     * @return Client
     */
    public function setLogoUpdatedAt($logoUpdatedAt)
    {
        $this->logoUpdatedAt = $logoUpdatedAt;

        return $this;
    }

    /**
     * Get logoUpdatedAt
     *
     * @return \DateTime 
     */
    public function getLogoUpdatedAt()
    {
        return $this->logoUpdatedAt;
    }

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return Client
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     * @return Client
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime 
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * Add projects
     *
     * @param \Acme\KeywordBundle\Entity\Project $projects
     * @return Client
     */
    public function addProject(\Acme\KeywordBundle\Entity\Project $projects)
    {
        $this->projects[] = $projects;

        return $this;
    }

    /**
     * Remove projects
     *
     * @param \Acme\KeywordBundle\Entity\Project $projects
     */
    public function removeProject(\Acme\KeywordBundle\Entity\Project $projects)
    {
        $this->projects->removeElement($projects);
    }

    /**
     * Get projects
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getProjects()
    {
        return $this->projects;
    }

    /**
     * Add files
     *
     * @param \Acme\KeywordBundle\Entity\File $files
     * @return Client
     */
    public function addFile(\Acme\KeywordBundle\Entity\File $files)
    {
        $this->files[] = $files;

        return $this;
    }

    /**
     * Remove files
     *
     * @param \Acme\KeywordBundle\Entity\File $files
     */
    public function removeFile(\Acme\KeywordBundle\Entity\File $files)
    {
        $this->files->removeElement($files);
    }

    /**
     * Get files
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getFiles()
    {
        return $this->files;
    }

    /**
     * Add contacts
     *
     * @param \Acme\KeywordBundle\Entity\Contact $contacts
     * @return Client
     */
    public function addContact(\Acme\KeywordBundle\Entity\Contact $contacts)
    {
        $this->contacts[] = $contacts;

        return $this;
    }

    /**
     * Remove contacts
     *
     * @param \Acme\KeywordBundle\Entity\Contact $contacts
     */
    public function removeContact(\Acme\KeywordBundle\Entity\Contact $contacts)
    {
        $this->contacts->removeElement($contacts);
    }

    /**
     * Get contacts
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getContacts()
    {
        return $this->contacts;
    }

public function __toString()
    {
    return $this->name;
    }

}
客户

show.html.twig

项目清单应该在

项目清单

下进行
{% extends '::base.html.twig' %}

{% block body -%}
    <h1>Client</h1>

    <table class="record_properties">
        <tbody>
        <!--
            <tr>
                <th>Id</th>
                <td>{{ entity.id }}</td>
            </tr>
            <tr>
        -->    
                <th>Name</th>
                <td>{{ entity.name }}</td>
            </tr>
            <tr>
        <!--
                <th>Createdat</th>
                <td>{{ entity.createdAt|date('Y-m-d H:i:s') }}</td>
            </tr>
            <tr>
                <th>Updatedat</th>
                <td>{{ entity.updatedAt|date('Y-m-d H:i:s') }}</td>
            </tr>
            <tr>
                <th>Logofilename</th>
                <td>{{ entity.logoFileName }}</td>
            </tr>
            <tr>
                <th>Logocontenttype</th>
                <td>{{ entity.logoContentType }}</td>
            </tr>
            <tr>
                <th>Logofilesize</th>
                <td>{{ entity.logoFileSize }}</td>
            </tr>
            <tr>
                <th>Logoupdatedat</th>
                <td>{{ entity.logoUpdatedAt|date('Y-m-d H:i:s') }}</td>
            </tr>
        -->
        </tbody>
    </table>

        <ul class="record_actions">
    <li>
        <a href="{{ path('clients') }}">
            Back to the list
        </a>
    </li>
    <li>
        <a href="{{ path('clients_edit', { 'id': entity.id }) }}">
            Edit
        </a>
    </li>
    <li>{{ form(delete_form) }}</li>
</ul>

 <h1>Project list</h1>



{% endblock %}

1 个答案:

答案 0 :(得分:0)

如果您的实体关系配置正确(它们看起来像是什么),那么您可以在twig中访问项目数组direclty:

{% for project in entity.projects %}
    Name: {{ project.name }}
    ...
{% endfor %}