or just $offest = strtolower(substr($field, -3)) == '_id';
-- Lep pozdrav, Tilen Majerle http://majerle.eu 2011/3/12 cricket <[email protected]> > On Sat, Mar 12, 2011 at 12:12 AM, Ryan Schmidt > <[email protected]> wrote: > > > > On Mar 9, 2011, at 09:29, Tilen Majerle wrote: > > > >> because of _id, if you connect 2 models, then they are connected with > modelalias_id in other controller so like that > >> > >> POST model (hasMany Comment) > >> posts table: > >> > >> id, > >> title, > >> created, > >> modified, > >> ...other rows in table > >> > >> COMMENT model (belongsTo Post) > >> comments table: > >> > >> id, > >> post_id //connected with post model, but you have _ident > >> created, > >> modified, > >> ... all other rows... > >> > >> > >> i Hope you understan > > > > I do not understand. Surely "_id" is only a suffix -- used only at the > end of a column name. Why then is it being abused by CakePHP in the middle > of a column name? > > Yes, looks like a regexp bug. It should use '_id$', I think. > > ... nope. It's not a regexp. In 1.3.7, console/libs/tasks/model.php, line > 502: > > function findBelongsTo(&$model, $associations) { > $fields = $model->schema(true); > foreach ($fields as $fieldName => $field) { > $offset = strpos($fieldName, '_id'); > if ($fieldName != $model->primaryKey && $fieldName != > 'parent_id' && > $offset !== false) { > $tmpModelName = > $this->_modelNameFromKey($fieldName); > $associations['belongsTo'][] = array( > 'alias' => $tmpModelName, > 'className' => $tmpModelName, > 'foreignKey' => $fieldName, > ); > } elseif ($fieldName == 'parent_id') { > $associations['belongsTo'][] = array( > 'alias' => 'Parent' . $model->name, > 'className' => $model->name, > 'foreignKey' => $fieldName, > ); > } > } > return $associations; > } > > Using strpos like this is no good because the actual position is being > ignored. It's simply checking to see if itoccurs at all somewhere. A > better approach would be to use a regexp to test it's at the end of > the string. > > foreach ($fields as $fieldName => $field) { > if ($fieldName != $model->primaryKey && $fieldName != 'parent_id' && > preg_match('/.+_id$/', $fieldName)) { > > > Untested, but it should not match a column with "_id" anywhere but the > end of the name. It should also not match columns named "_id". > > heohni, perhaps you could make a copy of the file and try baking again > with the above mod. If it works, feel free to file a bug and suggest > my fix. > https://launchpad.net/bugs/bugtrackers/cake-trac > > -- > Our newest site for the community: CakePHP Video Tutorials > http://tv.cakephp.org > Check out the new CakePHP Questions site http://ask.cakephp.org and help > others with their CakePHP related questions. > > > To unsubscribe from this group, send email to > [email protected] For more options, visit this group > at http://groups.google.com/group/cake-php > -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php
