我有两个实体
用户
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="first_name", type="text", nullable=true)
*/
protected $firstName;
/**
* @ORM\Column(name="last_name", type="text", nullable=true)
*/
protected $lastName;
/**
* @ORM\Column(type="bigint", nullable=true)
*/
protected $phone;
/**
* @ORM\Column(name="birth_date", type="date", nullable=true)
*/
protected $birthDate;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $gender;
/**
* @ORM\Column(name="location_country", type="text", nullable=true)
*/
protected $locationCountry;
/**
* @ORM\Column(name="location_city", type="text", nullable=true)
*/
protected $locationCity;
/**
* @ORM\Column( type="text", nullable=true)
*/
protected $avatar;
/**
* @ORM\Column(name="wall_image", type="text", nullable=true)
*/
protected $wallImage;
/**
* @ORM\Column( type="text", nullable=true)
*/
protected $about;
/**
* @ORM\OneToMany(targetEntity="Follower", mappedBy="user")
*/
protected $followers;
/**
* @ORM\OneToMany(targetEntity="Follower", mappedBy="follower")
*/
protected $followings;
/**
* Is followed. Used when checking is followed by another user.
*/
protected $isFollowed = false;
/**
* @ORM\OneToMany(targetEntity="Photo", mappedBy="user")
* @ORM\OrderBy({"id" = "DESC"})
*/
protected $photos;
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
public function getPhone()
{
return $this->phone;
}
public function getBirthDate()
{
return $this->birthDate;
}
public function getGender()
{
return $this->gender;
}
public function getLocationCountry()
{
return $this->locationCountry;
}
public function getLocationCity()
{
return $this->locationCity;
}
public function getAvatar()
{
return $this->avatar;
}
public function getAvatarImage()
{
return $this->getAvatarPath().$this->avatar;
}
public function getWallImage()
{
return $this->wallImage;
}
public function getAbout()
{
return $this->about;
}
public function getAvatarPath()
{
return '/web/uploads/avatars/'.$this->id.'/';
}
public function getWallImagePath()
{
return '/web/uploads/wall/'.$this->id.'/';
}
public function getFollowers()
{
return $this->followers;
}
public function getFollowings()
{
return $this->followings;
}
public function getFollowersCount()
{
//print_r($this->followers->toArray());
}
public function getPhotos()
{
return $this->photos;
}
public function isFollowed()
{
return $this->isFollowed;
}
public function setFirstName($value)
{
$this->firstName = $value;
}
public function setLastName($value)
{
$this->lastName = $value;
}
public function setPhone($value)
{
$this->phone = $value;
}
public function setBirthDate($value)
{
$this->birthDate = $value;
}
public function setGender($value)
{
$this->gender = $value;
}
public function setLocationCountry($value)
{
$this->locationCountry = $value;
}
public function setLocationCity($value)
{
$this->locationCity = $value;
}
public function setAvatar($path)
{
$this->avatar = $path;
}
public function setWallImage($path)
{
$this->wallImage = $path;
}
public function setAbout($about)
{
$this->about = $about;
}
public function setFollowed($isFollowed)
{
$this->isFollowed = $isFollowed;
}
}
照片
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="photos")
*/
class Photo
{
const
CATEGORY_PHOTOGRAPHY = 1,
CATEGORY_PAINTING = 2,
CATEGORY_3D = 3;
/*
* Flow photos limit
*/
const FLOW_PHOTOS_LIMIT = 15;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="photos")
*/
protected $user;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $title;
/**
* @ORM\Column(type="text", nullable=true)
*/
protected $description;
/**
* @ORM\Column(type="text")
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="PhotoCategory")
*/
protected $category;
/**
* @ORM\Column(name="creation_date", type="datetime")
*/
protected $creationDate;
/**
* @ORM\Column(name="edit_date", type="datetime", nullable=true)
*/
protected $editDate;
/**
* @ORM\Column(name="is_moderated", type="boolean")
*/
protected $isModerated = false;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
protected $isActive = true;
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="photo")
* @ORM\OrderBy({"id" = "DESC"})
*/
protected $comments;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* @param User $user
*
* @return Photo
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return User $user
*/
public function getUser()
{
return $this->user;
}
/**
* Get category
*
* @return Category $category
*/
public function getCategory()
{
return $this->category;
}
/**
* Set title
*
* @param string $title
*
* @return Photo
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Get creationDate
*
* @return \DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Get editDate
*
* @return \DateTime
*/
public function getEditDate()
{
return $this->editDate;
}
/**
* Get is active
*
* @return integer
*/
public function isActive()
{
return $this->isActive;
}
/**
* Get is moderated
*
* @return integer
*/
public function isModerated()
{
return $this->isModerated;
}
/*
* Get image
*
* @return string
*/
public function getImage()
{
return $this->getWebDirectory().$this->getName();
}
/*
* Get image directory
*
* @return string
*/
public function getDirectory()
{
return __DIR__.'/../../../web/uploads/photos/'.$this->getUser()->getId().'/'.$this->creationDate->format('Y-m-d').'/';
}
/*
* Get image web directory
*
* @return string
*/
public function getWebDirectory()
{
return '/web/uploads/photos/'.$this->getUser()->getId().'/'.$this->creationDate->format('Y-m-d').'/';
}
/*
* Get comments
*/
public function getComments()
{
return $this->comments;
}
/**
* Set description
*
* @param string $description
*
* @return Photo
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set name
*
* @param string $name
*
* @return Photo
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set creationDate
*
* @param \DateTime $creationDate
*/
public function setCreationDate(\DateTime $creationDate)
{
$this->creationDate = $creationDate;
}
/**
* Set editDate
*
* @param \DateTime $editDate
*/
public function setEditDate(\DateTime $editDate)
{
$this->editDate = $editDate;
}
/**
* Set active
*/
public function setActive($active)
{
$this->isActive = $active;
}
/**
* Set category
*/
public function setCategory($category)
{
$this->category = $category;
}
/**
* Set moderated
*/
public function setModerated($moderated)
{
$this->isModerated = $moderated;
}
}
如你所见,我在照片中有isActive,这说明是否删除了照片。所以,我通过User-&gt; getPhotos()得到所有用户的照片,这是一对多的。但它返回所有用户的照片。该怎么做,所以它返回所有isActive = true的照片?
谢谢
答案 0 :(得分:1)
您可以尝试使用过滤方法查询实体内部
$user->getPhotos()->filter(
function($photo) {
return $photo->isActive();
}
);
答案 1 :(得分:0)
你可以使用@Cosmin Ordean的代码在User实体中创建一个方法:)像这样的东西
public function getActivePhotos() {
return $this->getPhotos()->filter(
function($photo) {
return $photo->isActive();
}
);
}
答案 2 :(得分:0)
有很多方法可以做到这一点。
Cosmin为您提供了一个,这将导致Doctrine加载每张照片,然后PHP代码检查以查看它是否处于活动状态。这将适用于大多数情况,但可能效率低下。
另一种解决方案是使用“标准”:
$exp = new \Doctrine\ORM\Query\Expr();
$activePhotos = $user->getPhotos()->matching(
new \Doctrine\Common\Collections\Criteria(
$exp->eq('active', true)
)
);
这将执行与Cosmin建议的filter
类似的操作,但允许Doctrine在数据库级别进行过滤。