Hello,

In the book sections 
http://book.cakephp.org/view/78/Associations-Linking-Models-Together
3.7.6.2 hasOne #hasOne-80
3.7.6.3 belongsTo #belongsTo-81
3.7.6.4 hasMany #hasMany-82
3.7.6.5 hasAndBelongsToMany (HABTM) <same link>#hasAndBelongsToMany-
HABTM-83

I read: Possible keys for [hasOne|belongsTo|hasMany|HABTM] association
arrays include: ...
foreignKey: the name of the foreign key found in the [other|current]
model. This is especially handy if you need to define multiple [hasOne|
belongsTo|hasMany|HABTM] relationships.

And section 3.7.6.7 Multiple relations to the same model describes
cases where a Model has more then one relation to another Model.
However, I cannot seem to make this work.

What I'm trying to acheive is have the clon.name displayed in a select
box rather than the mated.motherID (or fatherID) displayed in a text
box when adding or editing a record in the mated table. And have the
form save the proper motherID (or fatherID) back to the mated table.
What I have below works for displaying the name in the index and
single record views but not in the add or edit form views. I.E.,
[motherID] => 3 displays 05-0000295 and [fatherID] => 7 displays
05-0000931 ONLY in index or view views and [pop_id] => 3 displays
WP-0000979 everywhere.

The specific details for my attempt to model this follow. Suggestions?

Thanx, DaveT.
///////////////////
// start backgound block
My pedigree (tree breeding) situation is slightly unusual in that the
scenario is based on the following recursive sequence:
A clone (individual tree) is selected from a population (of seeds
grown to seedlings).
A clone is mated with another clone to produce a new population (of
seeds).

... with the following qualifiers:
A clone may be mated with itself or a different clone.
A clone may be both a mother and a father parent at the same or
subsequent instances.
A mating of clones DOES NOT produce a new clone, it produces a new
generation (population) of seeds.
// end backgound block
///////////////////

///////////////////
// start data block
CREATE TABLE IF NOT EXISTS clons (
        id int(11) NOT NULL AUTO_INCREMENT,
        orig_id int(11) DEFAULT NULL,
        donor_id int(11) DEFAULT NULL,
        sourceID varchar(10) DEFAULT NULL,
        `date` date NOT NULL DEFAULT '0000-00-00',
        `name` char(10) NOT NULL,
        spp varchar(255) DEFAULT NULL,
        method set('','graft','root','se') DEFAULT NULL,
        `type` set('','pollen','seedling','cutting','tissue') DEFAULT NULL,
        num_created int(4) DEFAULT NULL,
        num_avail int(4) DEFAULT NULL,
        br_tested set('unknown','no','yes') NOT NULL DEFAULT 'unknown',
        br_resist set('unknown','no','yes') NOT NULL DEFAULT 'unknown',
        com text,
        PRIMARY KEY (id),
        KEY fk_orig (orig_id),
        KEY fk_donor (donor_id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

INSERT INTO clons (id, orig_id, donor_id, sourceID, date, name, spp,
method, type, num_created, num_avail, br_tested, br_resist, com)
VALUES
        (1, NULL, NULL, NULL, '0000-00-00', '00-0000001', 'wind', NULL, NULL,
NULL, NULL, 'unknown', 'unknown', 'marker'),
        (2, NULL, NULL, NULL, '0000-00-00', '00-0000002', 'P griffithi',
NULL, NULL, NULL, NULL, 'unknown', 'unknown', 'marker'),
        (3, 334, NULL, NULL, '0000-00-00', '05-0000295', 'P strobus x P
griffithi', NULL, NULL, NULL, NULL, 'unknown', 'unknown', NULL),
        (4, 327, NULL, NULL, '0000-00-00', '05-0000326', 'P griffithi', NULL,
NULL, NULL, NULL, 'unknown', 'unknown', NULL),
        (5, 214, NULL, NULL, '0000-00-00', '05-0000339', 'P griffithi x P
parviflora', NULL, NULL, NULL, NULL, 'unknown', 'unknown', NULL),
        (6, NULL, NULL, NULL, '0000-00-00', '05-0000927', 'P griffithi x P
strobus', NULL, NULL, NULL, NULL, 'unknown', 'unknown', NULL),
        (7, NULL, NULL, NULL, '0000-00-00', '05-0000931', 'P griffithi x
wind', NULL, NULL, NULL, NULL, 'unknown', 'unknown', 'spp originally P
griffithi x P strobus'),
        (8, NULL, NULL, NULL, '0000-00-00', '05-0001743', '(P griffithi x P
strobus) x (P griffithi x P strobus)', NULL, NULL, NULL, NULL,
'unknown', 'unknown', NULL),
        (9, NULL, NULL, NULL, '0000-00-00', '05-0001752', 'P griffithi x (P
griffithi x P parviflora)', NULL, NULL, NULL, NULL, 'unknown',
'unknown', NULL),
        (10, NULL, NULL, NULL, '0000-00-00', '05-0001766', '(P strobus x P
griffithi) x (P griffithi x wind)', NULL, NULL, NULL, NULL, 'unknown',
'unknown', NULL),
        (11, NULL, NULL, NULL, '2004-00-00', '05-0218401', '((P griffithi x
wind) x (P griffithi x wind)) x wind', NULL, 'cutting', NULL, NULL,
'unknown', 'unknown', 'originally: parent=run 2 rep 21 tree 2'),
        (12, NULL, NULL, NULL, '0000-00-00', '05-0219014', '(P strobus x P
griffithi) x wind', NULL, NULL, NULL, NULL, 'unknown', 'unknown',
NULL);

CREATE TABLE IF NOT EXISTS pops (
        id int(11) NOT NULL AUTO_INCREMENT,
        orig_id int(11) DEFAULT NULL,
        donor_id int(11) DEFAULT NULL,
        `date` date NOT NULL DEFAULT '0000-00-00',
        `name` char(10) NOT NULL,
        sourceID varchar(10) DEFAULT NULL,
        spp varchar(255) DEFAULT NULL,
        num_created int(4) DEFAULT NULL,
        num_avail int(4) DEFAULT NULL,
        com text,
        PRIMARY KEY (id),
        KEY fk_orig (orig_id),
        KEY fk_donor (donor_id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO pops (id, orig_id, donor_id, date, name, sourceID, spp,
num_created, num_avail, com) VALUES
        (1, 334, NULL, '0000-00-00', 'WP-0000139', NULL, 'P griffithi', NULL,
NULL, NULL),
        (2, 149, NULL, '0000-00-00', 'WP-0000799', NULL, 'P griffithi x (P
griffithi x P parviflora)', NULL, NULL, NULL),
        (3, 149, NULL, '0000-00-00', 'WP-0000979', NULL, '(P strobus x P
griffithi) x (P griffithi x wind)', NULL, NULL, NULL),
        (4, 149, NULL, '0000-00-00', 'WP-0001000', NULL, '(P griffithi x P
strobus) x (P griffithi x wind)', NULL, NULL, NULL),
        (5, 145, NULL, '0000-00-00', 'WP-0002184', NULL, '((P griffithi x P
strobus) x (P griffithi x P strobus)) x wind', NULL, NULL, NULL),
        (6, 146, NULL, '0000-00-00', 'WP-0002190', NULL, '((P strobus x P
griffithi) x (P griffithi x wind)) x wind', NULL, NULL, NULL);

CREATE TABLE IF NOT EXISTS mated (
        id int(11) NOT NULL AUTO_INCREMENT,
        motherID int(11) DEFAULT NULL,
        fatherID int(11) DEFAULT NULL,
        pop_id int(11) DEFAULT NULL,
        `date` date NOT NULL DEFAULT '0000-00-00',
        PRIMARY KEY (id),
        KEY fk_mother (motherID),
        KEY fk_father (fatherID),
        KEY fk_pop (pop_id)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO mated (id, motherID, fatherID, pop_id, date) VALUES
        (1, 2, NULL, 1, '0000-00-00'),
        (2, 4, 5, 2, '0000-00-00'),
        (3, 3, 7, 3, '0000-00-00'),
        (4, 6, 7, 4, '0000-00-00'),
        (5, 8, 1, 5, '0000-00-00'),
        (6, 10, 1, 6, '0000-00-00');
// end data block
///////////////////

///////////////////
// start model block
<?php
class Clon extends AppModel {
        var $name = 'Clon';
        var $validate = array(
                'name' => array('notempty')
        );

        var $belongsTo = array(
        /*      'Orig' => array( ... ),
                'Donor' => array( ... ) */
        ); // end $belongsTo

        var $hasMany = array( ... ),
                'Planted' => array( ... ),
                'Treated' => array( ... ),
                /* Attempt to link clon back to mated *** DOES NOT WORK ***
                'Mother' => array(
                        'className' => 'Mated',
                        'foreignKey' => 'motherID',
                        'dependent' => false,
                        'conditions' => '',
                        'fields' => '',
                        'order' => '',
                        'limit' => '',
                        'offset' => '',
                        'exclusive' => '',
                        'finderQuery' => '',
                        'counterQuery' => ''
                ),
                'Father' => array(
                        'className' => 'Mated',
                        'foreignKey' => 'fatherID',
                        'dependent' => false,
                        'conditions' => '',
                        'fields' => '',
                        'order' => '',
                        'limit' => '',
                        'offset' => '',
                        'exclusive' => '',
                        'finderQuery' => '',
                        'counterQuery' => ''
                )
                */
        ); // end $hasMany
}
?>

<?php
class Pop extends AppModel {
        var $name = 'Pop';
        var $validate = array(
                'name' => array('notempty')
        );

        var $belongsTo = array(
                'Orig' => array( ... ),
                'Donor' => array( ... )
        ); // end $belongsTo

        var $hasMany = array(
                'Generated' => array( ... ),
                'Mated' => array(
                        'className' => 'Mated',
                        'foreignKey' => 'pop_id',
                        'dependent' => false,
                        'conditions' => '',
                        'fields' => '',
                        'order' => '',
                        'limit' => '',
                        'offset' => '',
                        'exclusive' => '',
                        'finderQuery' => '',
                        'counterQuery' => ''
                ),
                'Planted' => array( ... ),
                'Treated' => array( ... )
        ); // end $hasMany
}
?>

<?php
class Mated extends AppModel {
        var $name = 'Mated';

        var $belongsTo = array(
                'Mother' => array(
                        'className' => 'Clon',
                        'foreignKey' => 'motherID',
                        'conditions' => '',
                        'fields' => '',
                        'order' => ''
                ),
                'Father' => array(
                        'className' => 'Clon',
                        'foreignKey' => 'fatherID',
                        'conditions' => '',
                        'fields' => '',
                        'order' => ''
                ),
                'Pop' => array(
                        'className' => 'Pop',
                        'foreignKey' => 'pop_id',
                        'conditions' => '',
                        'fields' => '',
                        'order' => ''
                )
        ); // end $belongsTo
}
?>
// end model block
///////////////////

///////////////////
// start debug block
<?php debug($this->data); ?> produces:
Array (
        [Mated] => Array (
                [id] => 3
                [motherID] => 3
                [fatherID] => 7
                [pop_id] => 3
                [date] => 0000-00-00
        )

        [Mother] => Array (
                [id] => 3
                [orig_id] => 334
                [donor_id] =>
                [sourceID] =>
                [date] => 0000-00-00
                [name] => 05-0000295
                [spp] => P strobus x P griffithi
                [method] =>
                [type] =>
                [num_created] =>
                [num_avail] =>
                [br_tested] => unknown
                [br_resist] => unknown
                [com] =>
        )

        [Father] => Array (
                [id] => 7
                [orig_id] =>
                [donor_id] =>
                [sourceID] =>
                [date] => 0000-00-00
                [name] => 05-0000931
                [spp] => P griffithi x wind
                [method] =>
                [type] =>
                [num_created] =>
                [num_avail] =>
                [br_tested] => unknown
                [br_resist] => unknown
                [com] => spp originally P griffithi x P strobus
        )

        [Pop] => Array (
                [id] => 3
                [orig_id] => 149
                [donor_id] =>
                [date] => 0000-00-00
                [name] => WP-0000979
                [sourceID] =>
                [spp] => (P strobus x P griffithi) x (P griffithi x wind)
                [num_created] =>
                [num_avail] =>
                [com] =>
        )
)
// end debug block
///////////////////

///////////////////
// start edit form block
<div class="mated form">
<?php echo $form->create('Mated');?>
        <fieldset>
                <legend>Edit Clone Mating</legend>
                <?php
                        echo $form->input('id');
                        echo $form->input('motherID', array( 'label' => 
'Mother', 'empty'
=> '', 'style' => 'width: 8em;' ));
                        echo $form->input('fatherID', array( 'label' => 
'Father', 'empty'
=> '', 'style' => 'width: 8em;' ));
                        echo $form->input('pop_id', array( 'label' => 
'Population', 'empty'
=> '' ));
                        echo $form->input('date', array( 'label' => 'Mating 
Date', 'empty'
=> '', 'type' => 'text', 'default' => '0000-00-00', 'style' => 'width:
8em;' ));
                ?>
        </fieldset>
        <div class="outerLeft">
        <?php
                echo $form->submit('Submit', array('div'=>false, 
'name'=>'Submit',
'style' => 'width: auto;' ));
                echo $form->submit('Cancel', array('div'=>false, 
'name'=>'Cancel',
'style' => 'width: auto;' ));
        ?>
        </div>
        <?php echo $form->end(); ?>
</div>
// end edit form block
///////////////////

///////////////////
// start view block
<?php echo $this->element('back_menu'); ?>
<div class="mated view">
<h2>Clone Mating</h2>
        <dl><?php $i = 0; $class = ' class="altrow"';?>
                <dt<?php if ($i % 2 == 0) echo $class;?>>Mother</dt>
                <dd<?php if ($i++ % 2 == 0) echo $class;?>>
                        <?php echo $mated['Mother']['name']; ?>
                        &nbsp;
                </dd>
                <dt<?php if ($i % 2 == 0) echo $class;?>>Father</dt>
                <dd<?php if ($i++ % 2 == 0) echo $class;?>>
                        <?php echo $mated['Father']['name']; ?>
                        &nbsp;
                </dd>
                <dt<?php if ($i % 2 == 0) echo $class;?>>Population</dt>
                <dd<?php if ($i++ % 2 == 0) echo $class;?>>
                        <?php echo $mated['Pop']['name']; ?>
                        &nbsp;
                </dd>
                <dt<?php if ($i % 2 == 0) echo $class;?>>Mating Date</dt>
                <dd<?php if ($i++ % 2 == 0) echo $class;?>>
                        <?php echo $mated['Mated']['date']; ?>
                        &nbsp;
                </dd>
        </dl>
</div>
// end view block
///////////////////
Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to