Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
added 11 characters in body
Source Link
Mick
  • 32.2k
  • 17
  • 114
  • 132

@Cerad@Cerad gave you a perfectly valid comment. One of the problem of storing arrays is that you don't have any chance of searching.

See [PHP/MySQL PHP/MySQL - Storing array in database- Storing array in database][1], and Storing arrays in the database[Storing arrays in the database][2]. As you can see, it is a terrible practice.

use Doctrine\Common\Collections\ArrayCollection;

class Book implements BookInterface
{
    /**
     * Categories for the books
     *
     * @ORM\OneToMany(targetEntity="Category", mappedBy="book")
     * @var Category[]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;
    }

3 Your can now search properly. [1]: PHP/MySQL - Storing array in database [2]: Storing arrays in the database

@Cerad gave you a perfectly valid comment. One of the problem of storing arrays is that you don't have any chance of searching.

See PHP/MySQL - Storing array in database, and Storing arrays in the database. As you can see, it is a terrible practice.

use Doctrine\Common\Collections\ArrayCollection;

class Book implements BookInterface
{
    /**
     * Categories for the books
     *
     * @ORM\OneToMany(targetEntity="Category", mappedBy="book")
     * @var Category[]
     */
    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;
    }

@Cerad gave you a perfectly valid comment. One of the problem of storing arrays is that you don't have any chance of searching.

See [PHP/MySQL - Storing array in database][1], and [Storing arrays in the database][2]. As you can see, it is a terrible practice.

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;
    }

3 Your can now search properly. [1]: PHP/MySQL - Storing array in database [2]: Storing arrays in the database

Source Link
Mick
  • 32.2k
  • 17
  • 114
  • 132

@Cerad gave you a perfectly valid comment. One of the problem of storing arrays is that you don't have any chance of searching.

See PHP/MySQL - Storing array in database, and Storing arrays in the database. As you can see, it is a terrible practice.

The best way is to simply create a Category entity, and to have a OneToMany relation with that category.

Here is an example of an entity Book that has many categories:

1 Create your category entity:

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;
    }


}

2 Your book entity:

use Doctrine\Common\Collections\ArrayCollection;

class Book implements BookInterface
{
    /**
     * Categories for the books
     *
     * @ORM\OneToMany(targetEntity="Category", mappedBy="book")
     * @var Category[]
     */
    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;
    }