我所拥有的记录数仅为15000.并且配置使用的内存php为128 MB。所以我得到了这个错误。
允许的内存大小为134217728字节耗尽
有两种方法可以解决这个问题。
我感到困惑的是,如果我将允许的内存增加到256,那么当数据的数量变为30 000时,这个错误将再次出现。
所以当我开发大型应用程序时,我不应该使用Yii cactiverecord findAll()吗?或者我应该随着更多数据的增加而不断增加内存大小。
最好的方法是什么?
答案 0 :(得分:6)
尝试使用批次检索数据:
http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#data-in-batches
// fetch 10 customers at a time
foreach (Customer::find()->batch(10) as $customers) {
// $customers is an array of 10 or fewer Customer objects
}
// fetch 10 customers at a time and iterate them one by one
foreach (Customer::find()->each(10) as $customer) {
// $customer is a Customer object
}
// batch query with eager loading
foreach (Customer::find()->with('orders')->each() as $customer) {
// $customer is a Customer object with the 'orders' relation populated
}
答案 1 :(得分:0)
class ModelObject{
....
public function unsetModel()
{
foreach($this->getRelatedRecords() as $name => $record)
{
unset($this->{$name});
}
unset($this);
}
....
}
并在foreach内部调用该函数,如下所示:
foreach($modelObjects as $model)
{
....
$model->unsetModel();
}