2013-03-28 145 views
0

我有以下代码来提取所有者对象的用户名。所有者对象本质上是商店的所有者。以下是Shop类中定义的方式。php和树枝语法

/** 
    * Set owner 
    * 
    * @param User $owner 
    * @return Shop 
    */ 
    public function setOwner(User $owner = null) 
    { 
     $this->owner = $owner; 

     return $this; 
    } 

    /** 
    * Get owner 
    * 
    * @return User 
    */ 
    public function getOwner() 
    { 
     return $this->owner; 
    } 
    /** 

正如你可以看到所有者返回一个用户对象。但是,当我做.username到User类时,我总是得到未定义的。为什么是这样?

"<%= '/profile/'+item.get('shop').owner.username %>"> 

当我做到以下几点:

"<%= '/profile/'+item.get('shop').name %>"> 

它打印出来就好了。

下面是对网店更多的代码:

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

    /** 
    * @Exclude() 
    * @ORM\Column(name="isVisible", type="boolean") 
    */ 
    private $isVisible = false; 

    /** 
    * @ORM\OneToOne(targetEntity="Shopious\MainBundle\Entity\ShopLogo", mappedBy="shop", cascade={"persist","remove"}) 
    */ 
    protected $shoplogo; 

    /** 
    * @Assert\NotBlank(message="Shope name should not be blank") 
    * @ORM\Column(name="name", type="string", length=100, unique=true) 
    */ 
    protected $name; 

    /** 
    * Assert\NotBlank(message="Description should not be blank") 
    * @ORM\Column(name="description", type="string", length=350, nullable=true) 
    */ 
    protected $description = ""; 


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

    /** 
    * @ORM\OneToMany(targetEntity="Shopious\MainBundle\Entity\Product", mappedBy="shop", cascade={"remove","persist"}) 
    */ 
    protected $products; 


    /** 
    * @Exclude() 
    * @ORM\OneToOne(targetEntity="Shopious\UserBundle\Entity\User", inversedBy="shop") 
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE", nullable=true) 
    */ 
    protected $owner; 

    /** 
    * @Exclude() 
    * @ORM\OneToOne(targetEntity="Shopious\MainBundle\Entity\PaymentInfo", inversedBy="shop", cascade={"persist","remove"}) 
    * @ORM\JoinColumn(name="paymentInfo_id", referencedColumnName="id" , onDelete="CASCADE") 
    */ 
    protected $paymentInfo; 

    /** 
    * 
    * @ORM\OneToMany(targetEntity="Shopious\MainBundle\Entity\ShopTestimonial", mappedBy="shop", cascade={"persist","remove"}) 
    */ 
    protected $testimonials; 

    public function __toString() 
    { 
     return $this->shopname; 
    } 

    /** 
    * @ORM\PrePersist 
    */ 
    public function initialisation() 
    { 
     $this->shoplogo = new ShopLogo(); 
     $this->shoplogo->setShop($this); 

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

    /** 
    * Set owner 
    * 
    * @param User $owner 
    * @return Shop 
    */ 
    public function setOwner(User $owner = null) 
    { 
     $this->owner = $owner; 

     return $this; 
    } 

    /** 
    * Get owner 
    * 
    * @return User 
    */ 
    public function getOwner() 
    { 
     return $this->owner; 
    } 
    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->products = new ArrayCollection(); 
    } 

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

     //return $this; 
    } 

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

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

     return $this; 
    } 

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


    /** 
    * Add products 
    * 
    * @param Product $products 
    * @return Shop 
    */ 
    public function addProduct(Product $products) 
    { 
     $this->products[] = $products; 

     return $this; 
    } 

    /** 
    * Remove products 
    * 
    * @param Product $products 
    */ 
    public function removeProduct(Product $products) 
    { 
     $this->products->removeElement($products); 
    } 

    /** 
    * Get products 
    * 
    * @return Collection 
    */ 
    public function getProducts() 
    { 
     return $this->products; 
    } 

    public function getPaymentInfo() 
    { 
     return $this->paymentInfo; 
    } 

    public function setPaymentInfo(\Shopious\MainBundle\Entity\PaymentInfo $paymentInfo) 
    { 
     $this->paymentInfo = $paymentInfo; 
    } 


    /** 
    * Set tags 
    * 
    * @param string $tags 
    * @return Shop 
    */ 
    public function setTags($tags) 
    { 
     $this->tags = $tags; 

     return $this; 
    } 

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

    /** 
    * Set shoplogo 
    * 
    * @param Shopious\MainBundle\Entity\ShopLogo $shoplogo 
    * @return Shop 
    */ 
    public function setShoplogo(\Shopious\MainBundle\Entity\ShopLogo $shoplogo = null) 
    { 
     $this->shoplogo = $shoplogo; 

     return $this; 
    } 

    /** 
    * Get shoplogo 
    * 
    * @return Shopious\MainBundle\Entity\ShopLogo 
    */ 
    public function getShoplogo() 
    { 
     return $this->shoplogo; 
    } 

    /** 
    * Add testimonials 
    * 
    * @param \Shopious\MainBundle\Entity\ShopTestimonial $testimonials 
    * @return Shop 
    */ 
    public function addTestimonial(\Shopious\MainBundle\Entity\ShopTestimonial $testimonials) 
    { 
     $this->testimonials[] = $testimonials; 

     return $this; 
    } 

    /** 
    * Remove testimonials 
    * 
    * @param \Shopious\MainBundle\Entity\ShopTestimonial $testimonials 
    */ 
    public function removeTestimonial(\Shopious\MainBundle\Entity\ShopTestimonial $testimonials) 
    { 
     $this->testimonials->removeElement($testimonials); 
    } 

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

    /** 
    * Set isVisible 
    * 
    * @param boolean $isVisible 
    * @return Product 
    */ 
    public function setIsVisible($isVisible) 
    { 
     $this->isVisible = $isVisible; 

     return $this; 
    } 

    /** 
    * Get isVisible 
    * 
    * @return boolean 
    */ 
    public function getIsVisible() 
    { 
     return $this->isVisible; 
    } 

} 

获取数据值的一些代码:

public function getItemsAction() 
    { 
     try{ 
      $query = $this->getRequest()->query; 
      $em = $this->getDoctrine()->getEntityManager(); 
      $items = $em->getRepository('ShopiousMainBundle:Product')->filterBy(
        $query->get('category'), 
        $query->get('tag'), 
        $query->get('size'), 
        $query->get('price'), 
        $query->get('shop'), 
        $query->get('sort'), 
        $query->get('page') 
       ); 

      return $this->Json($items); 
     }catch(\Exception $ex){ 
      return $this->Json($ex->getMessage(),false); 
     } 
    } 
+1

给对象的更多细节(给定类不完整) – 2013-03-28 02:07:09

+0

@IOIOMAD你的意思是Shop类?即:所有者对象?复制粘贴 – adit 2013-03-28 02:15:16

回答

0

我猜测这是因为他们不是关系的数据。它看起来像你可能有一个项目,你正在调用一个方法来获得另一个对象,然后调用该对象上的另一个方法来获得另一个对象。由于第一次调用是代理对象,因此它可能不会加载第二个代理对象数据。如果你看看模板之外的数据中的数据,比如在控制器中,我猜测它没有在那里定义。您可能需要确保在进入视图之前加载数据。我认为问题的根源在于最初的关系。

为了帮助您发布一些控制器代码,您可以在其中获取数据或设置值。

+0

添加了控制器代码来获取项目 – adit 2013-03-28 03:50:48

0

听起来像类所有者的名字存储到变量“名”,而不是“用户名“

+0

以上所以item.get('店')。所有者应该返回给你一个用户对象,并在用户对象内有用户名 – adit 2013-03-28 02:17:43

+0

有一个错字的代码..请重新检查 – adit 2013-03-28 02:19:23

0

我觉得protected $username;多年平均值只存在protected $name;

如果你想

“<%=“/profile/'+item.get('shop').owner.username%>”> 然后更改

它添加到

你先级

protected $username[]; 


public function addUsers(User $user = null) 
{ 
    array_push($this->username, $user); 

    return $this->username; 
} 


public function getUsers() 
{ 
    return $this->username; 
} 

或单个用户

protected $username; 

public function addUsers(User $user = null) 
{ 
    $this->username= $user; 

    return $this->username; 
} 


public function getUsers() 
{ 
    return $this->username; 
} 
+0

我改变了问题,什么作品是'/profile/'+item.get('shop').name – adit 2013-03-28 02:21:59

+0

ya $ username是未定义的调用$ name是为.owner.name定义的。请阅读我的答案伙伴! – 2013-03-28 02:24:09

+0

我想要店铺的用户名,而不是店名。再次重读我的问题,我编辑它 – adit 2013-03-28 02:25:21