2016-01-23 60 views
1

我有兩個實體主義一對多加列檢查

用戶

<?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-> getPhotos()獲取所有用戶的照片,這是一對多的。但它會返回所有用戶的照片。應該怎麼做,所以它返回所有具有isActive = true的照片?

謝謝

回答

0

有很多方法可以做到這一點。

Cosmin爲您提供了一個,這將導致Doctrine加載每張照片,然後PHP代碼檢查以查看它是否處於活動狀態。這將適用於大多數情況,但可能會效率低下。

又一解決方案是使用「標準」:

$exp = new \Doctrine\ORM\Query\Expr(); 
$activePhotos = $user->getPhotos()->matching(
    new \Doctrine\Common\Collections\Criteria(
     $exp->eq('active', true) 
    ) 
); 

這將做類似filter是科斯明建議的東西,但允許教到在數據庫級別進行篩選。

1

你可以嘗試過濾方法內部實體

$user->getPhotos()->filter(
    function($photo) { 
     return $photo->isActive(); 
    } 
); 
0

您可以創建用戶實體內的方法來查詢,使用@Cosmin Ordean的代碼:)像這樣的事情

public function getActivePhotos() { 
    return $this->getPhotos()->filter(
     function($photo) { 
      return $photo->isActive(); 
     } 
    ); 
}