多对多关系"文章"对于带有键的数组" 0,1和#34;在第35行的:: FrontEnd \ devis.html.twig中不存在

时间:2016-07-14 20:07:29

标签: php symfony orm doctrine-orm twig

我正在处理一个项目,我在ManyToMany协会中被阻止。我有一个名为Devis的实体(在引用中引用)和一个名为articles的实体。问题是,当我想要显示特定报价的文章时,它不起作用。

这是Devis实体:

<?php

namespace DevisBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Devis
 *
 * @ORM\Table(name="devis")
 * @ORM\Entity(repositoryClass="DevisBundle\Repository\DevisRepository")
 */
class Devis {

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

    //les relations
    /**
     * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="devis_user", cascade={"persist"})
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    /**
     * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="devis_enterprise")
     * @ORM\JoinColumn(name="enterprise_id", referencedColumnName="id")
     */
    private $enterprise;

    /**
     * @ORM\ManyToMany(targetEntity="ArticleBundle\Entity\Article", mappedBy="devis")
     */
    private $articles;


    public function __construct() {
        $this->articles = new \Doctrine\Common\Collections\ArrayCollection();
    }


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



    /**
     * Set user
     *
     * @param \UserBundle\Entity\User $user
     * @return Devis
     */
    public function setUser(\UserBundle\Entity\User $user = null) {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \UserBundle\Entity\User 
     */
    public function getUser() {
        return $this->user;
    }

    /**
     * Set enterprise
     *
     * @param \UserBundle\Entity\User $enterprise
     * @return Devis
     */
    public function setEnterprise(\UserBundle\Entity\User $enterprise = null) {
        $this->enterprise = $enterprise;

        return $this;
    }

    /**
     * Get enterprise
     *
     * @return \UserBundle\Entity\User 
     */
    public function getEnterprise() {
        return $this->enterprise;
    }

    /**
     * Add articles
     *
     * @param \ArticleBundle\Entity\Article $articles
     * @return Devis
     */
    public function addArticle(\ArticleBundle\Entity\Article $article) {
        $article->addDevi($this);
        $this->articles[] = $article;

        return $this;
    }

    /**
     * Remove articles
     *
     * @param \ArticleBundle\Entity\Article $articles
     */
    public function removeArticle(\ArticleBundle\Entity\Article $articles) {
        $this->articles->removeElement($articles);
    }

    /**
     * Get articles
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getArticles() {
        return $this->articles;
    }



}

文章实体

<?php

namespace ArticleBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Article
 *
 * @ORM\Table(name="article")
 * @ORM\Entity(repositoryClass="ArticleBundle\Repository\ArticleRepository")
 */
class Article
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

    //les relations

    /**
     * @ORM\ManyToMany(targetEntity="DevisBundle\Entity\Devis", inversedBy="articles")
     * @ORM\JoinTable(name="articles_devis")
     */
    private $devis;


    /**
     * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="articles")
     * @ORM\JoinColumn(name="utilisateur_id", referencedColumnName="id")
     */
    private $entreprise;

    /**
     * @ORM\ManyToOne(targetEntity="CategoryBundle\Entity\Category", inversedBy="articles")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;


    public function __construct() {
        $this->devis = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

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

        return $this;
    }

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

    /**
     * Set description
     *
     * @param string $description
     * @return Article
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

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

    /**
     * Set prix
     *
     * @param string $prix
     * @return Article
     */
    public function setPrix($prix)
    {
        $this->prix = $prix;

        return $this;
    }

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

    /**
     * Set entreprise
     *
     * @param \UserBundle\Entity\User $entreprise
     * @return Article
     */
    public function setEntreprise(\UserBundle\Entity\User $entreprise = null)
    {
        $this->entreprise = $entreprise;

        return $this;
    }

    /**
     * Get entreprise
     * 
     * @return \UserBundle\Entity\User 
     */
    public function getEntreprise()
    {
        return $this->entreprise;
    }

    /**
     * Set category
     *
     * @param \CategoryBundle\Entity\Category $category
     * @return Article
     */
    public function setCategory(\CategoryBundle\Entity\Category $category = null)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * Get category
     *
     * @return \CategoryBundle\Entity\Category 
     */
    public function getCategory()
    {
        return $this->category;
    }



    /**
     * Add devis
     *
     * @param \DevisBundle\Entity\Devis $devis
     * @return Article
     */
    public function addDevi(\DevisBundle\Entity\Devis $devis)
    {
        $devis->addArticle($this);
        $this->devis[] = $devis;

        return $this;
    }

    /**
     * Remove devis
     *
     * @param \DevisBundle\Entity\Devis $devis
     */
    public function removeDevi(\DevisBundle\Entity\Devis $devis)
    {
        $this->devis->removeElement($devis);
    }

    /**
     * Get devis
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getDevis()
    {
        return $this->devis;
    }
}

控制器

<?php

namespace DevisBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\RedirectResponse;
use DevisBundle\Entity\Devis;
use UserBundle\Entity\User;
class DevisController extends Controller {
    /**
     * 
     * @Route("/devis/{user_id}", name="devis")
     * @Method("GET")
     */
    public function devisAction($user_id) {
        $em = $this->getDoctrine()->getManager();
        $entreprise = new User();
        $mes_devis = new Devis();
        $enterprises = new \Doctrine\Common\Collections\ArrayCollection();

        $user = $this->get('security.context')->getToken()->getUser();
        $mes_devis = $em->getRepository('DevisBundle:Devis')->devisByUser($user_id);

        $categories = $em->getRepository('CategoryBundle:Category')->findAll();

        return $this->render('::FrontEnd/devis.html.twig', array(
                    'user' => $user,
                    'categories' => $categories,
                    'mes_devis' => $mes_devis,
        ));
    }
}

Devis.html.twig

<div class="cart_main">
        <h2>Mes devis ({{ mes_devis|length }})</h2>
        {% for devis in  mes_devis %}
            <div class="container">

                <div class="cart-items">
                    <h4>Entreprise : {{ devis.enterprise }}</h4>
                    <script>$(document).ready(function (c) {
                            $('.close1').on('click', function (c) {
                                $('.cart-header').fadeOut('slow', function (c) {
                                    $('.cart-header').remove();
                                });
                            });
                        });
                    </script>
                    {% for article in  mes_devis.articles %}
                        <div class="cart-header">
                            <div class="close1"> </div>
                            <div class="cart-sec">
                                <div class="cart-item cyc">
                                    <img src="{{ asset ('bundles/theme_front_end/images/ts4.jpg') }}"/>
                                </div>
                                <div class="cart-item-info">
                                    <h3>Mast & Harbour<span>Model No: 3578</span></h3>
                                    <h4><span>Rs. $ </span>150.00</h4>
                                    <p class="qty">Qty ::</p>
                                    <input min="1" type="number" id="quantity" name="quantity" value="1" class="form-control input-small">
                                </div>
                                <div class="clearfix"></div>
                                <div class="delivery">
                                    <p>Service Charges:: Rs.50.00</p>                            
                                </div>                      
                            </div>
                        </div>
                        <script>$(document).ready(function (c) {
                                $('.close2').on('click', function (c) {
                                    $('.cart-header2').fadeOut('slow', function (c) {
                                        $('.cart-header2').remove();
                                    });
                                });
                            });
                        </script>
                    {% endfor %}    
                </div>

                <div class="cart-total">
                    <a class="continue" href="#">Continue to basket</a>
                    <div class="price-details">
                        <h3>Price Details</h3>
                        <span>Total</span>
                        <span class="total">350.00</span>
                        <span>Discount</span>
                        <span class="total">---</span>
                        <span>Delivery Charges</span>
                        <span class="total">100.00</span>
                        <div class="clearfix"></div>                 
                    </div>  
                    <h4 class="last-price">TOTAL</h4>
                    <span class="total final">450.00</span>
                    <div class="clearfix"></div>
                    <a class="order" href="#">Place Order</a>
                    <div class="total-item">
                        <h3>OPTIONS</h3>
                        <h4>COUPONS</h4>
                        <a class="cpns" href="#">Apply Coupons</a>
                        <p><a href="#">Log In</a> to use accounts - linked coupons</p>
                    </div>
                </div>

            </div>
            <div class="clearfix"></div>
        {% endfor %}
    </div>

,错误是:

Key "articles" for array with keys "0, 1" does not exist in ::FrontEnd\devis.html.twig at line 35 

当我var_dump($mes_devis)时,我得到了

array (size=2)
  0 => 
    object(DevisBundle\Entity\Devis)[1024]
      private 'id' => int 7
      private 'isConfirmed' => boolean false
      private 'articleadded' => null
      private 'user' => 
        object(UserBundle\Entity\User)[856]
          protected 'id' => int 5
          private 'nom' => string 'kamel' (length=5)
          private 'prenom' => string 'bouslama' (length=8)
          private 'date_naissance' => string '14/10/1992' (length=10)
          private 'sexe' => string 'Homme' (length=5)
          private 'mobile' => string '50966301' (length=8)
          private 'ville' => string 'monastir' (length=8)
          private 'site_web' => string 'www.rochdi.com' (length=14)
          private 'logo_entreprise' => null
          private 'raison_social_entreprise' => null
          private 'denomination_entreprise' => null
          private 'slogan_entreprise' => null
          private 'presentation_entreprise' => null
          private 'code_postal_entreprise' => null
          private 'longitude_entreprise' => null
          private 'laltitude_entreprise' => null
          private 'registre_commerce_entreprise' => null
          private 'date_creation_entreprise' => null
          private 'effectif_entreprise' => null
          private 'capital_social_entreprise' => null
          private 'fax_entreprise' => null
          private 'pack_commercial_entreprise' => null
          private 'nom_entreprise' => null
          private 'adresse' => string 'Adresse' (length=7)
          private 'articles' => 
            object(Doctrine\ORM\PersistentCollection)[881]
              ...
          private 'devis_user' => 
            object(Doctrine\ORM\PersistentCollection)[904]
              ...
          private 'devis_enterprise' => 
            object(Doctrine\ORM\PersistentCollection)[906]
              ...
          protected 'username' => string 'kamel' (length=5)
          protected 'usernameCanonical' => string 'kamel' (length=5)
          protected 'email' => string 'kamel@gmail.com' (length=15)
          protected 'emailCanonical' => string 'kamel@gmail.com' (length=15)
          protected 'enabled' => boolean true
          protected 'salt' => string 'koikc9294lws8wgwgo8ss4owg84swg8' (length=31)
          protected 'password' => string '$2y$13$koikc9294lws8wgwgo8ssu.9zbRplUObbvJEIyuVZvbE9VnateEOi' (length=60)
          protected 'plainPassword' => null
          protected 'lastLogin' => 
            object(DateTime)[854]
              ...
          protected 'confirmationToken' => null
          protected 'passwordRequestedAt' => null
          protected 'groups' => null
          protected 'locked' => boolean false
          protected 'expired' => boolean false
          protected 'expiresAt' => null
          protected 'roles' => 
            array (size=1)
              ...
          protected 'credentialsExpired' => boolean false
          protected 'credentialsExpireAt' => null
      private 'enterprise' => 
        object(Proxies\__CG__\UserBundle\Entity\User)[1122]
          public '__initializer__' => 
            object(Closure)[1023]
              ...
          public '__cloner__' => 
            object(Closure)[1022]
              ...
          public '__isInitialized__' => boolean false
          protected 'id' => string '6' (length=1)
          private 'nom' (UserBundle\Entity\User) => null
          private 'prenom' (UserBundle\Entity\User) => null
          private 'date_naissance' (UserBundle\Entity\User) => null
          private 'sexe' (UserBundle\Entity\User) => null
          private 'mobile' (UserBundle\Entity\User) => null
          private 'ville' (UserBundle\Entity\User) => null
          private 'site_web' (UserBundle\Entity\User) => null
          private 'logo_entreprise' (UserBundle\Entity\User) => null
          private 'raison_social_entreprise' (UserBundle\Entity\User) => null
          private 'denomination_entreprise' (UserBundle\Entity\User) => null
          private 'slogan_entreprise' (UserBundle\Entity\User) => null
          private 'presentation_entreprise' (UserBundle\Entity\User) => null
          private 'code_postal_entreprise' (UserBundle\Entity\User) => null
          private 'longitude_entreprise' (UserBundle\Entity\User) => null
          private 'laltitude_entreprise' (UserBundle\Entity\User) => null
          private 'registre_commerce_entreprise' (UserBundle\Entity\User) => null
          private 'date_creation_entreprise' (UserBundle\Entity\User) => null
          private 'effectif_entreprise' (UserBundle\Entity\User) => null
          private 'capital_social_entreprise' (UserBundle\Entity\User) => null
          private 'fax_entreprise' (UserBundle\Entity\User) => null
          private 'pack_commercial_entreprise' (UserBundle\Entity\User) => null
          private 'nom_entreprise' (UserBundle\Entity\User) => null
          private 'adresse' (UserBundle\Entity\User) => null
          private 'articles' (UserBundle\Entity\User) => null
          private 'devis_user' (UserBundle\Entity\User) => null
          private 'devis_enterprise' (UserBundle\Entity\User) => null
          protected 'username' => null
          protected 'usernameCanonical' => null
          protected 'email' => null
          protected 'emailCanonical' => null
          protected 'enabled' => null
          protected 'salt' => null
          protected 'password' => null
          protected 'plainPassword' => null
          protected 'lastLogin' => null
          protected 'confirmationToken' => null
          protected 'passwordRequestedAt' => null
          protected 'groups' => null
          protected 'locked' => null
          protected 'expired' => null
          protected 'expiresAt' => null
          protected 'roles' => null
          protected 'credentialsExpired' => null
          protected 'credentialsExpireAt' => null
      private 'articles' => 
        object(Doctrine\ORM\PersistentCollection)[1121]
          private 'snapshot' => 
            array (size=0)
              ...
          private 'owner' => 
            &object(DevisBundle\Entity\Devis)[1024]
          private 'association' => 
            array (size=16)
              ...
          private 'em' => 
            object(Doctrine\ORM\EntityManager)[686]
              ...
          private 'backRefFieldName' => string 'devis' (length=5)
          private 'typeClass' => 
            object(Doctrine\ORM\Mapping\ClassMetadata)[857]
              ...
          private 'isDirty' => boolean false
          private 'initialized' => boolean false
          private 'coll' => 
            object(Doctrine\Common\Collections\ArrayCollection)[1120]
              ...
  1 => 
    object(DevisBundle\Entity\Devis)[1119]
      private 'id' => int 8
      private 'isConfirmed' => boolean false
      private 'articleadded' => null
      private 'user' => 
        object(UserBundle\Entity\User)[856]
          protected 'id' => int 5
          private 'nom' => string 'kamel' (length=5)
          private 'prenom' => string 'bouslama' (length=8)
          private 'date_naissance' => string '14/10/1992' (length=10)
          private 'sexe' => string 'Homme' (length=5)
          private 'mobile' => string '50966301' (length=8)
          private 'ville' => string 'monastir' (length=8)
          private 'site_web' => string 'www.rochdi.com' (length=14)
          private 'logo_entreprise' => null
          private 'raison_social_entreprise' => null
          private 'denomination_entreprise' => null
          private 'slogan_entreprise' => null
          private 'presentation_entreprise' => null
          private 'code_postal_entreprise' => null
          private 'longitude_entreprise' => null
          private 'laltitude_entreprise' => null
          private 'registre_commerce_entreprise' => null
          private 'date_creation_entreprise' => null
          private 'effectif_entreprise' => null
          private 'capital_social_entreprise' => null
          private 'fax_entreprise' => null
          private 'pack_commercial_entreprise' => null
          private 'nom_entreprise' => null
          private 'adresse' => string 'Adresse' (length=7)
          private 'articles' => 
            object(Doctrine\ORM\PersistentCollection)[881]
              ...
          private 'devis_user' => 
            object(Doctrine\ORM\PersistentCollection)[904]
              ...
          private 'devis_enterprise' => 
            object(Doctrine\ORM\PersistentCollection)[906]
              ...
          protected 'username' => string 'kamel' (length=5)
          protected 'usernameCanonical' => string 'kamel' (length=5)
          protected 'email' => string 'kamel@gmail.com' (length=15)
          protected 'emailCanonical' => string 'kamel@gmail.com' (length=15)
          protected 'enabled' => boolean true
          protected 'salt' => string 'koikc9294lws8wgwgo8ss4owg84swg8' (length=31)
          protected 'password' => string '$2y$13$koikc9294lws8wgwgo8ssu.9zbRplUObbvJEIyuVZvbE9VnateEOi' (length=60)
          protected 'plainPassword' => null
          protected 'lastLogin' => 
            object(DateTime)[854]
              ...
          protected 'confirmationToken' => null
          protected 'passwordRequestedAt' => null
          protected 'groups' => null
          protected 'locked' => boolean false
          protected 'expired' => boolean false
          protected 'expiresAt' => null
          protected 'roles' => 
            array (size=1)
              ...
          protected 'credentialsExpired' => boolean false
          protected 'credentialsExpireAt' => null
      private 'enterprise' => 
        object(Proxies\__CG__\UserBundle\Entity\User)[1118]
          public '__initializer__' => 
            object(Closure)[1023]
              ...
          public '__cloner__' => 
            object(Closure)[1022]
              ...
          public '__isInitialized__' => boolean false
          protected 'id' => string '8' (length=1)
          private 'nom' (UserBundle\Entity\User) => null
          private 'prenom' (UserBundle\Entity\User) => null
          private 'date_naissance' (UserBundle\Entity\User) => null
          private 'sexe' (UserBundle\Entity\User) => null
          private 'mobile' (UserBundle\Entity\User) => null
          private 'ville' (UserBundle\Entity\User) => null
          private 'site_web' (UserBundle\Entity\User) => null
          private 'logo_entreprise' (UserBundle\Entity\User) => null
          private 'raison_social_entreprise' (UserBundle\Entity\User) => null
          private 'denomination_entreprise' (UserBundle\Entity\User) => null
          private 'slogan_entreprise' (UserBundle\Entity\User) => null
          private 'presentation_entreprise' (UserBundle\Entity\User) => null
          private 'code_postal_entreprise' (UserBundle\Entity\User) => null
          private 'longitude_entreprise' (UserBundle\Entity\User) => null
          private 'laltitude_entreprise' (UserBundle\Entity\User) => null
          private 'registre_commerce_entreprise' (UserBundle\Entity\User) => null
          private 'date_creation_entreprise' (UserBundle\Entity\User) => null
          private 'effectif_entreprise' (UserBundle\Entity\User) => null
          private 'capital_social_entreprise' (UserBundle\Entity\User) => null
          private 'fax_entreprise' (UserBundle\Entity\User) => null
          private 'pack_commercial_entreprise' (UserBundle\Entity\User) => null
          private 'nom_entreprise' (UserBundle\Entity\User) => null
          private 'adresse' (UserBundle\Entity\User) => null
          private 'articles' (UserBundle\Entity\User) => null
          private 'devis_user' (UserBundle\Entity\User) => null
          private 'devis_enterprise' (UserBundle\Entity\User) => null
          protected 'username' => null
          protected 'usernameCanonical' => null
          protected 'email' => null
          protected 'emailCanonical' => null
          protected 'enabled' => null
          protected 'salt' => null
          protected 'password' => null
          protected 'plainPassword' => null
          protected 'lastLogin' => null
          protected 'confirmationToken' => null
          protected 'passwordRequestedAt' => null
          protected 'groups' => null
          protected 'locked' => null
          protected 'expired' => null
          protected 'expiresAt' => null
          protected 'roles' => null
          protected 'credentialsExpired' => null
          protected 'credentialsExpireAt' => null
      private 'articles' => 
        object(Doctrine\ORM\PersistentCollection)[1117]
          private 'snapshot' => 
            array (size=0)
              ...
          private 'owner' => 
            &object(DevisBundle\Entity\Devis)[1119]
          private 'association' => 
            array (size=16)
              ...
          private 'em' => 
            object(Doctrine\ORM\EntityManager)[686]
              ...
          private 'backRefFieldName' => string 'devis' (length=5)
          private 'typeClass' => 
            object(Doctrine\ORM\Mapping\ClassMetadata)[857]
              ...
          private 'isDirty' => boolean false
          private 'initialized' => boolean false
          private 'coll' => 
            object(Doctrine\Common\Collections\ArrayCollection)[1116]
              ...

1 个答案:

答案 0 :(得分:1)

你在第二次循环中犯了一个错误,它不是 mes_devis.articles ,而是 devis.articles

{% for article in  devis.articles %}
    <div class="cart-header">
        <div class="close1"> </div>
        <div class="cart-sec">
            <div class="cart-item cyc">
                <img src="{{ asset ('bundles/theme_front_end/images/ts4.jpg') }}"/>
            </div>
            <div class="cart-item-info">
                <h3>Mast & Harbour<span>Model No: 3578</span></h3>
                <h4><span>Rs. $ </span>150.00</h4>
                <p class="qty">Qty ::</p>
                <input min="1" type="number" id="quantity" name="quantity" value="1" class="form-control input-small">
            </div>
            <div class="clearfix"></div>
            <div class="delivery">
                <p>Service Charges:: Rs.50.00</p>                            
            </div>                      
        </div>
    </div>
    <script>$(document).ready(function (c) {
        $('.close2').on('click', function (c) {
            $('.cart-header2').fadeOut('slow', function (c) {
                $('.cart-header2').remove();
            });
        });
    });
    </script>
{% endfor %}