2015-05-25 6 views
7

Symfony आदेश सिद्धांत: उत्पन्न: crud उत्पन्न नियंत्रक < प्रपत्र और इसके दृश्यों को। लेकिन, इंडेक्स में अन्य टेबल संदर्भ फ़ील्ड "कई से एक" नहीं हैं।Symfony crud सूचकांक दृश्य उत्पन्न है, जहां संदर्भ क्षेत्रों की जरूरत नहीं है

इकाई मॉडल:

<?php 

namespace Acme\Bundle\AdminBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Albums 
* 
* @ORM\Table(name="albums", indexes={@ORM\Index(name="IDX_F4E2474F3D8E604F", columns={"parent"})}) 
* @ORM\Entity 
*/ 
class Albums 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="SEQUENCE") 
    * @ORM\SequenceGenerator(sequenceName="albums_id_seq", allocationSize=1, initialValue=1) 
    */ 
    private $id; 

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

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

    /** 
    * @var \ParentAlbums 
    * 
    * @ORM\ManyToOne(targetEntity="ParentAlbums") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="parent", referencedColumnName="id") 
    * }) 
    */ 
    private $parent; 



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

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

     return $this; 
    } 

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

    /** 
    * Set sort 
    * 
    * @param integer $sort 
    * @return Albums 
    */ 
    public function setSort($sort) 
    { 
     $this->sort = $sort; 

     return $this; 
    } 

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

    /** 
    * Set parent 
    * 
    * @param \Acme\Bundle\AdminBundle\Entity\ParentAlbums $parent 
    * @return Albums 
    */ 
    public function setParent(\Acme\Bundle\AdminBundle\Entity\ParentAlbums $parent = null) 
    { 
     $this->parent = $parent; 

     return $this; 
    } 

    /** 
    * Get parent 
    * 
    * @return \Acme\Bundle\AdminBundle\Entity\ParentAlbums 
    */ 
    public function getParent() 
    { 
     return $this->parent; 
    } 
} 

Index.html.twig - टेबल सिर अनुभाग:

<thead> 
     <tr> 
      <th>Id</th> 
      <th>Name</th> 
      <th>Sort</th> 
      <th>{{ 'views.index.actions'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</th> 
     </tr> 
    </thead> 

enter image description here

+0

यह 3 क्षेत्रों में शामिल होना चाहिए संघों को संभालने के लिए कैसे जांच करने के लिए है। जनक क्षेत्र उत्पन्न नहीं। –

+0

सिम्फोनी crud generateor आदेश: php एप्लिकेशन/कंसोल सिद्धांत: उत्पन्न: crud --entity = AdminBundle: एल्बम --route-उपसर्ग = व्यवस्थापक/--with-लिखना –

उत्तर

7

यह DoctrineCrudGenerator के सामान्य व्यवहार है क्योंकि जनरेटर केवल Doctrine\ORM\Mapping\ClassMetadataInfo::$fieldMappings सरणी का उपयोग करता है तालिका उत्पन्न करने के लिए है, लेकिन ParentAlbum-ManyToOne: आप यह बहुत थोड़ा index.html.twig बदलकर सरल प्रदर्शित कर सकता है एसोसिएशन Doctrine\ORM\Mapping\ClassMetadataInfo::$associationMappings सरणी में स्थित है। तो यह क्रूड पीढ़ी पर कभी पहचाना नहीं जाएगा।

एक संभव समाधान का उपयोग मैं सिम्फोनी-प्रदर्शन आवेदन की टिप्पणी इकाई प्रदर्शित करने के लिए:

<?php 

/* 
* This file is part of the Symfony package. 
* 
* (c) Fabien Potencier <[email protected]> 
* 
* For the full copyright and license information, please view the LICENSE 
* file that was distributed with this source code. 
*/ 

namespace AppBundle\Entity; 

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

/** 
* @ORM\Entity 
* 
* Defines the properties of the Comment entity to represent the blog comments. 
* See http://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class 
* 
* Tip: if you have an existing database, you can generate these entity class automatically. 
* See http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html 
* 
* @author Ryan Weaver <[email protected]> 
* @author Javier Eguiluz <[email protected]> 
*/ 
class Comment 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue 
    * @ORM\Column(type="integer") 
    */ 
    private $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments") 
    * @ORM\JoinColumn(nullable=false) 
    */ 
    private $post; 

    /** 
    * @ORM\Column(type="text") 
    * @Assert\NotBlank(message="Please don't leave your comment blank!") 
    * @Assert\Length(
    *  min = "5", 
    *  minMessage = "Comment is too short ({{ limit }} characters minimum)", 
    *  max = "10000", 
    *  maxMessage = "Comment is too long ({{ limit }} characters maximum)" 
    *) 
    */ 
    private $content; 

    /** 
    * @ORM\Column(type="string") 
    * @Assert\Email() 
    */ 
    private $authorEmail; 

    /** 
    * @ORM\Column(type="datetime") 
    * @Assert\DateTime() 
    */ 
    private $publishedAt; 

    public function __construct() 
    { 
     $this->publishedAt = new \DateTime(); 
    } 

    /** 
    * @Assert\True(message = "The content of this comment is considered spam.") 
    */ 
    public function isLegitComment() 
    { 
     $containsInvalidCharacters = false !== strpos($this->content, '@'); 

     return !$containsInvalidCharacters; 
    } 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function getContent() 
    { 
     return $this->content; 
    } 

    public function setContent($content) 
    { 
     $this->content = $content; 
    } 

    public function getAuthorEmail() 
    { 
     return $this->authorEmail; 
    } 

    public function setAuthorEmail($authorEmail) 
    { 
     $this->authorEmail = $authorEmail; 
    } 

    public function getPublishedAt() 
    { 
     return $this->publishedAt; 
    } 

    public function setPublishedAt($publishedAt) 
    { 
     $this->publishedAt = $publishedAt; 
    } 

    public function getPost() 
    { 
     return $this->post; 
    } 

    public function setPost(Post $post = null) 
    { 
     $this->post = $post; 
    } 
} 

आईडी, सामग्री, AUTHOREMAIL और publishedAt और पोस्ट करने के लिए एक ManyToOne संघ की तरह "सामान्य" स्तंभ हैं इकाई। इस इकाई के लिए निम्नलिखित मेटाडाटा उत्पन्न कर रहे हैं:

Doctrine\ORM\Mapping\ClassMetadata {#437 
    +name: "AppBundle\Entity\Comment" 
    +namespace: "AppBundle\Entity" 
    +rootEntityName: "AppBundle\Entity\Comment" 
    +customGeneratorDefinition: null 
    +customRepositoryClassName: null 
    +isMappedSuperclass: false 
    +isEmbeddedClass: false 
    +parentClasses: [] 
    +subClasses: [] 
    +embeddedClasses: [] 
    +namedQueries: [] 
    +namedNativeQueries: [] 
    +sqlResultSetMappings: [] 
    +identifier: array:1 [ 
    0 => "id" 
    ] 
    +inheritanceType: 1 
    +generatorType: 4 
    +fieldMappings: array:4 [ 
    "id" => array:9 [ 
     "fieldName" => "id" 
     "type" => "integer" 
     "scale" => 0 
     "length" => null 
     "unique" => false 
     "nullable" => false 
     "precision" => 0 
     "id" => true 
     "columnName" => "id" 
    ] 
    "content" => array:8 [ 
     "fieldName" => "content" 
     "type" => "text" 
     "scale" => 0 
     "length" => null 
     "unique" => false 
     "nullable" => false 
     "precision" => 0 
     "columnName" => "content" 
    ] 
    "authorEmail" => array:8 [ 
     "fieldName" => "authorEmail" 
     "type" => "string" 
     "scale" => 0 
     "length" => null 
     "unique" => false 
     "nullable" => false 
     "precision" => 0 
     "columnName" => "authorEmail" 
    ] 
    "publishedAt" => array:8 [ 
     "fieldName" => "publishedAt" 
     "type" => "datetime" 
     "scale" => 0 
     "length" => null 
     "unique" => false 
     "nullable" => false 
     "precision" => 0 
     "columnName" => "publishedAt" 
    ] 
    ] 
    +fieldNames: array:4 [ 
    "id" => "id" 
    "content" => "content" 
    "authorEmail" => "authorEmail" 
    "publishedAt" => "publishedAt" 
    ] 
    +columnNames: array:4 [ 
    "id" => "id" 
    "content" => "content" 
    "authorEmail" => "authorEmail" 
    "publishedAt" => "publishedAt" 
    ] 
    +discriminatorValue: null 
    +discriminatorMap: [] 
    +discriminatorColumn: null 
    +table: array:1 [ 
    "name" => "Comment" 
    ] 
    +lifecycleCallbacks: [] 
    +entityListeners: [] 
    +associationMappings: array:1 [ 
    "post" => array:19 [ 
     "fieldName" => "post" 
     "joinColumns" => array:1 [ 
     0 => array:6 [ 
      "name" => "post_id" 
      "unique" => false 
      "nullable" => false 
      "onDelete" => null 
      "columnDefinition" => null 
      "referencedColumnName" => "id" 
     ] 
     ] 
     "cascade" => [] 
     "inversedBy" => "comments" 
     "targetEntity" => "AppBundle\Entity\Post" 
     "fetch" => 2 
     "type" => 2 
     "mappedBy" => null 
     "isOwningSide" => true 
     "sourceEntity" => "AppBundle\Entity\Comment" 
     "isCascadeRemove" => false 
     "isCascadePersist" => false 
     "isCascadeRefresh" => false 
     "isCascadeMerge" => false 
     "isCascadeDetach" => false 
     "sourceToTargetKeyColumns" => array:1 [ 
     "post_id" => "id" 
     ] 
     "joinColumnFieldNames" => array:1 [ 
     "post_id" => "post_id" 
     ] 
     "targetToSourceKeyColumns" => array:1 [ 
     "id" => "post_id" 
     ] 
     "orphanRemoval" => false 
    ] 
    ] 
    +isIdentifierComposite: false 
    +containsForeignIdentifier: false 
    +idGenerator: Doctrine\ORM\Id\IdentityGenerator {#439 
    -sequenceName: null 
    } 
    +sequenceGeneratorDefinition: null 
    +tableGeneratorDefinition: null 
    +changeTrackingPolicy: 1 
    +isVersioned: null 
    +versionField: null 
    +cache: null 
    +reflClass: null 
    +isReadOnly: false 
    #namingStrategy: Doctrine\ORM\Mapping\DefaultNamingStrategy {#407} 
    +reflFields: array:5 [ 
    "id" => null 
    "content" => null 
    "authorEmail" => null 
    "publishedAt" => null 
    "post" => null 
    ] 
    -instantiator: Doctrine\Instantiator\Instantiator {#438} 
} 

आप देख सकते हैं, कि सामान्य क्षेत्रों, fieldMappings सरणी में स्थित हैं, जबकि संघ associationMappings सरणी में रहती है।

<thead> 
    <tr> 
     <th>Id</th> 
     <th>Content</th> 
     <th>Authoremail</th> 
     <th>Publishedat</th> 
     <th>Actions</th> 
    </tr> 
</thead> 

अब, यह व्यवहार बदलने के लिए, तो आप बस "मर्ज" करने के लिए associationMappings सरणी है: के लिए टिप्पणी इकाई crud जनरेटर चल रहा है के बाद संघ के बिना केवल "सामान्य" स्तंभों के लिए तालिका का उत्पादन क्रूड पीढ़ी पर फील्ड मैपिंग सरणी। आप इसे Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator में कर सकते हैं। उत्पादन के लिए, आप Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator और Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand ओवरराइड और अपने खुद के वर्ग में परिवर्तन करने की आवश्यकता। लेकिन अभी अवधारणा का एक सबूत के रूप में मैं शीघ्र और वास्तव में गंदा हैक करना होगा, एक सीधे DoctrineCrudGenerator को निम्न परिवर्तन जोड़ें:

/** 
* Generates a CRUD controller. 
* 
* @author Fabien Potencier <[email protected]> 
*/ 
class DoctrineCrudGenerator extends Generator 
{ 

    // ... 

    /** 
    * Generates the index.html.twig template in the final bundle. 
    * 
    * @param string $dir The path to the folder that hosts templates in the bundle 
    */ 
    protected function generateIndexView($dir) 
    { 
     $this->renderFile(
      'crud/views/index.html.twig.twig', 
      $dir . '/index.html.twig', 
      array(
       'bundle' => $this->bundle->getName(), 
       'entity' => $this->entity, 
       'identifier' => $this->metadata->identifier[0], 

       // Use the function instead of the "raw" fieldMappings array 
       // 'fields' => $this->metadata->fieldMappings, 
       'fields' => $this->processFieldMappings(), 

       'actions' => $this->actions, 
       'record_actions' => $this->getRecordActions(), 
       'route_prefix' => $this->routePrefix, 
       'route_name_prefix' => $this->routeNamePrefix, 
      ) 
     ); 
    } 

    // ... 
    /** 
    * Add the associations to the array 
    * 
    * @return array 
    */ 
    protected function processFieldMappings() 
    { 

     /** @var \Doctrine\ORM\Mapping\ClassMetadata $metadata */ 
     $metadata = $this->metadata; 

     $fields = $metadata->fieldMappings; 
     $associationMappings = $metadata->associationMappings; 

     foreach ($associationMappings as $k => $a) { 
      // Add the field only if it is a ToOne association and if the targetEntity implements the __toString method 
      if ($a['type'] & ClassMetadataInfo::TO_ONE && method_exists($a['targetEntity'], '__toString')) { 
       $fields[$k] = array(
        "fieldName" => $a["fieldName"], 
        "type" => "text", 
        "scale" => 0, 
        "length" => null, 
        "unique" => false, 
        "nullable" => false, 
        "precision" => 0, 
        "columnName" => $k 
       ); 
      } 
     } 

     return $fields; 
    } 
} 

बदलावों के बाद और आप पोस्ट संस्था के लिए __toString कहा कि यदि, जनरेटर निम्नलिखित कोड उत्पन्न करता है:

<table class="records_list"> 
    <thead> 
     <tr> 
      <th>Id</th> 
      <th>Content</th> 
      <th>Authoremail</th> 
      <th>Publishedat</th> 
      <th>Post</th> 
      <th>Actions</th> 
     </tr> 
    </thead> 
    <tbody> 
    {% for entity in entities %} 
     <tr> 
      <td><a href="{{ path('comment_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td> 
      <td>{{ entity.content }}</td> 
      <td>{{ entity.authorEmail }}</td> 
      <td>{% if entity.publishedAt %}{{ entity.publishedAt|date('Y-m-d H:i:s') }}{% endif %}</td> 
      <td>{{ entity.post }}</td> 
      <td> 
      <ul> 
       <li> 
        <a href="{{ path('comment_show', { 'id': entity.id }) }}">show</a> 
       </li> 
       <li> 
        <a href="{{ path('comment_edit', { 'id': entity.id }) }}">edit</a> 
       </li> 
      </ul> 
      </td> 
     </tr> 
    {% endfor %} 
    </tbody> 
</table> 

आप देख सकते हैं, पोस्ट एसोसिएशन अब मान्यता प्राप्त है। यदि आप अपने स्वयं के जनरेटर लेखन शुरू करना चाहते हैं तो आपको एक प्रवेश बिंदु के रूप में उपयोग कर सकते हैं। नाम, प्रकार, माता पिता: लेकिन आप कैसे ToMany संघों को संभालने के लिए और रूपों और इतने पर ..

+0

वास्तव में, मैं एक और तरीका 'फ़ील्ड' => array_merge ($ this-> metadata-> fieldMappings, $ this-> metadata-> associationMappings), –

+0

ज़रूर, आप कर सकते हैं द्वारा समस्या हल हो। लेकिन यदि आप टेम्पलेट्स को अनुकूलित किए बिना, ToMany एसोसिएशन के लिए 'array_merge' का उपयोग करना चाहते हैं, तो आप समस्याओं में भाग लेंगे। मैं यह भी दावा नहीं करता कि मेरा दृष्टिकोण ही एकमात्र है। यह आप पर है ;) – skroczek

-1

crud आदेश एकाधिक फ़ाइलों लेकिन इसके बारे में एक त्वरित पीढ़ी के साथ आप में मदद करता है सब कुछ नहीं है। $ माता-पिता किसी अन्य इकाई के लिए एक सूचक है। क्रूड विधि यह जानने में सक्षम नहीं है कि आप इस इकाई से क्या दिखाना चाहते हैं। कल्पना करें कि अभिभावकअल्बम्स की संपत्ति '$ name' है। ,

<thead> 
    <tr> 
     <th>Id</th> 
     <th>Name</th> 
     <th>Parent</th> 
     <th>Sort</th> 
     <th>{{ 'views.index.actions'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</th> 
    </tr> 
</thead> 
    <tbody> 
    {% for entity in entities %} 
     <tr> 
      <td>{{ entity.id }}</td> 
      <td>{{ entity.name }}</td> 
      <td>{{ entity.parent.name }}</td> 
      <td>{{ entity.sort }}</td> 
      <td> 
      <ul> 
        // actions here 
      </ul> 
      </td> 
     </tr> 
    {% endfor %} 
    </tbody> 
+0

--overwrite --format = एनोटेशन --no-बातचीत एलबम Entity.parent.name, entity.parent को लिखने की कोई आवश्यकता नहीं है। –

संबंधित मुद्दे