2013-08-20 19 views
0

在實體我有一個看起來像這樣的字段中選擇數據:Symfony2的-主義與WHERE從DoctrineArray

/** 
* @ORM\Column(type="array") 
*/ 
protected $category; 

和QueryBuilder的

$qb = $this->createQueryBuilder('s'); 
$qb->select($fields) 
    ->where('s.category IN (:category)') //////////// <---- 
    ->orderBy('s.name', 'ASC') 
    ->setParameter('category', $category_id); 

所以在數據庫中的字段類是Doctrine2陣列。我想用QueryBuilder從數據庫中選擇記錄。我的問題是,我怎麼能做到這一點,WHERE子句將檢查該數組中的字段?

+1

沒有。您無法查詢數組值。如果你檢查你的數據庫,你會發現它實際存儲爲一個字符串。數組類型是持久化數組的一個原則擴展。數據庫本身並不知道它們。如果你想做這種查詢,那麼你需要用它自己的表來創建一個名爲Category的實體,並將它與主實體相關聯。 – Cerad

回答

0

看看here可以幫助你

// Instead, use $qb->expr()->in('value', array('?1')) and bind your parameter to ?1 (see section above) 
public function in($x, $y); // Returns Expr\Func instance 
0
$qb->select($fields) 
    ->where($qb->expr()->in('s.category', $categories)) 
0

@Cerad給你一個完全有效的註釋。存儲數組的問題之一是你沒有任何搜索機會。

請參閱PHP/MySQL - Storing array in databaseStoring arrays in the database。正如你所看到的那樣,這是一種可怕的做法。

最好的方法是簡單地創建一個類別實體,並與該類別建立OneToMany關係。

這裏是實體書的一個例子,有許多類:

創建類的實體:

class Category implements CategoryInterface 
{ 
    //..... 

    /** 
    * Title of the category 
    * 
    * @ORM\Column(type="string", length=100) 
    */ 
    protected $title; 

    /** 
    * Relation with your book entity for example 
    * 
    * @ORM\ManyToOne(targetEntity="Book", inversedBy="categories") 
    * @ORM\JoinColumn(name="book_id", referencedColumnName="id") 
    */ 
    private $book; 

    /** 
    * Set book 
    * 
    * @param BookInterface $book 
    */ 
    public function setBook(BookInterface $book) 
    { 
     $this->book = $book; 
    } 

    /** 
    * Get book 
    * 
    * @return BookInterface 
    */ 
    public function getBook() 
    { 
     return $this->book; 
    } 


} 

你的書的實體:

use Doctrine\Common\Collections\ArrayCollection; 

class Book implements BookInterface 
{ 
    /** 
    * Categories for the books 
    * 
    * @ORM\OneToMany(targetEntity="Category", mappedBy="book") 
    * @var CategoryInterface[] 
    */ 
    protected $categories ; 

    public function __construct() 
    { 
     $this->categories = new ArrayCollection(); 
    } 

    /** 
    * Add Categories 
    * 
    * @param CategoryInterface $category 
    */ 
    public function addCategory(CategoryInterface $category) 
    { 
     $category->setBook($this); 
     $this->categories->add($category); 
    } 

    /** 
    * Remove Category 
    * 
    * @param CategoryInterface $category 
    * @return bool 
    */ 
    public function removeCategory(CategoryInterface $category) 
    { 
     return $this->categories->removeElement($category); 
    } 

    /** 
    * Get Categories 
    * 
    * @return Doctrine\Common\Collections\Collection 
    */ 
    public function getCategories() 
    { 
     return $this->categories; 
    } 

    /** 
    * Set Categories 
    * 
    * @param ArrayCollection $categories 
    */ 
    public function setCategories($categories) { 

     $this->categories->clear(); 

     foreach ($categories as $category) { 
      $this->addCategory($category); 
     } 

     return $this; 
    } 

您現在可以正確搜索。