I have a model, Artist, that HABTM Discipline (eg. Visual Arts,
Theatre, etc.) The client has decided that they want each artist to
have a "primary" discipline. I've changed the model, then, to have one
Discipline in belongsTo and added a discipline_id column to the DB. So
far, so sort of good. However, updates on the model are not going so
well because the HABTM array will always include the primary. I have
some ideas for dealing with this but thought I'd see if anyone has run
into this before.

Here's what I have:

Artist

var $belongsTo = array('Discipline');
var $hasAndBelongsToMany = array(
        'Discipline' => array(
                'with' => 'ArtistsDiscipline',
                'className' => 'Discipline',
                'joinTable' => 'artists_disciplines',
                'foreignKey' => 'artist_id',
                'associationForeignKey' => 'discipline_id',
                'unique' => true
        )
);

Initially, the Artist looks something like this:

Array
(
    [Artist] => Array
        (
                [id] => 1
                [created] => 2008-05-10 17:02:56.155567
                [modified] => 2008-05-10 17:02:56.155567
                [discipline_id] => 2
                        // other stuff ...
        )
    [Discipline] => Array
        (
                [id] => 2
                [name_en] => Visual Arts
                [name_fr] => Arts Visuels
                [name_es] => Artes Visuales
                [0] => Array
                (
                        [id] => 3
                        [name_en] => Film / Television
                        [name_fr] => Cinéma / Télévision
                        [name_es] => Cine / Televisión
                )
                [1] => Array
                (
                        [id] => 9
                        [name_en] => Theatre
                        [name_fr] => Théâtre
                        [name_es] => Teatro
                )
        )
)

Already, the Discipine array looks like it needs some fixing. I figure
I could do something in afterFind() here.

The edit.ctp has:

$form->label('Artist.discipline_id', __('* Primary Artistic
Discipline', true), array('class' => 'Required'))

$form->error('Artist.discipline_id', __('Please select a primary
discipline', true)) ?>
$form->select('Artist.discipline_id', $disciplines, null, array(), true)
                
$form->label('Discipline.Discipline', __('Other Disciplines', true))
$habtm->checkboxMultiple('Discipline.Discipline', $disciplines, null,
array('class' => 'Required', 'multiple' => 'checkbox'))


No surprises, the "Visual Arts" checkbox will be checked. After an
update, the Discipline array looks like:

[Discipline] => Array
(
        [id] => 2
        [name_en] => Visual Arts
        [name_fr] => Arts Visuels
        [name_es] => Artes Visuales
        [0] => Array
        (
                [id] => 2
                [name_en] => Visual Arts
                [name_fr] => Arts Visuels
                [name_es] => Artes Visuales
        )
        [1] => Array
        (
                [id] => 3
                [name_en] => Film / Television
                [name_fr] => Cinéma / Télévision
                [name_es] => Cine / Televisión
        )
        [2] => Array
        (
                [id] => 9
                [name_en] => Theatre
                [name_fr] => Théâtre
                [name_es] => Teatro
        )
)

So, I'm wondering what the best strategy would be. Should I modify
this in Artist's afterFind() or Discipline's? And, if the latter, how
can I get the Artist's discipline_id (so I can remove that entry from
the array)? I thought about doing it in Artist's beforeSave() but then
the primary will still always appear checked in the secondary group.

I tried this in Artist's afterFind()

/* Remove primary Discipline from list of secondaries, which
 * are indexed numerically.
 */
if (isset($results[0]['Discipline']))
{
        foreach(array_keys($results[0]['Discipline']) as $key)
        {
                if (!is_numeric($key))
                {
                        unset($results[0]['Discipline'][$key]);
                }
        }
}

The problem with that is I need that to be able to display the name of
the primary.

Then I remembered that I can use an alias in $belongsTo:

var $belongsTo = array(
        'PrimaryDiscipline' => array(
                'className'  => 'Discipline',
                'conditions' => '',
                'order'      => '',
                'foreignKey' => 'discipline_id'
        )
);

I thought that using PrimaryDiscipline would give me an array with
that as a key (and the other as Discipline). But that leads to a
failed DB query: SQL Error: ERROR:  missing FROM-clause entry for
table "Discipline"

My next thought was to remove the alias from $belongsTo but alter the
array to include it in afterFind(). Aside from that, I'm out of ideas
at this point. Anyone done this before?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to