0

というテーブルstockとの関係 1:m を持つテーブルがありますwarranty。私はストックテーブルのためにこのエンティティを作りました:

<?php

namespace StockBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
use CompanyBundle\Entity\Company;
use StockBundle\Entity\NStockStatus;
use ProductBundle\Entity\NLength;
use ProductBundle\Entity\NWeight;

/**
 * @ORM\Table(name="stock")
 * @ORM\Entity(repositoryClass="StockBundle\Entity\Repository\KStockRepository")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class KStock {

    /**
     * @ORM\Id      
     * @ORM\ManyToOne(targetEntity="ProductBundle\Entity\Product", inversedBy="stocks" )      
     * @ORM\JoinColumn(name="product", referencedColumnName="upc")   
     */
    protected $product;

    /**
     * @ORM\Id      
     * @ORM\ManyToOne(targetEntity="CompanyBundle\Entity\Company", inversedBy="companyHasStock" )      
     * @ORM\JoinColumn(name="company", referencedColumnName="id")   
     */
    protected $company;

    /**
     * 
     * @ORM\ManyToOne(targetEntity="StockBundle\Entity\NCondition", inversedBy="stocks" )      
     * @ORM\JoinColumn(name="kcondition", referencedColumnName="id")   
     */
    protected $condition;

    /**
     * 
     * @ORM\ManyToOne(targetEntity="StockBundle\Entity\NStockStatus", inversedBy="stocks" )      
     * @ORM\JoinColumn(name="status", referencedColumnName="id")   
     */
    protected $status;

    /**
     * 
     * @ORM\ManyToOne(targetEntity="StockBundle\Entity\Warranty", inversedBy="stocks" )      
     * @ORM\JoinColumn(name="warranty", referencedColumnName="id")   
     */
//    protected $warranty;

    /**
     * @ORM\Column(type="string", length=255, name="sku")
     */
    protected $sku;

    /**
     * @ORM\Column(type="integer")   
     */
    protected $availability;

    /**
     * @ORM\Column(type="string", length=255)    
     */
    protected $description;

    /**
     *
     * @ORM\Column(type="decimal", precision=19, scale=4)         
     */
    protected $price;

    /**
     * @ORM\ManyToOne(targetEntity="StockBundle\Entity\NUnit")
     * @ORM\JoinColumn(name="unit", referencedColumnName="id")
     */
    protected $unit;

    /**
     *
     * @ORM\Column(type="decimal", precision=4, scale=2)         
     */
    protected $width;

    /**
     *
     * @ORM\Column(type="decimal", precision=4, scale=2)         
     */
    protected $height;

    /**
     *
     * @ORM\Column(type="decimal", precision=4, scale=2)         
     */
    protected $weight;

    /**
     *
     * @ORM\Column(type="decimal", precision=4, scale=2)         
     */
    protected $length;

    /**
     *
     * @ORM\Column(type="integer")        
     */
    protected $amount;

    /**
     * @ORM\ManyToOne(targetEntity="ProductBundle\Entity\NWeight")
     * @ORM\JoinColumn(name="nweight", referencedColumnName="id")
     */
    protected $nweight;

    /**
     * @ORM\ManyToOne(targetEntity="ProductBundle\Entity\NLength")
     * @ORM\JoinColumn(name="nlength", referencedColumnName="id")
     */
    protected $nlength;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    protected $deletedAt;

    public function setProduct(\ProductBundle\Entity\Product $param) {
        $this->product = $param;
    }

    public function getProduct() {
        return $this->product;
    }

    public function setCompany(\CompanyBundle\Entity\Company $param) {
        $this->company = $param;
    }

    public function getCompany() {
        return $this->company;
    }

    public function setCondition(\StockBundle\Entity\NCondition $condition) {
        $this->condition = $condition;
    }

    public function getCondition() {
        return $this->condition;
    }

    public function setStatus(\StockBundle\Entity\NStockStatus $param) {
        $this->status = $param;
    }

    public function getStatus() {
        return $this->status;
    }

    public function setSku($param) {
        $this->sku = $param;
    }

    public function getSku() {
        return $this->sku;
    }

    public function setAvailability($param) {
        $this->availability = $param;
    }

    public function getAvailability() {
        return $this->availability;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setPrice($param) {
        $this->price = $param;
    }

    public function getPrice() {
        return $this->price;
    }

    public function setUnit(\StockBundle\Entity\NUnit $unit) {
        $this->unit = $unit;
    }

    public function getUnit() {
        return $this->unit;
    }

    public function setWidth($width) {
        $this->width = $width;
    }

    public function getWidth() {
        return $this->width;
    }

    public function setHeight($height) {
        $this->height = $height;
    }

    public function getHeight() {
        return $this->height;
    }

    public function setWeight($weight) {
        $this->weight = $weight;
    }

    public function getWeight() {
        return $this->weight;
    }

    public function setLength($length) {
        $this->length = $length;
    }

    public function getLength() {
        return $this->length;
    }

    public function setAmount($amount) {
        $this->amount = $amount;
    }

    public function getAmount() {
        return $this->amount;
    }

    public function setCreated($created) {
        $this->created = $created;
    }

    public function getCreated() {
        return $this->created;
    }

    public function setModified($modified) {
        $this->modified = $modified;
    }

    public function getModified() {
        return $this->modified;
    }

    public function getDeletedAt() {
        return $this->deletedAt;
    }

    public function setDeletedAt($deletedAt) {
        $this->deletedAt = $deletedAt;
    }

    public function setNWeight(\ProductBundle\Entity\NWeight $nweight) {
        $this->nweight = $nweight;
    }

    public function getNWeight() {
        return $this->nweight;
    }

    public function setNLength(\ProductBundle\Entity\NLength $nlength) {
        $this->nlength = $nlength;
    }

    public function getNLength() {
        return $this->nlength;
    }

    public function __toString() {
        return $this->company . ' -- ' . $this->product;
    }

}

そして、保証テーブルのこのエンティティ:

<?php

namespace StockBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity
 * @ORM\Table(name="warranty")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class Warranty {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * 
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Warranty")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id")
     */
    protected $parent;

    /**
     * @ORM\ManyToOne(targetEntity="ProductBundle\Entity\Product")
     * @ORM\JoinColumn(name="product", referencedColumnName="upc")
     */
    protected $product;

    /**
     * @ORM\ManyToOne(targetEntity="StockBundle\Entity\NCondition")
     * @ORM\JoinColumn(name="kcondition", referencedColumnName="id")
     */
    protected $kcondition;

    /**
     * @ORM\ManyToOne(targetEntity="CompanyBundle\Entity\Company")
     * @ORM\JoinColumn(name="company", referencedColumnName="id")
     */
    protected $company;

    /**
     *
     * @ORM\Column(type="date")
     */
    protected $valid_time;

    /**
     *
     * @ORM\Column(type="text")
     */
    protected $description;

    /**
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created", type="datetime")
     */
    protected $created;

    /**
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="modified", type="datetime")
     */
    protected $modified;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    protected $deletedAt;

    /**
     * @ORM\OneToMany(targetEntity="StockBundle\Entity\KStock")
     */
    protected $stocks;

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

    public function setParent(Warranty $parent = null) {
        $this->parent = $parent;
    }

    public function getParent() {
        return $this->parent;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setCreated($created) {
        $this->created = $created;
    }

    public function getCreated() {
        return $this->created;
    }

    public function setModified($modified) {
        $this->modified = $modified;
    }

    public function getModified() {
        return $this->modified;
    }

    public function getDeletedAt() {
        return $this->deletedAt;
    }

    public function setDeletedAt($deletedAt) {
        $this->deletedAt = $deletedAt;
    }

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

    public function getProduct() {
        return $this->product;
    }

    public function setCompany(\CompanyBundle\Entity\Company $company) {
        $this->company = $company;
    }

    public function getCompany() {
        return $this->company;
    }

    public function setKCondition(\StockBundle\Entity\NCondition $condition) {
        $this->kcondition = $condition;
    }

    public function getKCondition() {
        return $this->kcondition;
    }

    public function setValidTime($valid_time) {
        $this->valid_time = $valid_time;
    }

    public function getValidTime() {
        return $this->valid_time;
    }

}

次に、次のKStockType.phpコードで作成します。

<?php

namespace StockBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use CatalogBundle\Form\KCatalogType;

class KStockType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder->add('sku', 'text', array('required' => true, 'label' => 'SKU (Número de Referencia)'))
                ->add('price', 'money', array('label' => 'Precio', 'currency' => 'VEF'))
                ->add('unit', 'entity', array('label' => 'Moneda', 'class' => 'StockBundle:NUnit', 'property' => 'name', 'required' => true, 'multiple' => false, 'expanded' => false))
                ->add('amount', 'integer', array('label' => 'Cantidad'))
                ->add('status', 'entity', array('label' => 'Estado', 'class' => 'StockBundle:NStockStatus', 'property' => 'name', 'required' => true, 'multiple' => false, 'expanded' => false))
                ->add('condition', 'entity', array('label' => 'Condición del producto', 'class' => 'ProductBundle:NCondition', 'property' => 'name', 'required' => true, 'multiple' => false, 'expanded' => false))
                ->add('width', 'integer', array('required' => true, 'label' => 'Ancho'))
                ->add('height', 'integer', array('required' => true, 'label' => 'Alto'))
                ->add('length', 'integer', array('required' => true, 'label' => 'Largo'))
                ->add('nlength', 'entity', array('label' => 'Unidad de Medida', 'class' => 'ProductBundle:NLength', 'property' => 'name', 'required' => true, 'multiple' => false, 'expanded' => false))
                ->add('weight', 'integer', array('required' => true, 'label' => 'Peso'))
                ->add('nweight', 'entity', array('label' => 'Unidad de Peso', 'class' => 'ProductBundle:NWeight', 'property' => 'name', 'required' => true, 'multiple' => false, 'expanded' => false))
                ->add('description', 'textarea', array('label' => 'Descripción'))
                ->add('start_date', 'date', array('label' => 'Fecha (Inicio de la Publicación)', 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', 'attr' => array('class' => 'dpicker')))
                ->add('warranty', 'textarea', array('required' => true, 'label' => 'Condiciones de Garantía'))
                ->add('valid_time', 'date', array('label' => 'Tiempo de Validez de la Garantía', 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', 'attr' => array('class' => 'dpicker')));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'StockBundle\Entity\KStock'
        ));
    }

    /**
     * @return string
     */
    public function getName() {
        return 'stockbundle_kstock';
    }

}

お気づきかもしれませんが、そのフォームには に属するいくつかのフィールドがありますwarranty。次に、コントローラーには、編集アクション用の次のメソッドがあります。

/**
 * Stock
 * 
 * @Route("/edit/{company_id}/{product_id}", name="stock_edit")
 * @Method("GET")
 */
public function editAction($company_id, $product_id) {
    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('StockBundle:KStock')->findOneBy(array('company' => $company_id, 'product' => $product_id));

    $response = array();
    $response["response"] = true;

    if (!$entity) {
        $response['response'] = FALSE;
        $response['message'] = 'Unable to find Stock entity.';
        return new JsonResponse($response);
    }

    $product = $em->getRepository('ProductBundle:Product')->find($product_id);
    $company = $em->getRepository('CompanyBundle:Company')->find($company_id);

    if (!$product || !$company) {
        $response['response'] = FALSE;
        $response['message'] = 'Error not found product or company';
        return new JsonResponse($response);
    }

    $editForm = $this->createForm(new KStockType(), $entity);
    return $this->render("StockBundle:Stock:edit.html.twig", array('entity' => $entity, 'edit_form' => $editForm->createView()));
}

しかし、それを呼び出すと、次のエラーが発生しました。

プロパティ "start_date" も、メソッド "getStartDate()"、"isStartDate()"、"hasStartDate()"、"_ get()"、または " _call()" のいずれも存在せず、クラス "StockBundle" にパブリック アクセスがありません。 \Entity\KStock".

私は何が欠けていますか?

4

2 に答える 2

1

このページを見てください: Symfony のドキュメント

それから少し簡単に:

In more complex examples, you can embed entire forms, which is useful when 
creating forms that expose one-to-many relationships  

2 つのアプローチが考えられると思います:
1 - リレーショナル エンティティを使用してフォームを作成する方法を確認する
2 - クラスを使用せずにフォームを作成し、コントローラーですべての情報を管理する (私はこれを使用しません)

于 2013-09-03T14:15:27.893 に答える
0

フォームの start_date に関する行を次のように置き換えてみてください。

->add('start_date', 'date', array('label' => 'Fecha (Inicio de la Publicación)', 'widget' => 'single_text', 'format' => 'yyyy-MM-dd', 'attr' => array('class' => 'dpicker'), 'mapped' => false))
于 2013-09-03T14:21:44.823 に答える