2011-11-23 19 views
35

क्या आप जानते हैं कैसेइकाई वर्ग की तालिका नाम

इकाई कक्षा

<?php 

namespace Acme\StoreBundle\Entity; 

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

/** 
* Acme\StoreBundle\Entity\User 
* 
* @ORM\Table(name="users") 
* @ORM\Entity 
*/ 
class User 

मैं अब की तालिका नाम प्राप्त करना चाहते हैं मेरी नियंत्रक कक्षा में एक इकाई की घोषणा से तालिका नाम पाने के लिए जाओ उपयोगकर्ता इकाई, मैं अपने Symfony2 नियंत्रक में यह कैसे करूँगा?

उत्तर

80

एक नियंत्रक के अंदर से आप का प्रयोग करेंगे:

$em = $this->getDoctrine()->getManager(); 
$tableName = $em->getClassMetadata('StoreBundle:User')->getTableName(); 

ध्यान दें कि getClassMetadata विधि इकाई के बारे में दिलचस्प जानकारी का एक समूह देता है।

+1

टेबल नाम में शामिल होने के बारे में कैसे? क्या आप जानते हैं कि इसे कैसे प्राप्त करें? –

+5

PHP 5.5+ के साथ आप निर्मित कक्षा निरंतर :: कक्षा का उपयोग कर सकते हैं। '' '$ tableName = $ em-> getClassMetadata (उपयोगकर्ता :: कक्षा) -> getTableName();' '' –

0

Symfony 2.3 & सिद्धांत 2 के साथ यह मेरे लिए काम किया:

// my entity is called "Record" 
// Acme/DemoBundle/Entity/RecordRepository.php 
class RecordRepository extends EntityRepository 
{ 

    /** 
    * Sets the primary table definition. The provided array supports the 
    * following structure: 
    * 
    * name => <tableName> (optional, defaults to class name) 
    * indexes => array of indexes (optional) 
    * uniqueConstraints => array of constraints (optional) 
    * 
    * If a key is omitted, the current value is kept. 
    * 
    * @param array $table The table description. 
    */ 
    public function setDataTableName($tableInfo) { 
     return $this->getEntityManager()->getClassMetadata('AcmeDemoBundle:Record')->setPrimaryTable($tableInfo); 
    } 

} 
2

मैं एक बहुत-से-अनेक संबंध (FOSUserBundle का उपयोग कर) में एक मानचित्रण तालिका का नाम पता की जरूरत है। शायद यह किसी की मदद करता है:

$groupAssociation = $this->getEntityManager() 
          ->getClassMetadata('UOACLBundle:User') 
          ->getAssociationsByTargetClass(Group::class); 

    // 'groups' is the name of the property in my User Class 
    $mappingTable = $groupAssociation['groups']['joinTable']['name']; 
संबंधित मुद्दे