ZF2学说与objectSelect获得许多联系 [英] ZF2 Doctrine get manytomany relations with objectSelect

查看:44
本文介绍了ZF2学说与objectSelect获得许多联系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个用户,在多对多关系数据库中有多个商店。每个用户都有多个与其关联的商店。

I have multiple users, with multiple stores in a many to many relational database. Every user has multiple stores attached to them.

现在,我想以一种选择的形式从登录用户中加载所有商店名称。

Now, i want to load all the storenames from a logged in user in a select form.

我该怎么做?

我的用户实体:

namespace Application\Entity;

use BjyAuthorize\Provider\Role\ProviderInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ZfcUser\Entity\UserInterface;

/**
 * An example of how to implement a role aware user entity.
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="Application\Repositories\UserRepository")
 *
 */
class User implements UserInterface, ProviderInterface
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

    /**
     * @var string
     * @ORM\Column(type="string", unique=true,  length=255)
     */
    protected $email;

    /**
     * @var string
     * @ORM\Column(type="string", length=50, nullable=true)
     */
    protected $displayName;

    /**
     * @var string
     * @ORM\Column(type="string", length=128)
     */
    protected $password;

    /**
     * @var int
     */
    protected $state;

    /**
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\ManyToMany(targetEntity="Application\Entity\Role")
     * @ORM\JoinTable(name="user_role_linker",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    protected $roles;

    /**
         * @var \Doctrine\Common\Collections\Collection
         * @ORM\ManyToMany(targetEntity="Application\Entity\Store")
         * @ORM\JoinTable(name="user_store_linker",
         *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
         *      inverseJoinColumns={@ORM\JoinColumn(name="store_id", referencedColumnName="id")}
         * )
     */
    protected $stores;


    /**
     * Initialies the roles variable.
     */
    public function __construct()
    {
        $this->roles = new ArrayCollection();
        $this->stores = new ArrayCollection();
    }

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

    /**
     * Set id.
     *
     * @param int $id
     *
     * @return void
     */
    public function setId($id)
    {
        $this->id = (int) $id;
    }

    /**
     * Get username.
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set username.
     *
     * @param string $username
     *
     * @return void
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

    /**
     * Get email.
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set email.
     *
     * @param string $email
     *
     * @return void
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * Get displayName.
     *
     * @return string
     */
    public function getDisplayName()
    {
        return $this->displayName;
    }

    /**
     * Set displayName.
     *
     * @param string $displayName
     *
     * @return void
     */
    public function setDisplayName($displayName)
    {
        $this->displayName = $displayName;
    }

    /**
     * Get password.
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set password.
     *
     * @param string $password
     *
     * @return void
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * Get state.
     *
     * @return int
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * Set state.
     *
     * @param int $state
     *
     * @return void
     */
    public function setState($state)
    {
        $this->state = $state;
    }

    /**
     * Get role.
     *
     * @return array
     */
    public function getRoles()
    {
        return $this->roles->getValues();
    }

    /**
     * Add a role to the user.
     *
     * @param Role $role
     *
     * @return void
     */
    public function addRole($role)
    {
        $this->roles[] = $role;
    }

    /**
     * Get store.
     *
     * @return array
     */
    public function getStores()
    {
        return $this->stores;
    }

    /**
     * Get store.
     *
     * @return array
     */
    public function getStore($id)
    {
        return $this->stores[$id]->getValues();
    }

    /**
     * Add a store to the user.
     *
     * @param Role $store
     *
     * @return void
     */
    public function addStore($store)
    {
        $this->stores[] = $store;
    }
}

我的商店实体:

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * An example entity that represents a store.
 *
 * @ORM\Entity
 * @ORM\Table(name="store")
 *
 */
class Store
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

    /**
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\ManyToMany(targetEntity="Application\Entity\Product" )
     */
    protected $products;

    /**
     * Initialies the roles variable.
     */
    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

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

    /**
     * Set the id.
     *
     * @param int $id
     *
     * @return void
     */
    public function setId($id)
    {
        $this->id = (int)$id;
    }

    /**
     * Get the store id.
     *
     * @return string
     */
    public function getStoreName()
    {
        return $this->storeName;
    }

    /**
     * Set the store id.
     *
     * @param string $storeName
     *
     * @return void
     */
    public function setStoreName($storeName)
    {
        $this->storeName = (string) $storeName;
    }

    /**
     * Get product.
     *
     * @return array
     */
    public function getProducts()
    {
        return $this->products->getValues();
    }

    /**
     * Add a product to the user.
     *
     * @param Role $product
     *
     * @return void
     */
    public function addProduct($products)
    {
        $this->products[] = $products;
    }

}

我的表单:

    $this->add(array(
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'name' => 'stores',
        'attributes' => array(
            'multiple' => true,
        ),
        'options' => array(
            'object_manager' => $objectManager,
            'target_class'   => 'Application\Entity\User',
            'label' => 'Selecteer winkel',
            'column-size' => 'sm-9',
            'label_attributes' => array('class' => 'col-sm-3 control-label'),
            'property'       => 'stores',
            'find_method' => array(
                'name' => 'findBy',
                'params' => array(
                    'criteria' => array('id' => $userid)
                ),
            ),
            'is_method'      => true,
        ),
    ));

我收到此消息:

    File:
     zend/vendor/zendframework/zendframework/library/Zend/View/Helper/Escaper/AbstractHelper.php:70
Message:
Object provided to Escape helper, but flags do not allow recursion

有人吗?

推荐答案

好,我已经通过自定义存储库解决了这个问题。

Ok i've figured it out with a custom repository.

向您的商店添加存储库类

Add a repositoryclass to your store entity

    <?php

    namespace Application\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     *
     * @ORM\Entity
     * @ORM\Table(name="store")
     * @ORM\Entity(repositoryClass="Application\Repositories\StoreRepository")  
     *
     */
    class Store
    {
        /**
         * @var int
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

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

        /**
         * @var \Doctrine\Common\Collections\Collection
         * @ORM\ManyToMany(targetEntity="Application\Entity\Product", inversedBy="stores")
         * @ORM\JoinTable(name="product_store")
         */
        protected $products;

        /**
         * @var \Doctrine\Common\Collections\Collection
         * @ORM\ManyToMany(targetEntity="Application\Entity\Category", inversedBy="stores")
         * @ORM\JoinTable(name="category_store")
         */
        protected $categories;

        /**
         * @var \Doctrine\Common\Collections\Collection
         * @ORM\ManyToMany(targetEntity="Application\Entity\User", inversedBy="stores")
         * @ORM\JoinTable(name="user_store")
         */
        protected $users;

        /**
         * Initialies the roles variable.
         */
        public function __construct()
        {
            $this->products = new ArrayCollection();
            $this->categories = new ArrayCollection();
            $this->users = new ArrayCollection();
        }

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

        /**
         * Set the id.
         *
         * @param int $id
         *
         * @return void
         */
        public function setId($id)
        {
            $this->id = (int)$id;
        }

        /**
         * Get the store id.
         *
         * @return string
         */
        public function getStoreName()
        {
            return $this->storeName;
        }

        /**
         * Set the store id.
         *
         * @param string $storeName
         *
         * @return void
         */
        public function setStoreName($storeName)
        {
            $this->storeName = (string) $storeName;
        }

        /**
         * Get product.
         *
         * @return array
         */
        public function getProducts()
        {
            return $this->products->getValues();
        }

        /**
         * Add a product to the user.
         *
         * @param Role $product
         *
         * @return void
         */
        public function addProduct($products)
        {
            $this->products[] = $products;
        }

        /**
         * Get category.
         *
         * @return array
         */
        public function getCategories()
        {
            return $this->categories->getValues();
        }

        /**
         * Add a product to the user.
         *
         * @param Role $product
         *
         * @return void
         */
        public function addCategory($categories)
        {
            $this->categories[] = $categories;
        }

         /**
         * Get category.
         *
         * @return array
         */
        public function getUsers()
        {
            return $this->users->getValues();
        }

        /**
         * Add a product to the user.
         *
         * @param Role $product
         *
         * @return void
         */
        public function addUser($users)
        {
            $this->users[] = $users;
        }

    }

我的用户实体



namespace Application\Entity;

use BjyAuthorize\Provider\Role\ProviderInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ZfcUser\Entity\UserInterface;

/**
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="Application\Repositories\UserRepository")
 *
 */
class User implements UserInterface, ProviderInterface
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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

    /**
     * @var string
     * @ORM\Column(type="string", unique=true,  length=255)
     */
    protected $email;

    /**
     * @var string
     * @ORM\Column(type="string", length=50, nullable=true)
     */
    protected $displayName;

    /**
     * @var string
     * @ORM\Column(type="string", length=128)
     */
    protected $password;

    /**
     * @var int
     */
    protected $state;

    /**
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\ManyToMany(targetEntity="Application\Entity\Role")
     * @ORM\JoinTable(name="user_role_linker",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    protected $roles;


    /**
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\ManyToMany(targetEntity="Application\Entity\Store", mappedBy="users")
     */
    protected $stores;


    /**
     * Initialies the roles variable.
     */
    public function __construct()
    {
        $this->roles = new ArrayCollection();
        $this->stores = new ArrayCollection();
    }

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

    /**
     * Set id.
     *
     * @param int $id
     *
     * @return void
     */
    public function setId($id)
    {
        $this->id = (int) $id;
    }

    /**
     * Get username.
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set username.
     *
     * @param string $username
     *
     * @return void
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

    /**
     * Get email.
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set email.
     *
     * @param string $email
     *
     * @return void
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * Get displayName.
     *
     * @return string
     */
    public function getDisplayName()
    {
        return $this->displayName;
    }

    /**
     * Set displayName.
     *
     * @param string $displayName
     *
     * @return void
     */
    public function setDisplayName($displayName)
    {
        $this->displayName = $displayName;
    }

    /**
     * Get password.
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set password.
     *
     * @param string $password
     *
     * @return void
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * Get state.
     *
     * @return int
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * Set state.
     *
     * @param int $state
     *
     * @return void
     */
    public function setState($state)
    {
        $this->state = $state;
    }

    /**
     * Get role.
     *
     * @return array
     */
    public function getRoles()
    {
        return $this->roles->getValues();
    }

    /**
     * Add a role to the user.
     *
     * @param Role $role
     *
     * @return void
     */
    public function addRole($role)
    {
        $this->roles[] = $role;
    }

    /**
     * Get store.
     *
     * @return array
     */
    public function getStores()
    {
        return $this->stores;
    }

    /**
     * Get store.
     *
     * @return array
     */
    public function getStore($id)
    {
        return $this->stores[$id]->getValues();
    }

    /**
     * Add a store to the user.
     *
     * @param Role $store
     *
     * @return void
     */
    public function addStore($store)
    {
        $this->stores[] = $store;
    }
}

对象选择

    $this->add(array(
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'name' => 'stores',
        'attributes' => array(
            'multiple' => true,
        ),
        'options' => array(
            'object_manager' => $objectManager,
            'target_class'   => 'Application\Entity\Store',
            'label' => 'Selecteer winkel',
            'column-size' => 'sm-9',
            'label_attributes' => array('class' => 'col-sm-3 control-label'),
            'property'       => 'storeName',
            'is_method'      => true,
            'find_method'    => array(
                'name'   => 'storesByUser',
                'params' => array(
                    'criteria' => array('id' => $userid),
                ),
            ),


        ),
    ));

我的自定义存储库

    <?php

    namespace Application\Repositories;

    use Doctrine\ORM\EntityRepository;
    use Doctrine\ORM\Query\ResultSetMapping;
    use Doctrine\DBAL\Types\Type;


    class StoreRepository extends EntityRepository
    {
        public function storesByUser(array $criteria){

            return $this->createQueryBuilder('s')
            ->select('s')
            ->innerJoin("s.users", "u", "WITH", "u=:userid")
                ->setParameter("userid", $criteria['id'])
            ->getQuery()->getResult();

        }
    }

这篇关于ZF2学说与objectSelect获得许多联系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆