diem-project/diem — Issue #419: Problem by generating name and slug of an automatic page using dmSeoSynchronizer
Author: kisskrool
Created: 2011-10-24 14:37:54+00:00
State: open

Hi,

Wanting to automatically creating name and slug for the page of one of my models called 'Annonce', I've stumbled upon a problem which involves how I've created my relations.

Here is my schema.yml:

Annonce:
columns:
resume: {type: string(500), notnull: true}
loyer_cc: {type: integer, notnull: true}
insee: {type: string(5), notnull: true}
surface: {type: integer, notnull: true}
personne_a_contacter: {type: string(20), notnull: true}
telephone: {type: string(20), notnull: true}
nb_pieces_id: {type: integer, notnull: true}
user_id: {type: integer, notnull: true}
active_par_mail: { type: boolean, notnull: true, default: false }
is_active: { type: boolean, notnull: true, default: false }
code_valid_user: {type: string(10)}
code_valid_admin: {type: string(10)}
indexes:
index_ville:
fields: [insee]
index_loyer:
fields: [loyer_cc]
index_surface:
fields: [surface]
actAs:
Timestampable:
DmCommentable:
DmTaggable:
relations:
DmUser:
class: DmUser
local: user_id
foreignAlias: Annonces
onDelete: CASCADE
onUpdate: CASCADE
Ville:
class: Ville
local: insee
foreign: insee
foreignAlias: Annonces
onDelete: RESTRICT
onUpdate: CASCADE
NbPieces:
class: NbPieces
local: nb_pieces_id
foreignAlias: Annonces
onDelete: RESTRICT
onUpdate: CASCADE

Ville:
columns:
commune: {type: string(30), notnull: true}
code_postal: {type: string(5), notnull: true}
departement: {type: string(30), notnull: true}
insee: {type: string(5), notnull: true, unique:true}
nb_annonces: {type: integer, notnull: true, default: 0}
indexes:
index_commune:
fields: [commune, code_postal]

So, 'Annonce' and 'Ville' are linked by a one to many relation (Doctrine interpreted as such this relation) with the common field 'insee' which has been set unique in 'Ville'.

Searching in /dmCorePlugin/lib/seo/dmSeoSynchronizer.php (DIEM 5.4), I found at line 389:

$usedRecord = $record->getRelatedRecord($this->moduleManager->getModule($usedModuleKey)->getModel());

And when looking at the method getRelatedRecord at line 455 in /dmCorePlugin/lib/doctrine/record/dmDoctrineRecord.php

if ($relation instanceof Doctrine_Relation_LocalKey)//problem getLocalColumnName
{
return $relation['table']->createQuery('dm_foreign')
//->where('dm_foreign.id = ?', $this->get($relation->getLocal()))
->where('dm_foreign.'.$relation->getForeignColumnName().' = ?', $this->get($relation->getLocal())) //added foreign column name
->withI18n(null, $class, 'dm_foreign')
->fetchOne(array(), $hydrationMode);
}
elseif($relation instanceof Doctrine_Relation_ForeignKey)
{
return $relation['table']->createQuery('dm_foreign')
->where('dm_foreign.'.$relation->getForeignColumnName().' = ?', $this->get('id'))
->withI18n(null, $class, 'dm_foreign')
->fetchOne(array(), $hydrationMode);
}
elseif($relation instanceof Doctrine_Relation_Association)
{
return $relation['table']->createQuery('dm_foreign')
->leftJoin('dm_foreign.'.$relation['refTable']->getComponentName().' ref_table')
->where('ref_table.'.$relation->getLocal().' = ?', $this->get('id'))
->withI18n(null, $class, 'dm_foreign')
->fetchOne(array(), $hydrationMode);
}

In fact the at least two first types of relations handled are in the original code considered to have the column 'id' as local or foreign key. This provokes then the following query as found in 'front_dev.log':

SELECT v.id AS v__id, v.commune AS v__commune, v.code_postal AS v__code_postal, v.departement AS v__departement, v.insee AS v__insee, v.nb_annonces AS v__nb_annonces
FROM ville v
WHERE (v.id = ?) - (86194)

Which breaks the first part of my slug with a 'Annonce-ville not found'.

I don't know if I'm the first person to face this problem, but I think this issue should be quite easy to solve, at least for the two first use cases one-to-many and many-to-one, am I right?

Regards