0

所有者オブジェクトのユーザー名を抽出する次のコードがあります。Owner オブジェクトは基本的にショップのオーナーです。これが Shop クラスでどのように定義されているかです。

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

ご覧のとおり、所有者は User オブジェクトを返します。ただし、 User クラスに .username を実行すると、常に未定義になります。どうしてこれなの?

"<%= '/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);
        }
    }   
4

3 に答える 3

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;
}
于 2013-03-28T02:20:15.527 に答える
0

クラス所有者の名前は、「ユーザー名」ではなく変数「名前」に格納されているようです

于 2013-03-28T02:11:10.683 に答える
0

関係のデータではないためだと思います。おそらく、メソッドを呼び出して別のオブジェクトを取得し、そのオブジェクトで別のメソッドを呼び出してさらに別のオブジェクトを取得するアイテムがあるようです。最初の呼び出しはプロキシ オブジェクトであるため、おそらく 2 番目のプロキシ オブジェクト データは読み込まれません。コントローラーのようにテンプレートの外側のデータのデータを見ると、そこで定義されていないと思います。ビューに到達する前に、データがロードされていることを確認したい場合があります。問題の根源は最初の関係だと思います。

データをフェッチしたり、値を設定したりするコントローラーコードの一部を投稿するのに役立ちます。

于 2013-03-28T03:07:24.000 に答える