2012-11-15 62 views
1

我是Symfony2中的新成員,但我使用Rails。我願做這樣的事情在Symfony2中:Symfony2 + Doctrine vs Rails如何從相關表中檢索數據

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)?

感謝您的幫助......

神話拉什

回答

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

希望這對你的工作。

大衛

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 docs找到並在the templating docs


但是,RoR使用Active Record來從數據庫中讀取信息。在Symfony2中沒有「模型」。你可以使用任何你想要的。默認情況下,它使用Doctrine2,但也包括Propel

你也可以使用自己喜歡的'模型'庫。如果您正在尋找類似active record in PHP的東西,您會發現很多可用的有用庫。

相關問題