carrooi / favorites
Nette 框架的收藏夹组件包
1.0.2
2016-04-29 10:48 UTC
Requires
- php: >=5.4
- kdyby/doctrine: ~2.2
- kdyby/events: ~2.3
- nette/application: ~2.2
- nette/di: ~2.2
- nette/utils: ~2.2
Requires (Dev)
- nette/bootstrap: ~2.2
- nette/mail: ~2.2
- nette/safe-stream: ~2.2
- nette/tester: ~1.3.0
- tracy/tracy: ~2.2
This package is auto-updated.
Last update: 2024-09-06 03:01:14 UTC
README
Doctrine 框架中的 Nette 框架收藏夹模块。
安装
$ composer require carrooi/favorites
$ composer update
然后在您的 config.neon 配置文件中启用 nette 扩展。
extensions: favorites: Carrooi\Favorites\DI\FavoritesExtension
配置
extensions: favorites: Carrooi\Favorites\DI\FavoritesExtension favorites: userClass: App\Model\Entities\User
如您所见,您需要做的唯一一件事是设置您的 user
类,该类实现 Carrooi\Favorites\Model\Entities\IUserEntity
接口。
使用
让我们创建我们的 User
实现。
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; } }
现在假设您希望能够将实体 Article
添加到收藏夹中。
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; } }
请注意,您可以使用 TFavorites
特性,它实现了 IFavoritableEntity
接口的所有方法。
不要忘记在每次更改后更新您的数据库模式。
操作
您可以使用预定义的 Carrooi\Favorites\Model\Facades\FavoritesFacade
服务进行收藏夹操作。
添加到收藏夹
$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());
根据用户和类型查找所有项目
与上一个方法类似,但将返回 FavoriteItem
实体,而不是 IFavoritableEntity
。
$user = $this->user->getCurrentSomehow(); $favoritesFacade->findAllByUserAndType($user, Article::getClassName());
此方法只能与自定义关联结合使用。请参见下文
根据用户查找所有收藏夹
$user = $this->user->getCurrentSomehow(); $favoritesFacade->findAllByUser($user);
此方法只能与自定义关联结合使用。请参见下文
按用户计数
$user = $this->user->getCurrentSomehow(); $favoritesFacade->getCountByUser($user);
自定义 FavoriteItem
实体
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 { // ... }
当您想在使用 JOIN
的查询中使用 FavoriteItem
实体时,这会很有用。
只需想象您想在 FavoriteItem
实体中拥有例如 getArticle()
方法。
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; } }
添加配置
favorites: userClass: App\Model\Entities\User favoriteItemClass: App\Model\Entities\FavoriteItem associations: App\Model\Entities\Article: field: article setter: setArticle
现在您有了自己的 FavoriteItem
实体实现。
请注意,如果您使用此自定义关联映射,则该模块将使用一对多关系。否则,它将是多对多。
变更日志
-
1.0.2
- 添加缺少的用户级联删除功能#1
-
1.0.1
- 修复在 nette 2.3 下运行测试的问题
- 修复关系映射
-
1.0.0
- 第一个版本