douxia5179 2013-03-28 02:04
浏览 60
已采纳

php和twig语法

I have the following code to extract the username of an owner object. The Owner object is essentially the owner of the shop. Here's how it's defined in the Shop class.

 /**
     * 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;
    }
    /**

As you can see the owner returns a User object. However when I do .username to the User class I always get undefined. Why is this?

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

When I do the following:

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

it prints out just fine.

Here's some more code on the Shop:

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;
    }

}

Some code of fetching the data values:

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);
        }
    }   
  • 写回答

3条回答 默认 最新

  • dr5779 2013-03-28 03:07
    关注

    I am guessing it is because their is not data for the relationship. It looks like you probably have an item that you are calling a method on to get another object and then calling another method on that object to get yet another object. Since the first call is a proxy object it probably doesn't load the second proxy object data with it. If you look at the data in the data outside the template like in the controller I am guessing it isn't defined there. you might want to make sure that the data is loaded prior to getting to the view. I think that the root of the problem is the initial relationship.

    To help you might post some of your controller code where you are getting fetching the data or setting the values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM