2017 © Pedro Peláez
 

library favorites

Favorites bundle for Nette

image

carrooi/favorites

Favorites bundle for Nette

  • Friday, April 29, 2016
  • by sakren
  • Repository
  • 1 Watchers
  • 0 Stars
  • 383 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Carrooi/Favorites

Build Status Donate, (*1)

Favorites module in Doctrine for Nette framework., (*2)

Installation

$ composer require carrooi/favorites
$ composer update

Then just enable nette extension in your config.neon:, (*3)

extensions:
    favorites: Carrooi\Favorites\DI\FavoritesExtension

Configuration

extensions:
    favorites: Carrooi\Favorites\DI\FavoritesExtension

favorites:

    userClass: App\Model\Entities\User

As you can see, the only thing you need to do is set your user class which implements Carrooi\Favorites\Model\Entities\IUserEntity interface., (*4)

Usage

Lets create our User implementation., (*5)

namespace App\Model\Entities;

use Carrooi\Favorites\Model\Entities\IUserEntity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @author David Kudera
 */
class User implements IUserEntity
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @var int
     */
    private $id;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

}

Now imagine that you want to be able to add entity Article to favorites., (*6)

namespace App\Model\Entities;

use Carrooi\Favorites\Model\Entities\IFavoritableEntity;
use Carrooi\Favorites\Model\Entities\TFavorites;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @author David Kudera
 */
class Article implements IFavoritableEntity
{

    use TFavorites;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @var int
     */
    private $id;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

}

Please notice that you can use TFavorites trait, which implements all methods from IFavoritableEntity interface., (*7)

Do not forget to update your database schema after every change., (*8)

Manipulation

You can use prepared Carrooi\Favorites\Model\Facades\FavoritesFacade service for manipulations with favorites., (*9)

Add to favorites

$article = $this->articles->createSomehow();
$user = $this->users->getCurrentSomehow();

$favoritesFacade->addItemToFavorites($user, $article);

Remove from favorites

$article = $this->articles->getCurrentSomehow();
$user = $this->users->getCurrentSomehow();

$favoritesFacade->removeItemFromFavorites($user, $article);

Is item in favorites

$article = $this->articles->getCurrentSomehow();
$user = $this->users->getCurrentSomehow();

$favoritesFacade->hasItemInFavorites($user, $article);

Find all items by user and type

$user = $this->user->getCurrentSomehow();

$favoritesFacade->findAllItemsByUserAndType($user, Article::getClassName());

Find all by user and type

Similar to previous method, but will return FavoriteItem entities, not IFavoritableEntity., (*10)

$user = $this->user->getCurrentSomehow();

$favoritesFacade->findAllByUserAndType($user, Article::getClassName());

That method can be used only in combination with custom associations. See bellow, (*11)

Find all favorites by user

$user = $this->user->getCurrentSomehow();

$favoritesFacade->findAllByUser($user);

That method can be used only in combination with custom associations. See bellow, (*12)

Count by user

$user = $this->user->getCurrentSomehow();

$favoritesFacade->getCountByUser($user);

Custom FavoriteItem entity

favorites:

    userClass: App\Model\Entities\User
    favoriteItemClass: App\Model\Entities\FavoriteItem
namespace App\Model\Entities;

use Carrooi\Favorites\Model\Entities\FavoriteItem;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @author David Kudera
 */
class FavoriteItem extends FavoriteItem
{

    // ...

}

This will come in handy when you'll want to use FavoriteItem entity in your queries with JOIN., (*13)

Just imagine that you want to have eg. getArticle() method in FavoriteItem entity., (*14)

namespace App\Model\Entities;

use Carrooi\Favorites\Model\Entities\FavoriteItem;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @author David Kudera
 */
class FavoriteItem extends FavoriteItem
{

    /** @var \App\Model\Entities\Article */
    private $article;

    /**
     * @return \App\Model\Entities\Article
     */
    public function getArticle()
    {
        return $this->article;
    }

    /**
     * @param \App\Model\Entities\Article $article
     * @return $this
     */
    public function setArticle(Article $article)
    {
        $this->article = $article;
        return $this;
    }

}

And add configuration:, (*15)

favorites:

    userClass: App\Model\Entities\User
    favoriteItemClass: App\Model\Entities\FavoriteItem

    associations:
        App\Model\Entities\Article:
            field: article
            setter: setArticle

Now you have your own implementation of FavoriteItem entity., (*16)

Please also notice that if you'll use this custom association mapping, this module will work with one-to-many relations. Otherwise it will be many-to-many., (*17)

Changelog

  • 1.0.2, (*18)

    • Add missing cascade removing for user #1
  • 1.0.1, (*19)

    • Fixed tests running under nette 2.3
    • Fix relations mapping
  • 1.0.0, (*20)

    • First version

The Versions