【问题标题】:Twig upload data from entity in different bundleTwig 从不同包中的实体上传数据
【发布时间】:2017-02-19 03:36:27
【问题描述】:

我想获取实体在不同包中的数据:

MainBundle 中的 Twig:

     {% extends 'MainBundle:eshop:main.html.twig' %}

     {% set products = getClassVars(Product) %}

    {% block body %}


    <div class="container main-container">

    <!-- Main component call to action -->


    <div id="productslider" class="owl-carousel owl-theme">

        <?php $em= $this->getDoctrine()->getManager();?>
        <?php    $products= $em
        ->getRepository('ProductBundle:Product')->findAll(); ?>
        {%
        for product in  products %}
            {% if date(product.createdDate )> date('-1days') %}


                <div class="item">
                    <div class="product">
                        <a class="add-fav tooltipHere" data-toggle="tooltip"
                           data-original-title="Add to Wishlist"
                           data-placement="left">

                            <i class="glyphicon glyphicon-heart"></i>
                        </a>

                        <div class="image">
                            <div class="quickview">
                                <a data-toggle="modal" class="btn btn-xs btn-quickview" href="ajax/product.html"
                                   data-target="#productSetailsModalAjax">Quick View </a>
                            </div>
                            <a href="{{ asset('app_dev.php/product/')~product.id~"/show" }}"><img
                                        class="img-responsive" alt="img"
                                        src="{{ asset('bundles/imageproducts/')~ product.imageName }}"
                                        height="250" width="200"></a>

                            <div class="promotion"><span class="discount">15% OFF</span></div>
                        </div>
                        <div class="description">
                            <h4>
                                <a href="{{ asset('app_dev.php/product/')~product.id~"/show" }}">{{ product.libelle }} </a>
                            </h4>

                            <div class="grid-description">
                                <p>{{ product.libelle }}  </p>
                            </div>
                            <div class="list-description">
                                <p> {{ product.description }}</p>
                            </div>
                            <span class="size">XL / XXL / S </span></div>
                        <div class="price"><span>$25</span></div>
                        <div class="action-control"><a class="btn btn-primary"> <span class="add2cart"><i
                                            class="glyphicon glyphicon-shopping-cart"> </i> Add to cart </span>
                            </a></div>
                    </div>
                </div>

            {% endif %}
        {% endfor %}
    </div>
</div>
</div>
{% endblock %}

产品实体:

<?php

 namespace ProductBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
 use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
 use ProductBundle\Repository\ProductRepository;

/**
 * Produit
 *     @ORM\Entity(repositoryClass="\ProductBundle\Repository\ProductRepository")
  * @Vich\Uploadable
 * @ORM\Table(name="produit")
  */
  class Product
  {
   /**
   * @ORM\GeneratedValue
   * @ORM\Id
   * @ORM\Column(type="integer")
   */
   private $id;

未知的“getClassVars”函数。

我该怎么办?

【问题讨论】:

    标签: php database symfony doctrine twig


    【解决方案1】:

    就像我说的 100% 注意力不集中 如果有人需要,我会发布答案,所以我们有:

    MainBundle 希望有用户

    ProductBundle 希望有产品

     <?php
    
     namespace MainBundle\Controller;
    
     use ProductBundle\Repository\ProductRepository;
     use ProductBundle\Entity\Product;
    
     use Symfony\Bundle\FrameworkBundle\Controller\Controller;
     use Symfony\Component\HttpFoundation\Request;
    
    /**
     * User controller.
     *
     */
     class UserController extends Controller
     {
    
    public function indexAction()
    {
    
        $em= $this->getDoctrine()->getManager();
        $products= $em->getRepository('ProductBundle:Product')->findAll();
    
    
    
        return $this->render('admin/index.html.twig', array(
            'products' => $products,
    
    
        ));
    
      }}
    

    【讨论】:

      【解决方案2】:

      你可以使用

      $em->createQueryBuilder()
         ->select('Product')->from('ABCBundle:Product', 'Product')
         ->leftJoin('XYZBundle:Category', 'Category', 'Category.productID = Product.id')
         ->getQuery()->getResult();
      

      只是加入

      Product.yml

      oneToOne:
          description:
              targetEntity: Description
              mappedBy: product
              cascade: ["persist", "remove"]
      

      描述.yml

      oneToOne:
          product:
              targetEntity: Product
              mappedBy: description
              joinColumn:
                  name: product_id
                  referencedColumn: id    
      

      还有 如果您已经为应该轻松返回的实体创建了关系。

      【讨论】:

      • 我在用户控制器中的控制器中进行了更改,以加载不同捆绑中的产品我不需要添加任何注释非常感谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      相关资源
      最近更新 更多