在 Spot ORM 中使用 with()
Using with() in Spot ORM
我正在使用 Spot ORM 进行副项目。在文档中写着
All relation types are lazy-loaded by default, and can be eager-loaded to solve the N+1 query problem using the with method:
$posts = $posts->all()->with('comments');
但是Entity没有这个方法,我只能在Mapper中找到class,但是是protected的(所以不可用)。我该如何实现预加载?
一般
来自 documentation:
Since Spot follows the DataMapper design pattern, you will need a mapper instance for working with object Entities and database tables.
Mappers only work with one entity type, so you will need one mapper per entity class you work with (i.e. to save an Entity\Post, you will need the appropriate mapper, and to save an Entity\Comment, you will need a comment mapper, not the same post mapper. Relations will automatically be loaded and handled by their corresponding mapper by Spot.
这意味着您必须为您的实体使用 Mapper
class。
Although you do not have to create a mapper for each entity, sometimes it is nice to create one if you have a lot of custom finder methods, or want a better place to contain the logic of building all the queries you need.
这意味着如果您需要特定功能,您可以创建自己的 Mapper
扩展(不是必需的)。除非您需要特定的逻辑,例如查找器方法或其他自定义逻辑,否则 Spot 将为您加载通用映射器并 return 它。
因此,请使用您自己的或通用的 Mapper
并从那里使用 with()
方法。请注意,受保护字段并不表示它不可用。这只是意味着以 Mapper
作为父级的 classes 也获得了 with()
方法,而 with()
方法仍然是 "private" 所以它不能到达 class.
之外
正如在 class 中指定的那样,您链接的 with()
方法受到保护,注释如下:
/**
* Eager-load associations for an entire collection
*
* @internal Implementation may change... for internal use only
*/
您可以看到它被指定为仅供内部使用。请注意,这并不意味着它不能使用,它可以,但只能在 inside the Mapper
class 中使用。 Mapper
class 中还有其他方法使用 with()
您的报价
您链接到的特定部分似乎是自相矛盾的,因为它看起来指的是 Entity
,但我认为这是一个自定义 Mapper
。
如果您认为这是您的 Post
Entity
:
namespace Entity;
class PostEntity extends \Spot\Entity
{
protected static $mapper = 'Entity\Mapper\PostMapper';
// ... snip ...
}
还有另一个从 Mapper
延伸的:
namespace Entity\Mapper;
use Spot\Mapper;
class PostMapper extends Mapper
{
//other methods
//example method
/**
* Get 10 most recent posts for display on the sidebar
*
* @return \Spot\Query
*/
public function mostRecentPostsForSidebar()
{
return $this->where(['status' => 'active'])
->order(['date_created' => 'DESC'])
->limit(10);
}
//other methods
}
您现在已经扩展了 Mapper
,所以现在您可以使用 with()
方法。
在 PostMapper
你可以做这样的事情:
//Pseudocode
public function some_method()
{
//Some code
$this->with($collection, $entityName, $with);
//Some code
}
请注意,您还可以在 PostEntity
(作为 static
字段)中使用 PostMapper
。希望你现在更清楚了。也许他们应该在文档的那部分指定它使用的是自定义 Mapper
.
我正在使用 Spot ORM 进行副项目。在文档中写着
All relation types are lazy-loaded by default, and can be eager-loaded to solve the N+1 query problem using the with method:
$posts = $posts->all()->with('comments');
但是Entity没有这个方法,我只能在Mapper中找到class,但是是protected的(所以不可用)。我该如何实现预加载?
一般
来自 documentation:
Since Spot follows the DataMapper design pattern, you will need a mapper instance for working with object Entities and database tables.
Mappers only work with one entity type, so you will need one mapper per entity class you work with (i.e. to save an Entity\Post, you will need the appropriate mapper, and to save an Entity\Comment, you will need a comment mapper, not the same post mapper. Relations will automatically be loaded and handled by their corresponding mapper by Spot.
这意味着您必须为您的实体使用 Mapper
class。
Although you do not have to create a mapper for each entity, sometimes it is nice to create one if you have a lot of custom finder methods, or want a better place to contain the logic of building all the queries you need.
这意味着如果您需要特定功能,您可以创建自己的 Mapper
扩展(不是必需的)。除非您需要特定的逻辑,例如查找器方法或其他自定义逻辑,否则 Spot 将为您加载通用映射器并 return 它。
因此,请使用您自己的或通用的 Mapper
并从那里使用 with()
方法。请注意,受保护字段并不表示它不可用。这只是意味着以 Mapper
作为父级的 classes 也获得了 with()
方法,而 with()
方法仍然是 "private" 所以它不能到达 class.
之外
正如在 class 中指定的那样,您链接的 with()
方法受到保护,注释如下:
/**
* Eager-load associations for an entire collection
*
* @internal Implementation may change... for internal use only
*/
您可以看到它被指定为仅供内部使用。请注意,这并不意味着它不能使用,它可以,但只能在 inside the Mapper
class 中使用。 Mapper
class 中还有其他方法使用 with()
您的报价
您链接到的特定部分似乎是自相矛盾的,因为它看起来指的是 Entity
,但我认为这是一个自定义 Mapper
。
如果您认为这是您的 Post
Entity
:
namespace Entity;
class PostEntity extends \Spot\Entity
{
protected static $mapper = 'Entity\Mapper\PostMapper';
// ... snip ...
}
还有另一个从 Mapper
延伸的:
namespace Entity\Mapper;
use Spot\Mapper;
class PostMapper extends Mapper
{
//other methods
//example method
/**
* Get 10 most recent posts for display on the sidebar
*
* @return \Spot\Query
*/
public function mostRecentPostsForSidebar()
{
return $this->where(['status' => 'active'])
->order(['date_created' => 'DESC'])
->limit(10);
}
//other methods
}
您现在已经扩展了 Mapper
,所以现在您可以使用 with()
方法。
在 PostMapper
你可以做这样的事情:
//Pseudocode
public function some_method()
{
//Some code
$this->with($collection, $entityName, $with);
//Some code
}
请注意,您还可以在 PostEntity
(作为 static
字段)中使用 PostMapper
。希望你现在更清楚了。也许他们应该在文档的那部分指定它使用的是自定义 Mapper
.