Specify return fields for Relational Queries

This is my problem:

To generate a report, I have one table and about 3 joined table. At least 2 tables have a large number of fields.

Tables:

Student (40 fields)

School (10 fields)

Class (12 fields)

ZipCode (5 fields)

School has many classes

Classes has many student

Student has one ZipCode

In fact, I don’t need too many informations to my report. I’m trying to find my informations like this:


student::model()->with(array('school', 'class', 'zipcode'))->findAll();

Well, when a school have around 1000 students, PHP crashs with memory error. I realized that was because the joins and too many fields.

What I want to do:

Use criteria to define which fields I want to return from DB:

Something like




$criteria = new CDbCriteria();

$criteria->select = 'estudent_name, school.name, class.title';


student::model()->with(array('school', 'class', 'zipcode'))->findAll();



But this doesn’t work. Yii says it does not recognize ‘school.name’ or ‘class.title’.

  • I already added aliases to the related tables.

Any ideas?

Just a thought, but you can give php more memory via the memory_limit option in php.ini. It’s not necessarily a bad thing to do. I had to do this for my Drupal website even when just getting started.

Try this:




$criteria = new CDbCriteria();

$criteria->select = 'estudent_name';


student::model()->with(

  'school'=>array('select'=>'name'),

  'class'=>array('select'=>'title'),

  'zipcode'

  )->findAll($criteria);



(not tested)

/Tommy

I know, I’ve already configured it to 10M. But it is crashing anyway (and I don’t need too much information to crash it, but so many field in my related tables causes this)

Thanks Tommy, it is just what I am doing and it is working now! But thanks for your reply!

Just mentioning, I think that CDbCriteria shoul be fixed so that using like $criteria->select = ‘school.name’ should work.