Symfony2 + Doctrine vs Rails如何从相关表中检索数据

时间:2012-11-15 09:24:37

标签: ruby-on-rails symfony doctrine-orm

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

class Manager < ActiveRecord::Base
    has_many :employees
end

然后问这样的对象:

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

我在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;
}

如何在Symfony2中实现这些(上面的)功能(在ORR中简单的RoR)?

感谢您的帮助...

神话冲刺

2 个答案:

答案 0 :(得分:1)

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

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

让我们从一对多,双向关系(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;
    // ...
}

控制器

<?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,
    ));
  }

模板

{# 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 %}

希望这对你有用。

大卫

答案 1 :(得分:0)

如果您使用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 %}

可以在the Symfony2 doctrine docsthe templating docs中找到更多信息。


但是,RoR使用Active Record来保持从数据库读取信息。在Symfony2中没有“模型”。你可以使用你想要的任何东西。默认情况下,它使用Doctrine2,但也包括Propel

您也可以使用自己喜欢的“模特”图书馆。如果您正在寻找类似active record in PHP的内容,您会发现许多有用的库。