我有一个User实体,在多对多关系数据库中附加了商店。 我用我的控制器加载商店:
$userrepository = $this->getEntityManager()->getRepository('Thuiswinkelen\Entity\User');
$user = $userrepository->findOneBy(array('id' => $this->getUserId()));
$stores = $user->getStores();
这很好用。
现在,我希望登录用户选择附加到他的商店,并向他展示该商店中的所有商品。
我想到这样的事情:
$user->getStore($storeid)->getProducts();
这不起作用,因为我没有像这样设置实体。 我也认为这是一种不好的做法(?)
第二个选项是
$storerepository = $this->getEntityManager()->getRepository('Thuiswinkelen\Entity\Store');
$store = $storerepository->findOneBy(array('id' => $storeid));
但在这种情况下,每个人都可以查看未附加到商店的商品。
怎么做?
我的实体看起来像这样:
实体/ user.php的
<?php
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")
*
* @author Tom Oram <tom@scl.co.uk>
*/
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->getValues();
}
/**
* Get store.
*
* @return array
*/
public function getStore($id)
{
return $this->stores->getValues();
}
/**
* Add a store to the user.
*
* @param Role $store
*
* @return void
*/
public function addStore($store)
{
$this->stores[] = $store;
}
}
实体/ Store.php
<?php
*emphasized text*
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* An example entity that represents a store.
*
* @ORM\Entity
* @ORM\Table(name="store")
*
* @author Tom Oram <tom@scl.co.uk>
*/
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" )
* @ORM\JoinTable(name="product_store",
* joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="store_id", referencedColumnName="id")}
* )
*/
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;
}
}
实体/ Product.php
<?php
namespace Application\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* An example entity that represents a product.
*
* @ORM\Entity
* @ORM\Table(name="product")
*
* @author Tom Oram <tom@scl.co.uk>
*/
class Product
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Application\Entity\Store")
*/
protected $stores;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=false)
*/
protected $title;
/**
* @var string
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $sku;
/**
* @var string
* @ORM\Column(type="text", nullable=false)
*/
protected $description;
/**
* @var int
* @ORM\Column(type="decimal",precision=12, scale=2, nullable=false)
*/
protected $pricingIncl;
/**
* @var int
* @ORM\Column(type="decimal",precision=12, scale=2, nullable=true)
*/
protected $cost;
/**
* @var int
* @ORM\Column(type="integer", nullable=false)
*/
protected $taxgroup;
/**
* @var int
* @ORM\Column(type="decimal",precision=12, scale=2, nullable=true)
*/
protected $sale;
/**
* @var int
* @ORM\Column(type="integer", unique=false, nullable=false)
*/
protected $stock;
/**
* @var int
* @ORM\Column(type="integer", unique=false, nullable=false)
*/
protected $stockstate;
/**
* @var int
* @ORM\Column(type="integer", unique=false, nullable=false)
*/
protected $autostock;
/**
* @ORM\OneToMany(targetEntity="Application\Entity\Image", mappedBy="product", cascade={"persist"})
* )
*/
protected $images;
/**
* Initialies the roles variable.
*/
public function __construct()
{
$this->images = new ArrayCollection();
$this->stores = 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 store.
*
* @return array
*/
public function getStores()
{
return $this->stores->getValues();
}
public function addStores(Collection $stores){
foreach($stores as $store){
$this->stores->add($store);
}
}
public function removeStores(Collection $stores){
foreach($stores as $store){
$this->stores->removeElement($store);
}
}
/**
* Get the title.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set the title.
*
* @param string $title
*
* @return void
*/
public function setTitle($title)
{
$this->title = (string) $title;
}
/**
* Get the sku.
*
* @return string
*/
public function getSku()
{
return $this->sku;
}
/**
* Set the sku.
*
* @param string $sku
*
* @return void
*/
public function setSku($sku)
{
$this->sku = (string) $sku;
}
/**
* Get the description.
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set the description.
*
* @param string $description
*
* @return void
*/
public function setDescription($description)
{
$this->description = (string) $description;
}
/**
* Get the pricing incl tax.
*
* @return int
*/
public function getPricingIncl()
{
return $this->pricingIncl;
}
/**
* Set the pricing incl tax.
*
* @param int $pricingIncl
*
* @return void
*/
public function setPricingIncl($pricingIncl)
{
$this->pricingIncl = $pricingIncl;
}
/**
* Get the cost
*
* @return integer
*/
public function getCost()
{
return $this->cost;
}
/**
* Set the cost
*
* @param int $cost
*
* @return void
*/
public function setCost($cost)
{
$this->cost = $cost;
}
/**
* Get the tax group.
*
* @return integer
*/
public function getTaxgroup()
{
return $this->taxgroup;
}
/**
* Set the tax group.
*
* @param int $taxgroup
*
* @return void
*/
public function setTaxgroup($taxgroup)
{
$this->taxgroup = (int) $taxgroup;
}
/**
* Get the sale
*
* @return integer
*/
public function getSale()
{
return $this->sale;
}
/**
* Set the sale
*
* @param int $sale
*
* @return void
*/
public function setSale($sale)
{
$this->sale = (int) $sale;
}
/**
* Get the stock.
*
* @return integer
*/
public function getStock()
{
return $this->stock;
}
/**
* Set the stock.
*
* @param int $stock
*
* @return void
*/
public function setStock($stock)
{
$this->stock = (int) $stock;
}
/**
* Get the stock state
*
* @return integer
*/
public function getStockState()
{
return $this->stockstate;
}
/**
* Set the stock state
*
* @param int $stockstate
*
* @return void
*/
public function setStockState($stockstate)
{
$this->stockstate = (int) $stockstate;
}
/**
* Get the auto stock.
*
* @return integer
*/
public function getAutoStock()
{
return $this->autostock;
}
/**
* Set the auto stock.
*
* @param int $autostock
*
* @return void
*/
public function setAutoStock($autostock)
{
$this->autostock = (int) $autostock;
}
/**
* Get images.
*
* @return array
*/
public function getImages()
{
return $this->images;
}
/**
* Add a image to the product.
*
* @param Images
*
* @return void
*/
public function addImages(Collection $images)
{
foreach ($images as $image) {
$image->setProduct($this);
$this->images->add($image);
}
}
/**
* @param Collection $images
*/
public function removeImages(Collection $images)
{
foreach ($images as $image) {
$image->setProduct(null);
$this->images->removeElement($image);
}
}
}
答案 0 :(得分:2)
我认为,你的第二种方法是更好的方法。您应该使用存储库,甚至更好的服务类(内部处理存储库)来访问您的用户,商店等。在这种情况下,将分别为用户和商店提供两个单独的服务。然后,你打电话:
$user = $userService->get($userId);
获取用户,并且:
$store = $storeService->get($storeId);
获取您想要的商店。
对于访问控制,您可以在任一服务中简单地实现类似hasStore(User $user, Store $store)
方法的内容,并在需要时调用它。
当然,有一个很大的问题是,是否首先在您的应用程序中引入服务层,但通常它被认为是一种良好的做法(在大多数情况下)。
答案 1 :(得分:0)
在您的情况下,请尝试
$userrepository = $this->getEntityManager()->getRepository('Thuiswinkelen\Entity\User');
$user = $userrepository->findOneBy(array('id' => $this->getUserId()));
$stores = $user->getStores();
$product = array();
foreach ($stores as $store) {
$product[] = $store->getProducts();
}
但是,我认为这是一个坏主意:
/** * @var \Doctrine\Common\Collections\Collection * @ORM\ManyToMany(targetEntity="Application\Entity\Product" ) * @ORM\JoinTable(name="product_store", * joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="store_id", referencedColumnName="id")} * ) */ protected $products;
我的想法 - 你需要另一张带有约束的表idStore&lt; - &gt; idProduct