I'm trying to move an older site/database into Yii, so I already have the DB built.
Let's say I have groups, and the groups have members. I have a group list page where I'd display all the groups and the member count.
If the limit is larger than 5, PHP dies with a Fatal Error:
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 624 bytes) in C:xampplitehtdocsyiiframeworkdbarCActiveRecord.php on line 607
It's probably the way I'm doing the count, so here it is:
If you are executing thousands of SQLs (usually in command line), you should set YII_DEBUG to be false so that the trace messages are not logged. The trace messages are the major cause of memory consumption in such case.
yes, I have 1000s of records the reason why I have my DEBUG turned ON is because I’d like to see (in development mode) the actual SQL query that I’m trying to execute.
I'm fairly new to this "BELONGS_TO and HAS_MANY" syntax and until I figure out how it really works, I'd like to check that I'm executing the right SQL queries…
Having 1000s of records is not a problem. Are you executing 1000s of SQLs in a single request? Usually such memory exhaustion is caused by accumulative logging messages.
don’t even ask this is an old(er) structure we have to work with, and move the app to a newer platform (ie: Yii)
they used to go through table1 with a foreach() and query the table2 every time.
so instead I just want to join the second table and count the members. (table1=groups, table2=members) joined by the keys (foreign1=foreign1 AND foreign2=foreign2)
maybe I'm gonna have to try a different approach!?
SELECT
`groups`.`index` AS `t0_c0`, t1.`name` AS `t1_c2`,
t1.`desc` AS `t1_c4`, t1.`members.index` AS `t1_c13`
FROM `groups`
LEFT OUTER JOIN `members` t1 ON (t1.`userID`=`groups`.`index`)
AND
(t1.`groupID`=`groups`.`index`)
WHERE (`groups`.`index` IN (8, 15, 13, 11,
12))
and I'd need this instead:
SELECT
`groups`.`index` AS `t0_c0`, t1.`name` AS `t1_c2`,
t1.`desc` AS `t1_c4`, t1.`members.index` AS `t1_c13`
FROM `groups`
LEFT OUTER JOIN `members` t1 ON (t1.`userID`=`groups`.`userID`)
AND
(t1.`groupID`=`groups`.`groupID`)
WHERE (`groups`.`index` IN (8, 15, 13, 11,
12))
so as you can see, it's trying to JOIN the members table on the index instead of my userID and groupID (which would totally make sense unser "normal" surcamstances!)
so the question is: is there a way to tell Yii, hey join this 2 tables, but use these keys combined!
had to add a public variable called: $calculated_value!
it is still weird, why it's returning all the columns (*) rather just the ones I'm asking for. Is that because the app is in development mode? (trace, logging is turned ON!)
yes, that's right. You need a public accessible variable in your model for every field of your model. The variables for the fields from your database table are generated automatically - so you don't "see" them.
Quote
it is still weird, why it's returning all the columns (*) rather just the ones I'm asking for.
You're working with active records and using findAllBySql() will return a set of active records and therefore each record is filled with it's data.
If you want to use a direct SQL-Query you can use something like: