Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php &引用;“持续时间”;推进实例池中的元素数量_Php_Propel - Fatal编程技术网

Php &引用;“持续时间”;推进实例池中的元素数量

Php &引用;“持续时间”;推进实例池中的元素数量,php,propel,Php,Propel,我是PHP新手,也是推进新手 正如我所读到的,Propel有一个实例池,用于重用querys 比如说 <?php // first call $author1 = AuthorQuery::create()->getByFooField('foo'); $foo = $author1->getId(); // SELECT query ... // second call $author2 = AuthorQuery::create()->getByFooFiel

我是PHP新手,也是推进新手

正如我所读到的,Propel有一个实例池,用于重用querys

比如说

    <?php
// first call
$author1 = AuthorQuery::create()->getByFooField('foo');
$foo = $author1->getId();
// SELECT query
...
// second call
$author2 = AuthorQuery::create()->getByFooField('Foo');
// Skips the SQL query and returns the existing $author1 object
$foo = $author2->getId();

只是在我上面的评论之后添加了一个答案

基本上,实例池是由主键组织的对象集合。它不是查询缓存,而是特定实例之一。几乎在您请求特定记录的任何时候,它都会被添加到该类的实例池中。然后,当您第二次通过主键请求该记录时,spreep将返回缓存对象,而不是访问数据库。任何时候发出
->find()
spreep都将命中数据库,而不管实例池如何

在生成的基类中,通过查找
findPk
方法并查看它如何使用
getInstanceFromPool()
addInstanceToPool()
可以看到这种行为

例如:

$entity1 = EntityQuery::create()->findPk(13); // WILL hit database
$entity2 = EntityQuery::create()->findPk(13); // WILL NOT hit database

// This WILL hit DB, even if there is only 1 record & it is in the instance pool
$entity3 = EntityQuery::create()->findOneByName("Record Thirteen");
// This will also hit the DB EVERY TIME, regardless of the instance pool
// even though it is the same query
$entity4 = EntityQuery::create()->findOneByName("Record Thirteen");

// This will always hit the database, even if the instance is in the pool
$entity5 = EntityQuery::create()->findByUniqueField("Unique Value");

希望这有帮助。再次强调,这不是猜测,我已经进入代码并仔细检查了这一点。您可以查看生成的基类和
prople/runtime/lib/query/ModelCriteria.php
(特别是
find()
方法)来了解它是如何工作的。

我认为实例池不是在查询上工作的,而是在关系上工作的。换句话说,第一次执行
$author->getBooks()
sprip执行查询时,下一次它查看该
作者的
books
集合时,发现该集合已填充,因此不执行查询。如果你浏览
*Query
对象,我认为它总是执行查询。我不是这么想的,读这篇文章(我从这里得到了示例)注意,这些示例总是使用带有主
Id
字段的查询。实例池只是类/表和
Id
值的映射。无法在实例池中缓存对任意字段执行查找(在您的情况下为
Foo
),因为Prope不知道这些都是
Foo
字段具有该值的所有记录。如果Foo字段是唯一的,这将如何更改?不需要PK,如果不是唯一的键,我理解你的想法,但我只是告诉你推进是如何工作的。它不缓存查询,而是按
Id
缓存实例。任何时候执行
->find()
(或它的大多数变体)都会调用数据库。另一方面,如果您通过主键请求记录(而不是其他),那么spreep将首先检查实例池。(我将添加一个对此效果的答案,因为我已经亲自检查了代码。)