Favorites module in Doctrine for Nette framework.
$ composer require carrooi/favorites
$ composer update
Then just enable nette extension in your config.neon:
extensions:
favorites: Carrooi\Favorites\DI\FavoritesExtension
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.
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.
You can use prepared Carrooi\Favorites\Model\Facades\FavoritesFacade
service for manipulations with favorites.
$article = $this->articles->createSomehow();
$user = $this->users->getCurrentSomehow();
$favoritesFacade->addItemToFavorites($user, $article);
$article = $this->articles->getCurrentSomehow();
$user = $this->users->getCurrentSomehow();
$favoritesFacade->removeItemFromFavorites($user, $article);
$article = $this->articles->getCurrentSomehow();
$user = $this->users->getCurrentSomehow();
$favoritesFacade->hasItemInFavorites($user, $article);
$user = $this->user->getCurrentSomehow();
$favoritesFacade->findAllItemsByUserAndType($user, Article::getClassName());
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
$user = $this->user->getCurrentSomehow();
$favoritesFacade->findAllByUser($user);
That method can be used only in combination with custom associations. See bellow
$user = $this->user->getCurrentSomehow();
$favoritesFacade->getCountByUser($user);
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.
-
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