У меня несколько пользователей, с несколькими магазинами во многих-много реляционных баз данных. Каждый пользователь имеет несколько хранилищ, прикрепленных к ним.
Теперь я хочу загрузить все имена файлов из зарегистрированного пользователя в форме выбора.
Как я могу это сделать?
Мой пользовательский объект:
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
Кто угодно?
Хорошо, я понял это с помощью специального репозитория.
Добавить репозиторий в свой объект хранилища
<?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(); } }