Symfony2+Doctrine vs Rails howto retrieve data from related table

时间:2022-08-11 06:48:27

I am new in Symfony2, but I use Rails. I would like to do something like this in Symfony2:

我是Symfony2的新手,但我使用的是Rails。我想在Symfony2中做这样的事情:

class Manager < ActiveRecord::Base
    has_many :employees
end

and ask then the object like this:

然后问这样的对象:

m = Manager.find(1)
# Sends SQL query SELECT COUNT * FROM EMPLOYEES WHERE MANAGER_ID = 1
count = m.employees.count

# Sends SQL query SELECT * FROM EMPLOYEES WHERE MANAGER_ID = 1
m.employees.each do |e|
    puts e.name
end

I have this code in Symfony2...

我在Symfony2中有这个代码......

/**
 * @Entity @Table(name="Manager")
 */
class Manager
{
    /**
     * @Id @Column(type="integer") @GeneratedValue
     */
    public $id;

    /**
     * @Column(type="string")
     */
    public $description;


   /**
    * @OneToMany(targetEntity="User", mappedBy="manager")
    * @var User[]
    */
    private $employees;
}

How can I do implementation of these (above) functionality (which is in RoR easy made by its ORM) in Symfony2?

如何在Symfony2中实现这些(上面)功能(在ORR中简单地使用它的ORM)?

Thanks for help...

感谢帮助...

Myth Rush

神话匆匆

2 个解决方案

#1


1  

m = Manager.find(1)
# Sends SQL query SELECT * FROM EMPLOYEES WHERE MANAGER_ID = 1
m.employees.each do |e|
    puts e.name
end

Seems that we are looking for Employees depending on Manager id.

似乎我们正在寻找员工,具体取决于经理ID。

Let's start with a One-To-Many, Bidirectional relation (doctrine documentation)

让我们从一对多,双向关系(学说文档)开始

<?php
/** @Entity **/
class Manager
{
    // ...
    /**
     * @OneToMany(targetEntity="Employee", mappedBy="manager")
     **/
    private $employees;
    // ...

    public function __construct() {
        $this->employees = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

/** @Entity **/
class Employee
{
    // ...
    /**
     * @ManyToOne(targetEntity="Manager", inversedBy="employees")
     * @JoinColumn(name="manager_id", referencedColumnName="id")
     **/
    private $manager;
    // ...
}

The controller

控制器

<?php
  // src/Acme/DemoBundle/Controller/ManagerController.php

  // ...
  public function showAllEmployeesForManagerAction($managerId)
  {
    $repo = $this->getDoctrine()->getRepository('AcmeDemoBundle:Manager');

    $manager         = $repo->find(managerId);
    $employees       = $manager->getEmployees();
    // counting employees
    $employeesNumber = count($employees); // not passed to template

    return $this->render('AcmeDemoBundle:Employees:list.html.twig', array(
        'manager' => $manager,
        'employees' => $employees,
    ));
  }

The template

模板

{# src/Acme/DemoBundle/Resources/views/Employees/list.html.twig #}

{% extend '::base.html.twig' %}

{% block body -%}
    <h2>Employees depending on Manager {{manager.name}}</h2>
    <p>Number of employees : {{employees|length}}</p>
    <ul>
    {% for employee in employees %}
       <li>{{ employee.name }}</li>
    {% endfor %}
    </ul>
{% endblock %}

Hope this work for you.

希望这对你有用。

David

大卫

#2


0  

If you use the Dcotrine2 orm, it looks something like this:

如果您使用Dcotrine2 orm,它看起来像这样:

<?php
// src/Acme/DemoBundle/Controller/ManagerController.php

// ...
public function showAllAction()
{
    $repo = $this->getDoctrine()->getRepository('AcmeDemoBundle:Manager');
    $managers = $repo->findAll();

    return $this->render('AcmeDemoBundle:Manager:list.html.twig', array(
        'managers' => $managers,
    ));
}
{% extend '::base.html.twig' %}

{% block body -%}
    {% for manager in managers -%}
        {{ manager.name }}
    {% endfor %}
{% endblock %}

More information can be found in the Symfony2 doctrine docs and in the templating docs.

可以在Symfony2学说文档和模板文档中找到更多信息。


However, RoR uses Active Record to persist en read information from a database. In Symfony2 there is no 'Model'. You can use whatever you want. By default it uses Doctrine2, but Propel is included as well.

但是,RoR使用Active Record来保持从数据库读取信息。在Symfony2中没有“模型”。你可以随心所欲地使用它。默认情况下它使用Doctrine2,但也包括Propel。

You can use your own favorite 'Model' library too. If you are looking for something like active record in PHP you will find lots of usefull libraries you can use.

您也可以使用自己喜欢的“模型”库。如果您正在寻找像PHP中的活动记录这样的东西,您会发现许多有用的库可以使用。

#1


1  

m = Manager.find(1)
# Sends SQL query SELECT * FROM EMPLOYEES WHERE MANAGER_ID = 1
m.employees.each do |e|
    puts e.name
end

Seems that we are looking for Employees depending on Manager id.

似乎我们正在寻找员工,具体取决于经理ID。

Let's start with a One-To-Many, Bidirectional relation (doctrine documentation)

让我们从一对多,双向关系(学说文档)开始

<?php
/** @Entity **/
class Manager
{
    // ...
    /**
     * @OneToMany(targetEntity="Employee", mappedBy="manager")
     **/
    private $employees;
    // ...

    public function __construct() {
        $this->employees = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

/** @Entity **/
class Employee
{
    // ...
    /**
     * @ManyToOne(targetEntity="Manager", inversedBy="employees")
     * @JoinColumn(name="manager_id", referencedColumnName="id")
     **/
    private $manager;
    // ...
}

The controller

控制器

<?php
  // src/Acme/DemoBundle/Controller/ManagerController.php

  // ...
  public function showAllEmployeesForManagerAction($managerId)
  {
    $repo = $this->getDoctrine()->getRepository('AcmeDemoBundle:Manager');

    $manager         = $repo->find(managerId);
    $employees       = $manager->getEmployees();
    // counting employees
    $employeesNumber = count($employees); // not passed to template

    return $this->render('AcmeDemoBundle:Employees:list.html.twig', array(
        'manager' => $manager,
        'employees' => $employees,
    ));
  }

The template

模板

{# src/Acme/DemoBundle/Resources/views/Employees/list.html.twig #}

{% extend '::base.html.twig' %}

{% block body -%}
    <h2>Employees depending on Manager {{manager.name}}</h2>
    <p>Number of employees : {{employees|length}}</p>
    <ul>
    {% for employee in employees %}
       <li>{{ employee.name }}</li>
    {% endfor %}
    </ul>
{% endblock %}

Hope this work for you.

希望这对你有用。

David

大卫

#2


0  

If you use the Dcotrine2 orm, it looks something like this:

如果您使用Dcotrine2 orm,它看起来像这样:

<?php
// src/Acme/DemoBundle/Controller/ManagerController.php

// ...
public function showAllAction()
{
    $repo = $this->getDoctrine()->getRepository('AcmeDemoBundle:Manager');
    $managers = $repo->findAll();

    return $this->render('AcmeDemoBundle:Manager:list.html.twig', array(
        'managers' => $managers,
    ));
}
{% extend '::base.html.twig' %}

{% block body -%}
    {% for manager in managers -%}
        {{ manager.name }}
    {% endfor %}
{% endblock %}

More information can be found in the Symfony2 doctrine docs and in the templating docs.

可以在Symfony2学说文档和模板文档中找到更多信息。


However, RoR uses Active Record to persist en read information from a database. In Symfony2 there is no 'Model'. You can use whatever you want. By default it uses Doctrine2, but Propel is included as well.

但是,RoR使用Active Record来保持从数据库读取信息。在Symfony2中没有“模型”。你可以随心所欲地使用它。默认情况下它使用Doctrine2,但也包括Propel。

You can use your own favorite 'Model' library too. If you are looking for something like active record in PHP you will find lots of usefull libraries you can use.

您也可以使用自己喜欢的“模型”库。如果您正在寻找像PHP中的活动记录这样的东西,您会发现许多有用的库可以使用。