ZF2原则与objectSelect有很多关系



我有多个用户,在一个多对多关系数据库中有多个存储。每个用户都有多个附加的store

现在,我想加载所有的storename从一个登录的用户在一个选择的形式。

我该怎么做?

我的用户实体:

namespace ApplicationEntity;
use BjyAuthorizeProviderRoleProviderInterface;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use ZfcUserEntityUserInterface;
/**
 * An example of how to implement a role aware user entity.
 *
 * @ORMEntity
 * @ORMTable(name="users")
 * @ORMEntity(repositoryClass="ApplicationRepositoriesUserRepository")
 *
 */
class User implements UserInterface, ProviderInterface
{
    /**
     * @var int
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var string
     * @ORMColumn(type="string", length=255, unique=true, nullable=true)
     */
    protected $username;
    /**
     * @var string
     * @ORMColumn(type="string", unique=true,  length=255)
     */
    protected $email;
    /**
     * @var string
     * @ORMColumn(type="string", length=50, nullable=true)
     */
    protected $displayName;
    /**
     * @var string
     * @ORMColumn(type="string", length=128)
     */
    protected $password;
    /**
     * @var int
     */
    protected $state;
    /**
     * @var DoctrineCommonCollectionsCollection
     * @ORMManyToMany(targetEntity="ApplicationEntityRole")
     * @ORMJoinTable(name="user_role_linker",
     *      joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    protected $roles;
    /**
         * @var DoctrineCommonCollectionsCollection
         * @ORMManyToMany(targetEntity="ApplicationEntityStore")
         * @ORMJoinTable(name="user_store_linker",
         *      joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
         *      inverseJoinColumns={@ORMJoinColumn(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;
    }
}

My store Entity:

namespace ApplicationEntity;
use DoctrineORMMapping as ORM;
/**
 * An example entity that represents a store.
 *
 * @ORMEntity
 * @ORMTable(name="store")
 *
 */
class Store
{
    /**
     * @var int
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var string
     * @ORMColumn(type="string", name="storeName", length=255, unique=true, nullable=true)
     */
    protected $storeName;
    /**
     * @var DoctrineCommonCollectionsCollection
     * @ORMManyToMany(targetEntity="ApplicationEntityProduct" )
     */
    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' => 'DoctrineModuleFormElementObjectSelect',
        'name' => 'stores',
        'attributes' => array(
            'multiple' => true,
        ),
        'options' => array(
            'object_manager' => $objectManager,
            'target_class'   => 'ApplicationEntityUser',
            '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

有人知道吗?

好的,我已经找到了一个自定义存储库。

将repository类添加到您的存储实体

    <?php
    namespace ApplicationEntity;
    use DoctrineORMMapping as ORM;
    /**
     *
     * @ORMEntity
     * @ORMTable(name="store")
     * @ORMEntity(repositoryClass="ApplicationRepositoriesStoreRepository")  
     *
     */
    class Store
    {
        /**
         * @var int
         * @ORMId
         * @ORMColumn(type="integer")
         * @ORMGeneratedValue(strategy="AUTO")
         */
        protected $id;
        /**
         * @var string
         * @ORMColumn(type="string", name="storeName", length=255, unique=true, nullable=true)
         */
        protected $storeName;
        /**
         * @var DoctrineCommonCollectionsCollection
         * @ORMManyToMany(targetEntity="ApplicationEntityProduct", inversedBy="stores")
         * @ORMJoinTable(name="product_store")
         */
        protected $products;
        /**
         * @var DoctrineCommonCollectionsCollection
         * @ORMManyToMany(targetEntity="ApplicationEntityCategory", inversedBy="stores")
         * @ORMJoinTable(name="category_store")
         */
        protected $categories;
        /**
         * @var DoctrineCommonCollectionsCollection
         * @ORMManyToMany(targetEntity="ApplicationEntityUser", inversedBy="stores")
         * @ORMJoinTable(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 ApplicationEntity;
use BjyAuthorizeProviderRoleProviderInterface;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use ZfcUserEntityUserInterface;
/**
 *
 * @ORMEntity
 * @ORMTable(name="users")
 * @ORMEntity(repositoryClass="ApplicationRepositoriesUserRepository")
 *
 */
class User implements UserInterface, ProviderInterface
{
    /**
     * @var int
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var string
     * @ORMColumn(type="string", length=255, unique=true, nullable=true)
     */
    protected $username;
    /**
     * @var string
     * @ORMColumn(type="string", unique=true,  length=255)
     */
    protected $email;
    /**
     * @var string
     * @ORMColumn(type="string", length=50, nullable=true)
     */
    protected $displayName;
    /**
     * @var string
     * @ORMColumn(type="string", length=128)
     */
    protected $password;
    /**
     * @var int
     */
    protected $state;
    /**
     * @var DoctrineCommonCollectionsCollection
     * @ORMManyToMany(targetEntity="ApplicationEntityRole")
     * @ORMJoinTable(name="user_role_linker",
     *      joinColumns={@ORMJoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    protected $roles;

    /**
     * @var DoctrineCommonCollectionsCollection
     * @ORMManyToMany(targetEntity="ApplicationEntityStore", 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;
    }
}

对象select

    $this->add(array(
        'type' => 'DoctrineModuleFormElementObjectSelect',
        'name' => 'stores',
        'attributes' => array(
            'multiple' => true,
        ),
        'options' => array(
            'object_manager' => $objectManager,
            'target_class'   => 'ApplicationEntityStore',
            '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 ApplicationRepositories;
    use DoctrineORMEntityRepository;
    use DoctrineORMQueryResultSetMapping;
    use DoctrineDBALTypesType;

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

最新更新