Componette

Componette

Carrooi

Carrooi / Nette-Favorites

Favorites module in Doctrine for Nette framework

download-cloud-line composer require carrooi/favorites

Carrooi/Favorites

Build Status Donate

Favorites module in Doctrine for Nette framework.

Installation

$ composer require carrooi/favorites
$ composer update

Then just enable nette extension in your config.neon:

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.

Usage

Lets create our User implementation.

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.

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.

Do not forget to update your database schema after every change.

Manipulation

You can use prepared Carrooi\Favorites\Model\Facades\FavoritesFacade service for manipulations with favorites.

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.

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

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

That method can be used only in combination with custom associations. See bellow

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

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.

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

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:

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.

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.

Changelog

  • 1.0.2

    • Add missing cascade removing for user #1
  • 1.0.1

    • Fixed tests running under nette 2.3
    • Fix relations mapping
  • 1.0.0

    • First version

No release at this moment. Try to create first one.

bar-chart-fill

Statistics

download-cloud-fill
391
star-fill
0
bug-fill
0
flashlight-fill
7.9y
price-tag-2-line

Badges

guide-fill

Dependencies

Componette Componette felix@nette.org