2017-07-21 9 views
0

Ich versuche, Admin-Seite für Produkt, die Beziehung 1: 1 mit Bild hat.One-to-One-Beziehung in SonataAdmin

Produkt

/** 
* @ORM\Entity 
* @ORM\Table(name="products") 
    class Product 
    { 

     /** 
     * @ORM\Column(type="integer") 
     * @ORM\GeneratedValue 
     * @ORM\Id 
     * @var int 
     */ 
     private $id = 0; 

     /** 
     * @ORM\OneToOne(targetEntity="Image", mappedBy="product") 
     */ 


       private $image; 
    /** 
    * @return Image 
    */ 
    public function getImage(): ?Image 
    { 
     return $this->image; 
    } 

    /** 
    * @param Image $image 
    */ 
    public function setImage(Image $image) 
    { 
     $this->image = $image; 
     return $this; 
    } 
    } 

Bild

/** 
* @ORM\Entity 
* @ORM\Table(name="images") 
*/ 
class Image 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue 
    * @ORM\Id 
    * @var int 
    */ 
    private $id = 0; 

    /** 
    * @ORM\OneToOne(targetEntity="Product", inversedBy="image") 
    * @ORM\JoinColumn(name="product_id", referencedColumnName="id") 
    */ 
    private $product; 

    /** 
    * @return mixed 
    */ 
    public function getProduct() 
    { 
     return $this->product; 
    } 

    public function setProduct(Product $product) 
    { 
     $this->product = $product; 
    } 
} 

ProductAdmin

class ProductAdmin extends AbstractAdmin 
{ 
    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper->add('image', 'sonata_type_admin', array('label' => 'Okładka', 'by_reference' => false,)); 
} 

ImageAdmin

class ImageAdmin extends AbstractAdmin 
{ 
    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
      ->add('file', 'file', array('label' => 'Okładka', 'required' => false)) 
      ->add('path', 'text', array('label' => 'Scieżka do pliku', 'required' => false)); 

    } 

setuped I Dienste korrekt, aber ich kann nicht Produkt bearbeiten und nach neuen Fehler

nicht in der Lage, das Objekt mit ID finden Geting i Sparend: 0

Antwort

0

Versuchen Sie nicht, Ihre $ id zu initialisieren

private $id = 0; // =====> this is a private $id; 
0

Sie haben mehrere Fehler. Lass uns versuchen, deinen Code zu korrigieren.

  1. einfach den Anleitungen folgen und Schreib Anmerkung für $ id setzen:

    /** 
    * @var integer $id 
    * 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
    
  2. Hoffnung, dass dies nur ein Tippfehler mit "Bild?":

    /** 
    * @return Image 
    */ 
    public function getImage() : Image 
    { 
         return $this->image; 
    } 
    
  3. Und endlich.

    class ProductAdmin extends AbstractAdmin 
    { 
        protected function configureFormFields(FormMapper $formMapper) 
        { 
         $formMapper 
          ->add('image', 'sonata_type_model_list', [ 
             'btn_add'  => true,  //Or you can specify a custom label 
             'btn_list'  => 'list button!',  //which will be translated 
             'btn_delete' => false,    //or hide the button. 
             'btn_catalogue' => 'messages', //Custom translation domain for buttons 
             'label'   => 'My image', 
            ], [ 
             'placeholder' => $this->trans('messages.no_images_message'), 
             'edit'   => 'standard', 
             'inline'  => 'standard', 
             'sortable'  => 'id', 
            ]) 
          ; 
        } 
    } 
    
Verwandte Themen