I'm new in PHP, and also in Propel.
As I have read, Propel got a Instance Pool, to reuse querys.
For example
<?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();
the thing is
for how many time this objects are alive? This is origined because I have to parse an excel, and build some objects to persist them in the db.
So, maybe in the excel I read in 100 rows, I read 25 times 'foo', so, querying the db every time for getting the PK will be bad thing, if I can avoid 24 querys.
Looks like Instance Propel solves this problem, but I don't know for how many time.
That depends on time, memory used, or something like the scope where the querys are executed ? (I mean, maybe after the method that executes the first query, the Instance Pool is cleared, so, unless the second query is in the same method, it will query again the db. Or maybe while the object is alive, I don't know)
I've search a little but not found anything, mi intuition tell me that depends on memory used, but, nothing official.
Or maybe there's a better way of doing this.
Just adding an answer that follows my comments above.
Basically, the instance pool is a collection of objects organized by primary key. It is not a cache of queries, but rather one of specific instances. Just about any time you request a specific record it will be added to the instance pool for that class. When you then request that record by it's primary key a second time, Propel will give you back the cached object instead of hitting the database. Any time you issue a ->find()
Propel will hit the database, regardless of the instance pool.
In your generated base classes you can see this behavior by looking for the findPk
method and seeing how it uses getInstanceFromPool()
and addInstanceToPool()
.
For example:
$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");
Hope this helps. Again, this is not speculation, I have gone into the code and double checked this. You can look in your generated base classes and in propel/runtime/lib/query/ModelCriteria.php
(specifically the find()
method) to see how it works.